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.
This commit is contained in:
Benjamin Franzke 2022-09-13 09:09:43 +02:00 committed by GitHub
parent 2e4c9cc04e
commit 9bdb998f6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 10 deletions

View file

@ -23,28 +23,48 @@ declare(strict_types=1);
namespace WerkraumMedia\ThueCat\Domain\Import; namespace WerkraumMedia\ThueCat\Domain\Import;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface; 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\Exception\ExtensionConfigurationExtensionNotConfiguredException;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; 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 * @var ExtensionConfiguration
*/ */
private $extensionConfiguration; private $extensionConfiguration;
/**
* @var RequestFactoryInterface
*/
private $requestFactory;
/**
* @var UriFactoryInterface
*/
private $uriFactory;
public function __construct( public function __construct(
ExtensionConfiguration $extensionConfiguration ExtensionConfiguration $extensionConfiguration,
RequestFactoryInterface $requestFactory,
UriFactoryInterface $uriFactory
) { ) {
$this->extensionConfiguration = $extensionConfiguration; $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 public function createRequest(string $method, $uri): RequestInterface
{ {
$uri = new Uri((string) $uri); if (!$uri instanceof UriInterface) {
$uri = $this->uriFactory->createUri((string) $uri);
}
$query = []; $query = [];
parse_str($uri->getQuery(), $query); parse_str($uri->getQuery(), $query);
@ -60,6 +80,6 @@ class RequestFactory extends Typo3RequestFactory
$uri = $uri->withQuery(http_build_query($query)); $uri = $uri->withQuery(http_build_query($query));
return parent::createRequest($method, $uri); return $this->requestFactory->createRequest($method, $uri);
} }
} }

View file

@ -26,6 +26,8 @@ namespace WerkraumMedia\ThueCat\Tests\Unit\Domain\Import;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; 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; use WerkraumMedia\ThueCat\Domain\Import\RequestFactory;
/** /**
@ -39,9 +41,13 @@ class RequestFactoryTest extends TestCase
public function canBeCreated(): void public function canBeCreated(): void
{ {
$extensionConfiguration = $this->createStub(ExtensionConfiguration::class); $extensionConfiguration = $this->createStub(ExtensionConfiguration::class);
$requestFactory = new Typo3RequestFactory();
$uriFactory = new UriFactory();
$subject = new RequestFactory( $subject = new RequestFactory(
$extensionConfiguration $extensionConfiguration,
$requestFactory,
$uriFactory
); );
self::assertInstanceOf(RequestFactory::class, $subject); self::assertInstanceOf(RequestFactory::class, $subject);
@ -53,9 +59,13 @@ class RequestFactoryTest extends TestCase
public function returnsRequestWithJsonIdFormat(): void public function returnsRequestWithJsonIdFormat(): void
{ {
$extensionConfiguration = $this->createStub(ExtensionConfiguration::class); $extensionConfiguration = $this->createStub(ExtensionConfiguration::class);
$requestFactory = new Typo3RequestFactory();
$uriFactory = new UriFactory();
$subject = new RequestFactory( $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'); $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 = $this->createStub(ExtensionConfiguration::class);
$extensionConfiguration->method('get')->willReturn('some-api-key'); $extensionConfiguration->method('get')->willReturn('some-api-key');
$requestFactory = new Typo3RequestFactory();
$uriFactory = new UriFactory();
$subject = new RequestFactory( $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'); $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 = $this->createStub(ExtensionConfiguration::class);
$extensionConfiguration->method('get')->willThrowException(new ExtensionConfigurationExtensionNotConfiguredException()); $extensionConfiguration->method('get')->willThrowException(new ExtensionConfigurationExtensionNotConfiguredException());
$requestFactory = new Typo3RequestFactory();
$uriFactory = new UriFactory();
$subject = new RequestFactory( $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'); $request = $subject->createRequest('GET', 'https://example.com/api/ext-sync/get-updated-nodes?syncScopeId=dd3738dc-58a6-4748-a6ce-4950293a06db');