diff --git a/.php_cs.dist b/.php-cs-fixer.dist.php similarity index 60% rename from .php_cs.dist rename to .php-cs-fixer.dist.php index acb275a..b15fc94 100755 --- a/.php_cs.dist +++ b/.php-cs-fixer.dist.php @@ -1,13 +1,15 @@ exclude('vendor') ->in(__DIR__); -return PhpCsFixer\Config::create() +return (new Config()) ->setRules([ '@Symfony' => true, - 'yoda_style' => null, // Do not enforce Yoda style (add unit tests instead...) + 'yoda_style' => false, // Do not enforce Yoda style (add unit tests instead...) 'ordered_imports' => true, ]) ->setFinder($finder); diff --git a/.scrutinizer.yml b/.scrutinizer.yml index b577bec..21b9c23 100755 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -32,7 +32,7 @@ build: - command: '~/.symfony/bin/symfony security:check --force-update' only_if: '[ "$SCRUTINIZER_BRANCH" == "master" ] && [ -z "$SCRUTINIZER_PR_SOURCE_BRANCH" ]' - - command: './vendor/bin/phpunit --coverage-clover=coverage-clover.xml' + - command: 'XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-clover=coverage-clover.xml --coverage-text' coverage: file: 'coverage-clover.xml' format: 'clover' @@ -41,7 +41,7 @@ build: environment: php: - version: '7.4' + version: '8.2' build_failure_conditions: - 'elements.rating(<= D).new.exists' # No new classes/methods with a rating of D or worse. diff --git a/composer.json b/composer.json index 51110f0..739d466 100755 --- a/composer.json +++ b/composer.json @@ -10,14 +10,14 @@ } ], "require": { - "php": "^7.1.3 || ^8.1", - "symfony/cache": "^4.2 || ^5.0 || ^6.1", - "symfony/http-client-contracts": "^1.1 || ^2 || ^3.3" + "php": "^8.2", + "symfony/cache": "^6.4", + "symfony/http-client-contracts": "^3.4" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.15", - "phpunit/phpunit": "^8.3", - "symfony/http-client": "^4.3" + "friendsofphp/php-cs-fixer": "^3.45", + "phpunit/phpunit": "^10.0", + "symfony/http-client": "^6.4" }, "suggest": { "symfony/http-client": "This package requires an actual Symfony HTTP client implementation to decorate." diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f9d29e3..295260b 100755 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,23 +1,21 @@ - - + + - + ./tests/ - - - + + ./src/ - - + + diff --git a/src/AccessToken.php b/src/AccessToken.php index 601bea4..982dccb 100755 --- a/src/AccessToken.php +++ b/src/AccessToken.php @@ -7,25 +7,10 @@ * * @author Niels Nijens */ -class AccessToken +readonly class AccessToken { - /** - * @var string - */ - private $token; - - /** - * @var int - */ - private $ttl; - - /** - * Constructs a new AccessToken instance. - */ - public function __construct(string $token, int $ttl) + public function __construct(private string $token, private int $ttl) { - $this->token = $token; - $this->ttl = $ttl; } public function getToken(): string diff --git a/src/AuthZeroAuthenticatingHttpClient.php b/src/AuthZeroAuthenticatingHttpClient.php index 0db1d6b..3b80e00 100755 --- a/src/AuthZeroAuthenticatingHttpClient.php +++ b/src/AuthZeroAuthenticatingHttpClient.php @@ -3,11 +3,11 @@ namespace Superbrave\AuthZeroHttpClient; use Symfony\Component\Cache\Adapter\ArrayAdapter; +use Symfony\Component\HttpClient\DecoratorTrait; use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\Cache\ItemInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; -use Symfony\Contracts\HttpClient\ResponseStreamInterface; /** * Handles authentication with Auth0 before actual requests are made. @@ -16,37 +16,18 @@ */ class AuthZeroAuthenticatingHttpClient implements HttpClientInterface { - /** - * @var HttpClientInterface - */ - private $client; - - /** - * @var AuthZeroConfiguration - */ - private $authZeroConfiguration; + use DecoratorTrait; - /** - * @var CacheInterface - */ - private $accessTokenCache; - - /** - * Constructs a new AuthZeroAuthenticatingHttpClient instance. - */ public function __construct( HttpClientInterface $client, - AuthZeroConfiguration $authZeroConfiguration, - CacheInterface $accessTokenCache = null + private readonly AuthZeroConfiguration $authZeroConfiguration, + private ArrayAdapter|CacheInterface|null $accessTokenCache = null, ) { - $this->client = $client; - $this->authZeroConfiguration = $authZeroConfiguration; - - if ($accessTokenCache === null) { - $accessTokenCache = new ArrayAdapter(); + if ($this->accessTokenCache === null) { + $this->accessTokenCache = new ArrayAdapter(); } - $this->accessTokenCache = $accessTokenCache; + $this->client = $client; } /** @@ -61,14 +42,6 @@ public function request(string $method, string $url, array $options = []): Respo return $this->client->request($method, $url, $options); } - /** - * {@inheritdoc} - */ - public function stream($responses, float $timeout = null): ResponseStreamInterface - { - return $this->client->stream($responses, $timeout); - } - /** * Appends the 'auth_bearer' option with the retrieved access token from Auth0. */ @@ -92,6 +65,10 @@ private function getAccessTokenFromCache(): ?AccessToken // Replace invalid cache key characters with an underscore. $cacheKey = preg_replace('#[\{\}\(\)\/\\\@:]+#', '_', $this->authZeroConfiguration->getAudience()); + if (!$this->accessTokenCache instanceof CacheInterface) { + return null; + } + return $this->accessTokenCache->get( $cacheKey, function (ItemInterface $item) { diff --git a/src/AuthZeroConfiguration.php b/src/AuthZeroConfiguration.php index 6cf924e..2f6f35f 100755 --- a/src/AuthZeroConfiguration.php +++ b/src/AuthZeroConfiguration.php @@ -8,28 +8,8 @@ * * @author Niels Nijens */ -class AuthZeroConfiguration +readonly class AuthZeroConfiguration { - /** - * @var string - */ - private $tenantUri; - - /** - * @var string - */ - private $clientId; - - /** - * @var string - */ - private $clientSecret; - - /** - * @var string - */ - private $audience; - /** * Constructs a new AuthZeroConfiguration instance. * @@ -38,12 +18,12 @@ class AuthZeroConfiguration * @param string $clientSecret Your application's Client Secret * @param string $audience The unique identifier of the target API you want to access */ - public function __construct(string $tenantUri, string $clientId, string $clientSecret, string $audience) - { - $this->tenantUri = $tenantUri; - $this->clientId = $clientId; - $this->clientSecret = $clientSecret; - $this->audience = $audience; + public function __construct( + private string $tenantUri, + private string $clientId, + private string $clientSecret, + private string $audience + ) { } public function getAudience(): string diff --git a/tests/AuthZeroAuthenticatingHttpClientTest.php b/tests/AuthZeroAuthenticatingHttpClientTest.php index 36c6a5d..ceccde7 100755 --- a/tests/AuthZeroAuthenticatingHttpClientTest.php +++ b/tests/AuthZeroAuthenticatingHttpClientTest.php @@ -2,7 +2,6 @@ namespace Superbrave\AuthZeroHttpClient\Tests; -use ArrayIterator; use PHPUnit\Framework\TestCase; use Superbrave\AuthZeroHttpClient\AuthZeroAuthenticatingHttpClient; use Superbrave\AuthZeroHttpClient\AuthZeroConfiguration; @@ -44,7 +43,7 @@ class AuthZeroAuthenticatingHttpClientTest extends TestCase */ protected function setUp(): void { - $this->mockResponses = new ArrayIterator(); + $this->mockResponses = new \ArrayIterator(); $this->mockHttpClient = new MockHttpClient($this->mockResponses); $this->authZeroConfiguration = new AuthZeroConfiguration(