Skip to content

Commit

Permalink
Add binary responder, including getCovertArt method
Browse files Browse the repository at this point in the history
  • Loading branch information
usox committed Apr 23, 2022
1 parent 833371e commit 7dea13b
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 23 deletions.
16 changes: 16 additions & 0 deletions src/FeatureSet/V1161/Contract/GetCoverArtDataProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Usox\HyperSonic\FeatureSet\V1161\Contract;

interface GetCoverArtDataProviderInterface extends V1161DataProviderInterface
{
/**
* @return array{
* contentType: string,
* art: string
* }
*/
public function getArt(string $coverArtId): array;
}
8 changes: 8 additions & 0 deletions src/FeatureSet/V1161/FeatureSetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function getMethods(): array
'ping.view' => fn (): Method\V1161MethodInterface => $this->createPingMethod(),
'getLicense.view' => fn (): Method\V1161MethodInterface => $this->createGetLicense(),
'getArtists.view' => fn (): Method\V1161MethodInterface => $this->createGetArtistsMethod(),
'getCoverArt.view' => fn (): Method\V1161MethodInterface => $this->createGetCoverArtMethod(),
];
}

Expand All @@ -45,4 +46,11 @@ public function createGetLicense(): Method\V1161MethodInterface
new ResponderFactory(),
);
}

public function createGetCoverArtMethod(): Method\V1161MethodInterface
{
return new Method\GetCoverArtMethod(
new ResponderFactory()
);
}
}
2 changes: 2 additions & 0 deletions src/FeatureSet/V1161/FeatureSetFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public function createPingMethod(): Method\V1161MethodInterface;
public function createGetArtistsMethod(): Method\V1161MethodInterface;

public function createGetLicense(): Method\V1161MethodInterface;

public function createGetCoverArtMethod(): Method\V1161MethodInterface;
}
4 changes: 3 additions & 1 deletion src/FeatureSet/V1161/Method/GetArtistsMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public function __construct(
}

/**
* @param array<string, scalar> $queryParams
* @param array<string, scalar> $args
*/
public function __invoke(
ResponseWriterInterface $responseWriter,
ArtistListDataProviderInterface $artistListDataProvider,
array $args
array $queryParams,
array $args,
): ResponderInterface {
$data = [];
$currentIndex = null;
Expand Down
38 changes: 38 additions & 0 deletions src/FeatureSet/V1161/Method/GetCoverArtMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Usox\HyperSonic\FeatureSet\V1161\Method;

use Usox\HyperSonic\FeatureSet\V1161\Contract\GetCoverArtDataProviderInterface;
use Usox\HyperSonic\FeatureSet\V1161\Responder\ResponderFactoryInterface;
use Usox\HyperSonic\Response\ResponderInterface;
use Usox\HyperSonic\Response\ResponseWriterInterface;

final class GetCoverArtMethod implements V1161MethodInterface
{
public function __construct(
private readonly ResponderFactoryInterface $responderFactory,
) {
}

/**
* @param array<string, scalar> $queryParams
* @param array<string, scalar> $args
*/
public function __invoke(
ResponseWriterInterface $responseWriter,
GetCoverArtDataProviderInterface $getCoverArtDataProvider,
array $queryParams,
array $args
): ResponderInterface {
$art = $getCoverArtDataProvider->getArt(
(string) ($queryParams['id'] ?? '')
);

return $this->responderFactory->createCoverArtResponder(
$art['art'],
$art['contentType'],
);
}
}
2 changes: 2 additions & 0 deletions src/FeatureSet/V1161/Method/GetLicenseMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public function __construct(
}

/**
* @param array<string, scalar> $queryParams
* @param array<string, scalar> $args
*/
public function __invoke(
ResponseWriterInterface $responseWriter,
LicenseDataProviderInterface $licenseDataProvider,
array $queryParams,
array $args
): ResponderInterface {
return $this->responderFactory->createLicenseResponder([
Expand Down
2 changes: 2 additions & 0 deletions src/FeatureSet/V1161/Method/PingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public function __construct(
}

/**
* @param array<string, scalar> $queryParams
* @param array<string, scalar> $args
*/
public function __invoke(
ResponseWriterInterface $responseWriter,
PingDataProviderInterface $dataProvider,
array $queryParams,
array $args
): ResponderInterface {
if (!$dataProvider->isOk()) {
Expand Down
9 changes: 7 additions & 2 deletions src/FeatureSet/V1161/Responder/ArtistsResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Usox\HyperSonic\FeatureSet\V1161\Responder;

use AaronDDM\XMLBuilder\XMLArray;
use Usox\HyperSonic\Response\ResponderInterface;
use Usox\HyperSonic\Response\FormattedResponderInterface;

final class ArtistsResponder implements ResponderInterface
final class ArtistsResponder implements FormattedResponderInterface
{
/**
* @param array{
Expand Down Expand Up @@ -59,4 +59,9 @@ public function writeJson(array &$root): void
{
$root['artists'] = $this->artistList;
}

public function isBinaryResponder(): bool
{
return false;
}
}
29 changes: 29 additions & 0 deletions src/FeatureSet/V1161/Responder/CoverArtResponder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Usox\HyperSonic\FeatureSet\V1161\Responder;

use Psr\Http\Message\ResponseInterface;
use Usox\HyperSonic\Response\BinaryResponderInterface;

final class CoverArtResponder implements BinaryResponderInterface
{
public function __construct(
private readonly string $covertArt,
private readonly string $contentType,
) {
}

public function isBinaryResponder(): bool
{
return true;
}

public function writeResponse(ResponseInterface $response): ResponseInterface
{
$response->getBody()->write($this->covertArt);

return $response->withHeader('Content-Type', $this->contentType);
}
}
9 changes: 7 additions & 2 deletions src/FeatureSet/V1161/Responder/LicenseResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Usox\HyperSonic\FeatureSet\V1161\Responder;

use AaronDDM\XMLBuilder\XMLArray;
use Usox\HyperSonic\Response\ResponderInterface;
use Usox\HyperSonic\Response\FormattedResponderInterface;

final class LicenseResponder implements ResponderInterface
final class LicenseResponder implements FormattedResponderInterface
{
/**
* @param array{
Expand All @@ -34,4 +34,9 @@ public function writeJson(array &$root): void
{
$root['license'] = $this->licenseData;
}

public function isBinaryResponder(): bool
{
return false;
}
}
9 changes: 7 additions & 2 deletions src/FeatureSet/V1161/Responder/PingResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Usox\HyperSonic\FeatureSet\V1161\Responder;

use AaronDDM\XMLBuilder\XMLArray;
use Usox\HyperSonic\Response\ResponderInterface;
use Usox\HyperSonic\Response\FormattedResponderInterface;

final class PingResponder implements ResponderInterface
final class PingResponder implements FormattedResponderInterface
{
public function writeXml(XMLArray $XMLArray): void
{
Expand All @@ -16,4 +16,9 @@ public function writeXml(XMLArray $XMLArray): void
public function writeJson(array &$root): void
{
}

public function isBinaryResponder(): bool
{
return false;
}
}
10 changes: 10 additions & 0 deletions src/FeatureSet/V1161/Responder/ResponderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ public function createPingResponder(): ResponderInterface
{
return new PingResponder();
}

public function createCoverArtResponder(
string $coverArt,
string $contentType,
): ResponderInterface {
return new CoverArtResponder(
$coverArt,
$contentType,
);
}
}
6 changes: 5 additions & 1 deletion src/FeatureSet/V1161/Responder/ResponderFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Usox\HyperSonic\FeatureSet\V1161\Responder;

use DateTimeInterface;
use Usox\HyperSonic\Response\ResponderInterface;

interface ResponderFactoryInterface
Expand Down Expand Up @@ -39,4 +38,9 @@ public function createLicenseResponder(
): ResponderInterface;

public function createPingResponder(): ResponderInterface;

public function createCoverArtResponder(
string $coverArt,
string $contentType,
): ResponderInterface;
}
17 changes: 13 additions & 4 deletions src/HyperSonic.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Usox\HyperSonic\Authentication\Exception\AbstractAuthenticationException;
use Usox\HyperSonic\Exception\ErrorCodeEnum;
use Usox\HyperSonic\FeatureSet\V1161\FeatureSetFactoryInterface;
use Usox\HyperSonic\Response\BinaryResponderInterface;
use Usox\HyperSonic\Response\FormattedResponderInterface;
use Usox\HyperSonic\Response\ResponderInterface;
use Usox\HyperSonic\Response\ResponseWriterFactory;
use Usox\HyperSonic\Response\ResponseWriterFactoryInterface;
Expand Down Expand Up @@ -48,8 +50,10 @@ public function run(
ResponseInterface $response,
array $args
): ResponseInterface {
$queryParams = $request->getQueryParams();

// subsonic response format
$responseFormat = $request->getQueryParams()['f'] ?? 'xml';
$responseFormat = $queryParams['f'] ?? 'xml';

if ($responseFormat === 'xml') {
$responseWriter = $this->responseWriterFactory->createXmlResponseWriter(
Expand Down Expand Up @@ -93,9 +97,14 @@ public function run(

// execute handler method
/** @var ResponderInterface $responder */
$responder = call_user_func($method, $responseWriter, $dataProvider, $args);

$response = $responseWriter->write($response, $responder);
$responder = call_user_func($method, $responseWriter, $dataProvider, $queryParams, $args);
if ($responder->isBinaryResponder()) {
/** @var BinaryResponderInterface $responder */
$response = $responder->writeResponse($response);
} else {
/** @var FormattedResponderInterface $responder */
$response = $responseWriter->write($response, $responder);
}
} else {
$response = $responseWriter->writeError($response, ErrorCodeEnum::SERVER_VERSION_INCOMPATIBLE);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Response/BinaryResponderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Usox\HyperSonic\Response;

use Psr\Http\Message\ResponseInterface;

interface BinaryResponderInterface extends ResponderInterface
{
public function writeResponse(ResponseInterface $response): ResponseInterface;
}
17 changes: 17 additions & 0 deletions src/Response/FormattedResponderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Usox\HyperSonic\Response;

use AaronDDM\XMLBuilder\XMLArray;

interface FormattedResponderInterface extends ResponderInterface
{
public function writeXml(XMLArray $XMLArray): void;

/**
* @param array<mixed, mixed> $root
*/
public function writeJson(array &$root): void;
}
2 changes: 1 addition & 1 deletion src/Response/JsonResponseWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(

public function write(
ResponseInterface $response,
ResponderInterface $responder
FormattedResponderInterface $responder
): ResponseInterface {
$root = [
'status' => 'ok',
Expand Down
9 changes: 1 addition & 8 deletions src/Response/ResponderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@

namespace Usox\HyperSonic\Response;

use AaronDDM\XMLBuilder\XMLArray;

interface ResponderInterface
{
public function writeXml(XMLArray $XMLArray): void;

/**
* @param array<mixed, mixed> $root
*/
public function writeJson(array &$root): void;
public function isBinaryResponder(): bool;
}
2 changes: 1 addition & 1 deletion src/Response/ResponseWriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ResponseWriterInterface
{
public function write(
ResponseInterface $response,
ResponderInterface $responder,
FormattedResponderInterface $responder,
): ResponseInterface;

public function writeError(
Expand Down
2 changes: 1 addition & 1 deletion src/Response/XmlResponseWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(

public function write(
ResponseInterface $response,
ResponderInterface $responder,
FormattedResponderInterface $responder,
): ResponseInterface {
$rootNode = $this->getRootNode();

Expand Down

0 comments on commit 7dea13b

Please sign in to comment.