Skip to content

Commit

Permalink
Merge pull request #14 from superbrave/DV-6720-post-eventsauce-upgrade
Browse files Browse the repository at this point in the history
[DV-6720] Upgraded client to PHP ^8.2 and updated related symfony packages/contracts
  • Loading branch information
atehvg authored Jan 3, 2024
2 parents 51fae57 + e44d797 commit dd61632
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 106 deletions.
6 changes: 4 additions & 2 deletions .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

use PhpCsFixer\Config;

$finder = PhpCsFixer\Finder::create()
->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);
4 changes: 2 additions & 2 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
30 changes: 14 additions & 16 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
<?xml version='1.0' encoding='UTF-8'?>
<phpunit
backupGlobals='false'
backupStaticAttributes='false'
colors='true'
convertErrorsToExceptions='true'
convertNoticesToExceptions='true'
convertWarningsToExceptions='true'
processIsolation='false'
stopOnFailure='false'>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false">

<testsuites>
<testsuite name='auth0-http-client'>
<testsuite name="auth0-http-client">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<source>
<include>
<directory>./src/</directory>
</whitelist>
</filter>
</include>
</source>
</phpunit>
19 changes: 2 additions & 17 deletions src/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,10 @@
*
* @author Niels Nijens <nn@superbrave.nl>
*/
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
Expand Down
45 changes: 11 additions & 34 deletions src/AuthZeroAuthenticatingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -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.
*/
Expand All @@ -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) {
Expand Down
34 changes: 7 additions & 27 deletions src/AuthZeroConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,8 @@
*
* @author Niels Nijens <nn@superbrave.nl>
*/
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.
*
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/AuthZeroAuthenticatingHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Superbrave\AuthZeroHttpClient\Tests;

use ArrayIterator;
use PHPUnit\Framework\TestCase;
use Superbrave\AuthZeroHttpClient\AuthZeroAuthenticatingHttpClient;
use Superbrave\AuthZeroHttpClient\AuthZeroConfiguration;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit dd61632

Please sign in to comment.