Skip to content

Commit

Permalink
Add getVersion method to FeatureSetFactory
Browse files Browse the repository at this point in the history
Also apply some namespace corrections
  • Loading branch information
usox committed Apr 23, 2022
1 parent 6028738 commit 833371e
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 34 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"autoload-dev": {
"psr-4": {
"Uxmp\\Core\\": ["tests/"]
"Usox\\HyperSonic\\": ["tests/"]
}
},
"authors": [
Expand Down
15 changes: 15 additions & 0 deletions src/FeatureSet/FeatureSetInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Usox\HyperSonic\FeatureSet;

interface FeatureSetInterface
{
public function getVersion(): string;

/**
* @return array<string, callable(): object>
*/
public function getMethods(): array;
}
5 changes: 5 additions & 0 deletions src/FeatureSet/V1161/FeatureSetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

final class FeatureSetFactory implements FeatureSetFactoryInterface
{
public function getVersion(): string
{
return '1.16.1';
}

/**
* @return array<string, callable(): Method\V1161MethodInterface>
*/
Expand Down
4 changes: 3 additions & 1 deletion src/FeatureSet/V1161/FeatureSetFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Usox\HyperSonic\FeatureSet\V1161;

interface FeatureSetFactoryInterface
use Usox\HyperSonic\FeatureSet\FeatureSetInterface;

interface FeatureSetFactoryInterface extends FeatureSetInterface
{
/**
* @return array<string, callable(): Method\V1161MethodInterface>
Expand Down
9 changes: 0 additions & 9 deletions src/FeatureSet/V1161/FeatureSetResponder.php

This file was deleted.

8 changes: 6 additions & 2 deletions src/HyperSonic.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ public function run(
$responseFormat = $request->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 {
Expand Down
19 changes: 11 additions & 8 deletions src/Response/JsonResponseWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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,
Expand Down
17 changes: 11 additions & 6 deletions src/Response/ResponseWriterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
8 changes: 6 additions & 2 deletions src/Response/ResponseWriterFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 3 additions & 2 deletions src/Response/XmlResponseWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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(
Expand All @@ -73,7 +74,7 @@ private function getRootNode(): XMLArray
[
'xmlns' => 'http://subsonic.org/restapi',
'status' => 'ok',
'version' => '1.16.1',
'version' => $this->apiVersion,
]
);
}
Expand Down
11 changes: 8 additions & 3 deletions tests/HyperSonicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Uxmp\Core;
namespace Usox\HyperSonic;

use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
Expand All @@ -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;

Expand Down Expand Up @@ -51,14 +50,20 @@ 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()
->once()
->andReturn([]);

$this->responseWriterFactory->shouldReceive('createXmlResponseWriter')
->withNoArgs()
->with($apiVersion)
->once()
->andReturn($responseWriter);

Expand Down

0 comments on commit 833371e

Please sign in to comment.