Skip to content

Commit

Permalink
Add support for getMusicFolders method
Browse files Browse the repository at this point in the history
  • Loading branch information
usox committed Jul 4, 2022
1 parent 8c3aba7 commit bef14c0
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Please see the official [subsonic api documentation](http://www.subsonic.org/pag
- getCoverArt.view
- getGenres.view
- getLicense.view
- getMusicFolders.view
- ping.view


Expand Down
31 changes: 16 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Usox\HyperSonic\FeatureSet\V1161\Contract;

use Traversable;

interface MusicFolderListDataProviderInterface
{
/**
* @return Traversable<array{name: string, id: string}>
*/
public function getMusicFolders(): Traversable;
}
8 changes: 8 additions & 0 deletions src/FeatureSet/V1161/FeatureSetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getMethods(): array
'getCoverArt.view' => fn (): Method\V1161MethodInterface => $this->createGetCoverArtMethod(),
'getArtist.view' => fn (): Method\V1161MethodInterface => $this->createGetArtistMethod(),
'getGenres.view' => fn (): Method\V1161MethodInterface => $this->createGetGenresMethod(),
'getMusicFolders.view' => fn (): Method\V1161MethodInterface => $this->createGetMusicFoldersMethod(),
];
}

Expand Down Expand Up @@ -72,4 +73,11 @@ public function createGetGenresMethod(): Method\V1161MethodInterface
new ResponderFactory()
);
}

public function createGetMusicFoldersMethod(): Method\V1161MethodInterface
{
return new Method\GetMusicFoldersMethod(
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 @@ -24,4 +24,6 @@ public function createGetCoverArtMethod(): Method\V1161MethodInterface;
public function createGetArtistMethod(): Method\V1161MethodInterface;

public function createGetGenresMethod(): Method\V1161MethodInterface;

public function createGetMusicFoldersMethod(): Method\V1161MethodInterface;
}
39 changes: 39 additions & 0 deletions src/FeatureSet/V1161/Method/GetMusicFoldersMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Usox\HyperSonic\FeatureSet\V1161\Method;

use Usox\HyperSonic\Exception\SubSonicApiException;
use Usox\HyperSonic\FeatureSet\V1161\Contract\MusicFolderListDataProviderInterface;
use Usox\HyperSonic\FeatureSet\V1161\Responder\ResponderFactoryInterface;
use Usox\HyperSonic\Response\ResponderInterface;

/**
* Retrieves the list of available music folders
*
* This class covers the `getMusicFolders.view` method
*/
final class GetMusicFoldersMethod implements V1161MethodInterface
{
public function __construct(
private readonly ResponderFactoryInterface $responderFactory,
) {
}

/**
* @param array<string, scalar> $queryParams
* @param array<string, scalar> $args
*
* @throws SubSonicApiException
*/
public function __invoke(
MusicFolderListDataProviderInterface $dataProvider,
array $queryParams,
array $args
): ResponderInterface {
return $this->responderFactory->createMusicFoldersResponder(
$dataProvider->getMusicFolders()
);
}
}
53 changes: 53 additions & 0 deletions src/FeatureSet/V1161/Responder/MusicFoldersResponder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Usox\HyperSonic\FeatureSet\V1161\Responder;

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

final class MusicFoldersResponder implements FormattedResponderInterface
{
/**
* @param Traversable<array{
* name: string,
* id: string
* }> $musicFolders
*/
public function __construct(
private readonly Traversable $musicFolders
) {
}

public function writeXml(XMLArray $XMLArray): void
{
$XMLArray->startLoop(
'musicFolders',
[],
function (XMLArray $XMLArray): void {
foreach ($this->musicFolders as $musicFolder) {
$XMLArray->add(
'musicFolder',
'',
[
'name' => htmlspecialchars($musicFolder['name']),
'id' => $musicFolder['id'],
]
);
}
}
);
}

public function writeJson(array &$root): void
{
$root['musicFolders'] = ['musicFolder' => iterator_to_array($this->musicFolders)];
}

public function isBinaryResponder(): bool
{
return false;
}
}
15 changes: 15 additions & 0 deletions src/FeatureSet/V1161/Responder/ResponderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Usox\HyperSonic\FeatureSet\V1161\Responder;

use Traversable;
use Usox\HyperSonic\Response\ResponderInterface;

final class ResponderFactory implements ResponderFactoryInterface
Expand Down Expand Up @@ -105,4 +106,18 @@ public function createGenresResponder(
$genres
);
}

/**
* @param Traversable<array{
* name: string,
* id: string
* }> $musicFolders
*/
public function createMusicFoldersResponder(
Traversable $musicFolders
): ResponderInterface {
return new MusicFoldersResponder(
$musicFolders
);
}
}
11 changes: 11 additions & 0 deletions src/FeatureSet/V1161/Responder/ResponderFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Usox\HyperSonic\FeatureSet\V1161\Responder;

use Traversable;
use Usox\HyperSonic\Response\ResponderInterface;

interface ResponderFactoryInterface
Expand Down Expand Up @@ -81,4 +82,14 @@ public function createArtistResponder(
public function createGenresResponder(
array $genres,
): ResponderInterface;

/**
* @param Traversable<array{
* name: string,
* id: string
* }> $musicFolders
*/
public function createMusicFoldersResponder(
Traversable $musicFolders
): ResponderInterface;
}
1 change: 1 addition & 0 deletions tests/FeatureSet/V1161/FeatureSetFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function methodDataProvider(): array
['getCoverArt.view', Method\GetCoverArtMethod::class],
['getArtist.view', Method\GetArtistMethod::class],
['getGenres.view', Method\GetGenresMethod::class],
['getMusicFolders.view', Method\GetMusicFoldersMethod::class],
];
}

Expand Down
52 changes: 52 additions & 0 deletions tests/FeatureSet/V1161/Method/GetMusicFoldersMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Usox\HyperSonic\FeatureSet\V1161\Method;

use ArrayIterator;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\MockInterface;
use Usox\HyperSonic\FeatureSet\V1161\Contract\MusicFolderListDataProviderInterface;
use Usox\HyperSonic\FeatureSet\V1161\Responder\ResponderFactoryInterface;
use Usox\HyperSonic\Response\ResponderInterface;

class GetMusicFoldersMethodTest extends MockeryTestCase
{
private MockInterface $responder_factory;

private GetMusicFoldersMethod $subject;

public function setUp(): void
{
$this->responder_factory = Mockery::mock(ResponderFactoryInterface::class);

$this->subject = new GetMusicFoldersMethod(
$this->responder_factory,
);
}

public function testInvokeReturnsResponder(): void
{
$dataProvider = Mockery::mock(MusicFolderListDataProviderInterface::class);
$responder = Mockery::mock(ResponderInterface::class);

$value = new ArrayIterator([666]);

$dataProvider->shouldReceive('getMusicFolders')
->withNoArgs()
->once()
->andReturn($value);

$this->responder_factory->shouldReceive('createMusicFoldersResponder')
->with($value)
->once()
->andReturn($responder);

$this->assertSame(
$responder,
call_user_func($this->subject, $dataProvider, [], [])
);
}
}
Loading

0 comments on commit bef14c0

Please sign in to comment.