From 833371e1b1d96651945f529f02fbe89ba01c218e Mon Sep 17 00:00:00 2001 From: Daniel Jakob Date: Sat, 23 Apr 2022 08:19:47 +0200 Subject: [PATCH] Add `getVersion` method to FeatureSetFactory Also apply some namespace corrections --- composer.json | 2 +- src/FeatureSet/FeatureSetInterface.php | 15 +++++++++++++++ src/FeatureSet/V1161/FeatureSetFactory.php | 5 +++++ .../V1161/FeatureSetFactoryInterface.php | 4 +++- src/FeatureSet/V1161/FeatureSetResponder.php | 9 --------- src/HyperSonic.php | 8 ++++++-- src/Response/JsonResponseWriter.php | 19 +++++++++++-------- src/Response/ResponseWriterFactory.php | 17 +++++++++++------ .../ResponseWriterFactoryInterface.php | 8 ++++++-- src/Response/XmlResponseWriter.php | 5 +++-- tests/HyperSonicTest.php | 11 ++++++++--- 11 files changed, 69 insertions(+), 34 deletions(-) create mode 100644 src/FeatureSet/FeatureSetInterface.php delete mode 100644 src/FeatureSet/V1161/FeatureSetResponder.php diff --git a/composer.json b/composer.json index cd53a99..e1f7bb3 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ }, "autoload-dev": { "psr-4": { - "Uxmp\\Core\\": ["tests/"] + "Usox\\HyperSonic\\": ["tests/"] } }, "authors": [ diff --git a/src/FeatureSet/FeatureSetInterface.php b/src/FeatureSet/FeatureSetInterface.php new file mode 100644 index 0000000..ba98d33 --- /dev/null +++ b/src/FeatureSet/FeatureSetInterface.php @@ -0,0 +1,15 @@ + + */ + public function getMethods(): array; +} diff --git a/src/FeatureSet/V1161/FeatureSetFactory.php b/src/FeatureSet/V1161/FeatureSetFactory.php index 13e801d..d35374c 100644 --- a/src/FeatureSet/V1161/FeatureSetFactory.php +++ b/src/FeatureSet/V1161/FeatureSetFactory.php @@ -8,6 +8,11 @@ final class FeatureSetFactory implements FeatureSetFactoryInterface { + public function getVersion(): string + { + return '1.16.1'; + } + /** * @return array */ diff --git a/src/FeatureSet/V1161/FeatureSetFactoryInterface.php b/src/FeatureSet/V1161/FeatureSetFactoryInterface.php index 1761e79..323cd97 100644 --- a/src/FeatureSet/V1161/FeatureSetFactoryInterface.php +++ b/src/FeatureSet/V1161/FeatureSetFactoryInterface.php @@ -4,7 +4,9 @@ namespace Usox\HyperSonic\FeatureSet\V1161; -interface FeatureSetFactoryInterface +use Usox\HyperSonic\FeatureSet\FeatureSetInterface; + +interface FeatureSetFactoryInterface extends FeatureSetInterface { /** * @return array diff --git a/src/FeatureSet/V1161/FeatureSetResponder.php b/src/FeatureSet/V1161/FeatureSetResponder.php deleted file mode 100644 index 5bba280..0000000 --- a/src/FeatureSet/V1161/FeatureSetResponder.php +++ /dev/null @@ -1,9 +0,0 @@ -getQueryParams()['f'] ?? 'xml'; if ($responseFormat === 'xml') { - $responseWriter = $this->responseWriterFactory->createXmlResponseWriter(); + $responseWriter = $this->responseWriterFactory->createXmlResponseWriter( + $this->featureSetFactory->getVersion() + ); } else { - $responseWriter = $this->responseWriterFactory->createJsonResponseWriter(); + $responseWriter = $this->responseWriterFactory->createJsonResponseWriter( + $this->featureSetFactory->getVersion() + ); } try { diff --git a/src/Response/JsonResponseWriter.php b/src/Response/JsonResponseWriter.php index 7c09377..dc49900 100644 --- a/src/Response/JsonResponseWriter.php +++ b/src/Response/JsonResponseWriter.php @@ -9,20 +9,23 @@ final class JsonResponseWriter implements ResponseWriterInterface { - /** @var array{status: string, version: string} */ - private array $root = [ - 'status' => 'ok', - 'version' => '1.16.1', - ]; + public function __construct( + private readonly string $apiVersion + ) { + } public function write( ResponseInterface $response, ResponderInterface $responder ): ResponseInterface { - $responder->writeJson($this->root); + $root = [ + 'status' => 'ok', + 'version' => $this->apiVersion, + ]; + $responder->writeJson($root); $response->getBody()->write( - (string) json_encode(['subsonic-response' => $this->root], JSON_PRETTY_PRINT) + (string) json_encode(['subsonic-response' => $root], JSON_PRETTY_PRINT) ); return $response->withHeader('Content-Type', 'application/json'); @@ -36,7 +39,7 @@ public function writeError( $data = [ 'subsonic-response' => [ 'status' => 'failed', - 'version' => '1.16.1', + 'version' => $this->apiVersion, 'error' => [ 'code' => $errorCode->value, 'message' => $message, diff --git a/src/Response/ResponseWriterFactory.php b/src/Response/ResponseWriterFactory.php index df4b681..3386ebc 100644 --- a/src/Response/ResponseWriterFactory.php +++ b/src/Response/ResponseWriterFactory.php @@ -8,15 +8,20 @@ final class ResponseWriterFactory implements ResponseWriterFactoryInterface { - public function createXmlResponseWriter(): ResponseWriterInterface - { + public function createXmlResponseWriter( + string $apiVersion + ): ResponseWriterInterface { return new XmlResponseWriter( - new XMLWriterService() + new XMLWriterService(), + $apiVersion, ); } - public function createJsonResponseWriter(): ResponseWriterInterface - { - return new JsonResponseWriter(); + public function createJsonResponseWriter( + string $apiVersion + ): ResponseWriterInterface { + return new JsonResponseWriter( + $apiVersion + ); } } diff --git a/src/Response/ResponseWriterFactoryInterface.php b/src/Response/ResponseWriterFactoryInterface.php index 4a7cb75..6389301 100644 --- a/src/Response/ResponseWriterFactoryInterface.php +++ b/src/Response/ResponseWriterFactoryInterface.php @@ -6,7 +6,11 @@ interface ResponseWriterFactoryInterface { - public function createXmlResponseWriter(): ResponseWriterInterface; + public function createXmlResponseWriter( + string $apiVersion + ): ResponseWriterInterface; - public function createJsonResponseWriter(): ResponseWriterInterface; + public function createJsonResponseWriter( + string $apiVersion + ): ResponseWriterInterface; } diff --git a/src/Response/XmlResponseWriter.php b/src/Response/XmlResponseWriter.php index 0f9a367..d8255d9 100644 --- a/src/Response/XmlResponseWriter.php +++ b/src/Response/XmlResponseWriter.php @@ -18,6 +18,7 @@ final class XmlResponseWriter implements ResponseWriterInterface public function __construct( private readonly XMLWriterService $XMLWriterService, + private readonly string $apiVersion, ) { $this->XMLBuilder = new XMLBuilder($this->XMLWriterService); } @@ -47,7 +48,7 @@ public function writeError(ResponseInterface $response, ErrorCodeEnum $errorCode [ 'xmlns' => 'http://subsonic.org/restapi', 'status' => 'failed', - 'version' => '1.16.1', + 'version' => $this->apiVersion, ] ) ->add( @@ -73,7 +74,7 @@ private function getRootNode(): XMLArray [ 'xmlns' => 'http://subsonic.org/restapi', 'status' => 'ok', - 'version' => '1.16.1', + 'version' => $this->apiVersion, ] ); } diff --git a/tests/HyperSonicTest.php b/tests/HyperSonicTest.php index a33bd04..623ad4f 100644 --- a/tests/HyperSonicTest.php +++ b/tests/HyperSonicTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Uxmp\Core; +namespace Usox\HyperSonic; use Mockery; use Mockery\Adapter\Phpunit\MockeryTestCase; @@ -13,7 +13,6 @@ use Usox\HyperSonic\Authentication\Exception\AuthenticationFailedException; use Usox\HyperSonic\Exception\ErrorCodeEnum; use Usox\HyperSonic\FeatureSet\V1161\FeatureSetFactoryInterface; -use Usox\HyperSonic\HyperSonic; use Usox\HyperSonic\Response\ResponseWriterFactoryInterface; use Usox\HyperSonic\Response\ResponseWriterInterface; @@ -51,6 +50,12 @@ public function testInvokeErrorsOnAuthError(): void $responseWriter = Mockery::mock(ResponseWriterInterface::class); $errorMessage = 'some-error'; + $apiVersion = 'some-version'; + + $this->featureSetFactory->shouldReceive('getVersion') + ->withNoArgs() + ->once() + ->andReturn($apiVersion); $request->shouldReceive('getQueryParams') ->withNoArgs() @@ -58,7 +63,7 @@ public function testInvokeErrorsOnAuthError(): void ->andReturn([]); $this->responseWriterFactory->shouldReceive('createXmlResponseWriter') - ->withNoArgs() + ->with($apiVersion) ->once() ->andReturn($responseWriter);