Skip to content

Commit

Permalink
Merge branch 'PHP8' into 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelJ2324 committed Dec 17, 2024
2 parents 3ed3ac9 + 57a6a49 commit 5d28571
Show file tree
Hide file tree
Showing 51 changed files with 244 additions and 338 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"api"
],
"require": {
"php": ">=7.4",
"php": ">=8.0",
"guzzlehttp/guzzle": ">=6.3.3",
"psr/log": ">=1",
"psr/simple-cache": "1.*|2.*",
Expand Down
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
SimplifyEmptyCheckOnEmptyArrayRector::class,
])
->withSets([
SetList::PHP_74,
SetList::PHP_80,
SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
Expand Down
56 changes: 22 additions & 34 deletions src/Auth/Abstracts/AbstractAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,28 @@ abstract class AbstractAuthController implements AuthControllerInterface

/**
* Configured Actions on the Controlller
* @var array
*/
protected $actions = [];
protected array $actions = [];

/**
* Configured Endpoints for configured actions
* @var array
*/
protected $endpoints = [];
protected array $endpoints = [];

/**
* The credentials used for authentication
* @var array
*/
protected $credentials = [];
protected array $credentials = [];

/**
* The authentication token
* @var mixed
*/
protected $token;
protected mixed $token;

/**
* The Cache Key to store the token
* @var string
*/
protected $cacheKey;
protected string $cacheKey;

public function __construct()
{
Expand All @@ -63,7 +58,7 @@ public function __construct()
/**
* @inheritdoc
*/
public function setCredentials(array $credentials)
public function setCredentials(array $credentials): static
{
$this->credentials = $credentials;
$this->cacheKey = '';
Expand All @@ -87,7 +82,7 @@ public function getCacheKey(): string
/**
* @inheritdoc
*/
public function updateCredentials(array $credentials): AuthControllerInterface
public function updateCredentials(array $credentials): static
{
return $this->setCredentials(array_replace($this->getCredentials(), $credentials));
}
Expand All @@ -103,7 +98,7 @@ public function getCredentials(): array
/**
* @inheritDoc
*/
public function setToken($token)
public function setToken($token): static
{
$this->token = $token;
$this->cacheToken();
Expand All @@ -113,25 +108,25 @@ public function setToken($token)
/**
* @inheritdoc
*/
public function getToken()
public function getToken(): mixed
{
return $this->token;
return $this->token ?? null;
}

/**
* Clear the token property to null
* @return $this
*/
public function clearToken()
public function clearToken(): static
{
$this->token = null;
unset($this->token);
return $this;
}

/**
* @inheritdoc
*/
public function setActions(array $actions)
public function setActions(array $actions): static
{
$this->actions = $actions;
return $this;
Expand All @@ -148,7 +143,7 @@ public function getActions(): array
/**
* @inheritdoc
*/
public function setActionEndpoint(string $action, EndpointInterface $Endpoint): AuthControllerInterface
public function setActionEndpoint(string $action, EndpointInterface $Endpoint): static
{
if (in_array($action, $this->actions)) {
$this->endpoints[$action] = $Endpoint;
Expand Down Expand Up @@ -224,7 +219,7 @@ public function logout(): bool
/**
* @inheritDoc
**/
public function reset(): AuthControllerInterface
public function reset(): static
{
$this->credentials = [];
return $this->clearToken();
Expand All @@ -240,9 +235,8 @@ protected function cacheToken(): bool

/**
* Get the cached token for the Auth Controller
* @return mixed
*/
protected function getCachedToken()
protected function getCachedToken(): mixed
{
return $this->getCache()->get($this->getCacheKey(), null);
}
Expand All @@ -261,16 +255,11 @@ protected function removeCachedToken(): bool
*/
protected function configureEndpoint(EndpointInterface $Endpoint, $action): EndpointInterface
{
switch ($action) {
case self::ACTION_AUTH:
$Endpoint = $this->configureAuthenticationEndpoint($Endpoint);
break;
case self::ACTION_LOGOUT:
$Endpoint = $this->configureLogoutEndpoint($Endpoint);
break;
}

return $Endpoint;
return match ($action) {
self::ACTION_AUTH => $this->configureAuthenticationEndpoint($Endpoint),
self::ACTION_LOGOUT => $this->configureLogoutEndpoint($Endpoint),
default => $Endpoint,
};
}

/**
Expand All @@ -290,10 +279,9 @@ protected function configureLogoutEndpoint(EndpointInterface $Endpoint): Endpoin
/**
* Given a response from Authentication endpoint, parse the response
*
* @return mixed
* @codeCoverageIgnore
*/
protected function parseResponseToToken(string $action, Response $response)
protected function parseResponseToToken(string $action, Response $response): mixed
{
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Auth/Abstracts/AbstractBasicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ abstract class AbstractBasicController extends AbstractAuthController

public const DEFAULT_AUTH_TYPE = 'Basic';

protected static $_AUTH_HEADER = self::DEFAULT_AUTH_HEADER;
protected static string $_AUTH_HEADER = self::DEFAULT_AUTH_HEADER;

protected static $_AUTH_TYPE = self::DEFAULT_AUTH_TYPE;
protected static string $_AUTH_TYPE = self::DEFAULT_AUTH_TYPE;

/**
* @inheritdoc
Expand Down
42 changes: 16 additions & 26 deletions src/Auth/Abstracts/AbstractOAuth2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ abstract class AbstractOAuth2Controller extends AbstractBasicController

public const OAUTH_REFRESH_GRANT = 'refresh_token';

/**
* @var string
*/
protected static $_DEFAULT_GRANT_TYPE = self::OAUTH_CLIENT_CREDENTIALS_GRANT;
protected static string $_DEFAULT_GRANT_TYPE = self::OAUTH_CLIENT_CREDENTIALS_GRANT;

/**
* @inheritdoc
* The type of OAuth token we are receiving
*/
protected static $_AUTH_TYPE = self::DEFAULT_AUTH_TYPE;
protected static string $_AUTH_TYPE = self::DEFAULT_AUTH_TYPE;

/**
* @inheritdoc
Expand All @@ -43,12 +40,9 @@ abstract class AbstractOAuth2Controller extends AbstractBasicController
* The OAuth2 Full token
* @var array
*/
protected $token = [];
protected mixed $token;

/**
* @var
*/
protected $grant_type;
protected string $grant_type;

public function __construct()
{
Expand All @@ -60,7 +54,7 @@ public function __construct()
* @param $grant_type
* @return $this
*/
public function setGrantType($grant_type): AuthControllerInterface
public function setGrantType(string $grant_type): AuthControllerInterface
{
$this->grant_type = $grant_type;
return $this;
Expand All @@ -75,7 +69,7 @@ public function getGrantType(): string
* Get/Set the OAuth Authorization header
* @param $header
*/
public static function oauthHeader($header = null): string
public static function oauthHeader(string $header = null): string
{
if ($header !== null) {
static::$_AUTH_HEADER = $header;
Expand All @@ -88,7 +82,7 @@ public static function oauthHeader($header = null): string
* @inheritdoc
* @throws InvalidToken
*/
public function setToken($token)
public function setToken($token): static
{
if (is_array($token) && isset($token['access_token'])) {
$token = $this->configureToken($token);
Expand Down Expand Up @@ -127,9 +121,9 @@ protected function configureToken($token)
* @param $prop
* @return mixed|null
*/
public function getTokenProp($prop)
public function getTokenProp(string $prop): mixed
{
if ($this->token) {
if (isset($this->token)) {
if (is_object($this->token) && $this->token->$prop) {
return $this->token->$prop;
} elseif (is_array($this->token) && isset($this->token[$prop])) {
Expand Down Expand Up @@ -236,12 +230,10 @@ protected function isTokenExpired(): int
*/
protected function configureEndpoint(EndpointInterface $Endpoint, $action): EndpointInterface
{
switch ($action) {
case self::ACTION_OAUTH_REFRESH:
return $this->configureRefreshEndpoint($Endpoint);
default:
return parent::configureEndpoint($Endpoint, $action);
}
return match ($action) {
self::ACTION_OAUTH_REFRESH => $this->configureRefreshEndpoint($Endpoint),
default => parent::configureEndpoint($Endpoint, $action),
};
}

/**
Expand Down Expand Up @@ -271,18 +263,16 @@ protected function configureAuthenticationEndpoint(EndpointInterface $Endpoint):
/**
* @inheritDoc
*/
public function reset(): AuthControllerInterface
public function reset(): static
{
$this->setGrantType(static::$_DEFAULT_GRANT_TYPE);
return parent::reset();
}

/**
* Parse token responses as string
*
* @return mixed
*/
protected function parseResponseToToken(string $action, Response $response)
protected function parseResponseToToken(string $action, Response $response): mixed
{
$tokenStr = $response->getBody()->getContents();
$response->getBody()->rewind();
Expand Down
15 changes: 7 additions & 8 deletions src/Auth/AuthControllerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ public function getCredentials(): array;
* Set the credentials used for authentication
* @return $this
*/
public function setCredentials(array $credentials);
public function setCredentials(array $credentials): static;

/**
* Update parts of credentials used for authentication
* @return $this
*/
public function updateCredentials(array $credentials);
public function updateCredentials(array $credentials): static;

/**
* @return $this
*/
public function setActions(array $actions);
public function setActions(array $actions): static;

public function getActions(): array;

/**
* @return $this
*/
public function setActionEndpoint(string $action, EndpointInterface $Endpoint);
public function setActionEndpoint(string $action, EndpointInterface $Endpoint): static;

/**
* Get the Endpoint configured for an action
Expand Down Expand Up @@ -62,7 +62,7 @@ public function logout(): bool;
* Reset the auth controller to default state. Does not call 'logout' but does clear current token/credentials
* @return $this
*/
public function reset();
public function reset(): static;

/**
* Is currently authenticated
Expand All @@ -74,11 +74,10 @@ public function isAuthenticated(): bool;
* @param $token mixed
* @return $this
*/
public function setToken($token);
public function setToken(mixed $token): static;

/**
* Get the current token on the Auth Controller
* @return mixed
*/
public function getToken();
public function getToken(): mixed;
}
Loading

0 comments on commit 5d28571

Please sign in to comment.