Convert AuthenticationType constants to enum cases

This commit is contained in:
netkas 2024-09-14 08:30:41 -04:00
parent 12f0ff2ffa
commit e02f1f56dc
8 changed files with 21 additions and 19 deletions

View file

@ -473,7 +473,7 @@
{ {
switch($authentication->getAuthenticationType()) switch($authentication->getAuthenticationType())
{ {
case AuthenticationType::ACCESS_TOKEN: case AuthenticationType::ACCESS_TOKEN->value:
if($authentication instanceof AccessToken) if($authentication instanceof AccessToken)
{ {
$headers[] = 'Authorization: token ' . $authentication->getAccessToken(); $headers[] = 'Authorization: token ' . $authentication->getAccessToken();
@ -482,7 +482,7 @@
throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType())); throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType()));
case AuthenticationType::USERNAME_PASSWORD: case AuthenticationType::USERNAME_PASSWORD->value:
if($authentication instanceof UsernamePassword) if($authentication instanceof UsernamePassword)
{ {
curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword()); curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword());

View file

@ -453,7 +453,7 @@
{ {
switch($authentication->getAuthenticationType()) switch($authentication->getAuthenticationType())
{ {
case AuthenticationType::ACCESS_TOKEN: case AuthenticationType::ACCESS_TOKEN->value:
if($authentication instanceof AccessToken) if($authentication instanceof AccessToken)
{ {
$headers[] = 'Authorization: Bearer ' . $authentication->getAccessToken(); $headers[] = 'Authorization: Bearer ' . $authentication->getAccessToken();
@ -461,7 +461,7 @@
} }
throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType())); throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType()));
case AuthenticationType::USERNAME_PASSWORD: case AuthenticationType::USERNAME_PASSWORD->value:
if($authentication instanceof UsernamePassword) if($authentication instanceof UsernamePassword)
{ {
curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword()); curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword());

View file

@ -471,7 +471,7 @@
{ {
switch($authentication->getAuthenticationType()) switch($authentication->getAuthenticationType())
{ {
case AuthenticationType::ACCESS_TOKEN: case AuthenticationType::ACCESS_TOKEN->value:
if($authentication instanceof AccessToken) if($authentication instanceof AccessToken)
{ {
$headers[] = 'Private-Token: ' . $authentication->getAccessToken(); $headers[] = 'Private-Token: ' . $authentication->getAccessToken();
@ -480,7 +480,7 @@
throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType())); throw new AuthenticationException(sprintf('Invalid authentication type for Access Token, got %s instead', $authentication->getAuthenticationType()));
case AuthenticationType::USERNAME_PASSWORD: case AuthenticationType::USERNAME_PASSWORD->value:
if($authentication instanceof UsernamePassword) if($authentication instanceof UsernamePassword)
{ {
curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword()); curl_setopt($curl, CURLOPT_USERPWD, $authentication->getUsername() . ':' . $authentication->getPassword());

View file

@ -22,15 +22,15 @@
namespace ncc\Enums\Types; namespace ncc\Enums\Types;
final class AuthenticationType enum AuthenticationType : int
{ {
/** /**
* A combination of a username and password is used for authentication * A combination of a username and password is used for authentication
*/ */
public const USERNAME_PASSWORD = 1; case USERNAME_PASSWORD = 1;
/** /**
* A single private access token is used for authentication * A single private access token is used for authentication
*/ */
public const ACCESS_TOKEN = 2; case ACCESS_TOKEN = 2;
} }

View file

@ -177,10 +177,10 @@
$input = []; $input = [];
switch($entry->getPassword()->getAuthenticationType()) switch($entry->getPassword()->getAuthenticationType())
{ {
case AuthenticationType::USERNAME_PASSWORD: case AuthenticationType::USERNAME_PASSWORD->value:
$input = ['password' => $password]; $input = ['password' => $password];
break; break;
case AuthenticationType::ACCESS_TOKEN: case AuthenticationType::ACCESS_TOKEN->value:
$input = ['token' => $password]; $input = ['token' => $password];
break; break;
} }

View file

@ -103,7 +103,7 @@
switch($this->password->getAuthenticationType()) switch($this->password->getAuthenticationType())
{ {
case AuthenticationType::USERNAME_PASSWORD: case AuthenticationType::USERNAME_PASSWORD->value:
if(!($this->password instanceof UsernamePassword)) if(!($this->password instanceof UsernamePassword))
{ {
return false; return false;
@ -129,7 +129,7 @@
return $username === $this->password->getUsername() && $password === $this->password->getPassword(); return $username === $this->password->getUsername() && $password === $this->password->getPassword();
case AuthenticationType::ACCESS_TOKEN: case AuthenticationType::ACCESS_TOKEN->value:
if(!($this->password instanceof AccessToken)) if(!($this->password instanceof AccessToken))
{ {
return false; return false;
@ -402,8 +402,8 @@
{ {
$self->password = match (Functions::array_bc($password, 'authentication_type')) $self->password = match (Functions::array_bc($password, 'authentication_type'))
{ {
AuthenticationType::USERNAME_PASSWORD => UsernamePassword::fromArray($password), AuthenticationType::USERNAME_PASSWORD->value => UsernamePassword::fromArray($password),
AuthenticationType::ACCESS_TOKEN => AccessToken::fromArray($password) AuthenticationType::ACCESS_TOKEN->value => AccessToken::fromArray($password)
}; };
} }
} }

View file

@ -69,7 +69,8 @@
*/ */
public function getAuthenticationType(): string public function getAuthenticationType(): string
{ {
return AuthenticationType::ACCESS_TOKEN; // TODO: Could return the enum case here
return AuthenticationType::ACCESS_TOKEN->value;
} }
/** /**
@ -91,7 +92,7 @@
public function toArray(bool $bytecode=false): array public function toArray(bool $bytecode=false): array
{ {
return [ return [
($bytecode ? Functions::cbc('authentication_type') : 'authentication_type') => AuthenticationType::ACCESS_TOKEN, ($bytecode ? Functions::cbc('authentication_type') : 'authentication_type') => AuthenticationType::ACCESS_TOKEN->value,
($bytecode ? Functions::cbc('access_token') : 'access_token') => $this->access_token, ($bytecode ? Functions::cbc('access_token') : 'access_token') => $this->access_token,
]; ];
} }

View file

@ -96,7 +96,8 @@
*/ */
public function getAuthenticationType(): string public function getAuthenticationType(): string
{ {
return AuthenticationType::USERNAME_PASSWORD; // TODO: Could return the enum here
return AuthenticationType::USERNAME_PASSWORD->value;
} }
/** /**
@ -115,7 +116,7 @@
public function toArray(bool $bytecode=false): array public function toArray(bool $bytecode=false): array
{ {
return [ return [
($bytecode ? Functions::cbc('authentication_type') : 'authentication_type') => AuthenticationType::USERNAME_PASSWORD, ($bytecode ? Functions::cbc('authentication_type') : 'authentication_type') => AuthenticationType::USERNAME_PASSWORD->value,
($bytecode ? Functions::cbc('username') : 'username') => $this->username, ($bytecode ? Functions::cbc('username') : 'username') => $this->username,
($bytecode ? Functions::cbc('password') : 'password') => $this->password, ($bytecode ? Functions::cbc('password') : 'password') => $this->password,
]; ];