From 9bdb998f6d4f489418a588c94f8e56c7bb0e3e32 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Tue, 13 Sep 2022 09:09:43 +0200 Subject: [PATCH] Use decorator pattern for RequestFactory (#54) The implementation of the TYPO3 RequestFactory and Uri classes is internal API. It is therefore better to decorate the PSR-17 interfaces instead of extending or using the TYPO3 implementation directly. --- Classes/Domain/Import/RequestFactory.php | 32 +++++++++++++++---- .../Unit/Domain/Import/RequestFactoryTest.php | 26 ++++++++++++--- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/Classes/Domain/Import/RequestFactory.php b/Classes/Domain/Import/RequestFactory.php index 533fb6e..2e9302d 100644 --- a/Classes/Domain/Import/RequestFactory.php +++ b/Classes/Domain/Import/RequestFactory.php @@ -23,28 +23,48 @@ declare(strict_types=1); namespace WerkraumMedia\ThueCat\Domain\Import; +use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\UriFactoryInterface; +use Psr\Http\Message\UriInterface; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; -use TYPO3\CMS\Core\Http\RequestFactory as Typo3RequestFactory; -use TYPO3\CMS\Core\Http\Uri; -class RequestFactory extends Typo3RequestFactory +class RequestFactory implements RequestFactoryInterface { /** * @var ExtensionConfiguration */ private $extensionConfiguration; + /** + * @var RequestFactoryInterface + */ + private $requestFactory; + + /** + * @var UriFactoryInterface + */ + private $uriFactory; + public function __construct( - ExtensionConfiguration $extensionConfiguration + ExtensionConfiguration $extensionConfiguration, + RequestFactoryInterface $requestFactory, + UriFactoryInterface $uriFactory ) { $this->extensionConfiguration = $extensionConfiguration; + $this->requestFactory = $requestFactory; + $this->uriFactory = $uriFactory; } + /** + * @param UriInterface|string $uri The URI associated with the request. + */ public function createRequest(string $method, $uri): RequestInterface { - $uri = new Uri((string) $uri); + if (!$uri instanceof UriInterface) { + $uri = $this->uriFactory->createUri((string) $uri); + } $query = []; parse_str($uri->getQuery(), $query); @@ -60,6 +80,6 @@ class RequestFactory extends Typo3RequestFactory $uri = $uri->withQuery(http_build_query($query)); - return parent::createRequest($method, $uri); + return $this->requestFactory->createRequest($method, $uri); } } diff --git a/Tests/Unit/Domain/Import/RequestFactoryTest.php b/Tests/Unit/Domain/Import/RequestFactoryTest.php index 47140b1..73dae8e 100644 --- a/Tests/Unit/Domain/Import/RequestFactoryTest.php +++ b/Tests/Unit/Domain/Import/RequestFactoryTest.php @@ -26,6 +26,8 @@ namespace WerkraumMedia\ThueCat\Tests\Unit\Domain\Import; use PHPUnit\Framework\TestCase; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; +use TYPO3\CMS\Core\Http\RequestFactory as Typo3RequestFactory; +use TYPO3\CMS\Core\Http\UriFactory; use WerkraumMedia\ThueCat\Domain\Import\RequestFactory; /** @@ -39,9 +41,13 @@ class RequestFactoryTest extends TestCase public function canBeCreated(): void { $extensionConfiguration = $this->createStub(ExtensionConfiguration::class); + $requestFactory = new Typo3RequestFactory(); + $uriFactory = new UriFactory(); $subject = new RequestFactory( - $extensionConfiguration + $extensionConfiguration, + $requestFactory, + $uriFactory ); self::assertInstanceOf(RequestFactory::class, $subject); @@ -53,9 +59,13 @@ class RequestFactoryTest extends TestCase public function returnsRequestWithJsonIdFormat(): void { $extensionConfiguration = $this->createStub(ExtensionConfiguration::class); + $requestFactory = new Typo3RequestFactory(); + $uriFactory = new UriFactory(); $subject = new RequestFactory( - $extensionConfiguration + $extensionConfiguration, + $requestFactory, + $uriFactory ); $request = $subject->createRequest('GET', 'https://example.com/api/ext-sync/get-updated-nodes?syncScopeId=dd3738dc-58a6-4748-a6ce-4950293a06db'); @@ -70,9 +80,13 @@ class RequestFactoryTest extends TestCase { $extensionConfiguration = $this->createStub(ExtensionConfiguration::class); $extensionConfiguration->method('get')->willReturn('some-api-key'); + $requestFactory = new Typo3RequestFactory(); + $uriFactory = new UriFactory(); $subject = new RequestFactory( - $extensionConfiguration + $extensionConfiguration, + $requestFactory, + $uriFactory ); $request = $subject->createRequest('GET', 'https://example.com/api/ext-sync/get-updated-nodes?syncScopeId=dd3738dc-58a6-4748-a6ce-4950293a06db'); @@ -87,9 +101,13 @@ class RequestFactoryTest extends TestCase { $extensionConfiguration = $this->createStub(ExtensionConfiguration::class); $extensionConfiguration->method('get')->willThrowException(new ExtensionConfigurationExtensionNotConfiguredException()); + $requestFactory = new Typo3RequestFactory(); + $uriFactory = new UriFactory(); $subject = new RequestFactory( - $extensionConfiguration + $extensionConfiguration, + $requestFactory, + $uriFactory ); $request = $subject->createRequest('GET', 'https://example.com/api/ext-sync/get-updated-nodes?syncScopeId=dd3738dc-58a6-4748-a6ce-4950293a06db');