Passed
Push — main ( e933de...5ef56a )
by Daniel
03:35
created

AlbumListDataProvider::getAlbums()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 9.7998
cc 3
nc 4
nop 5
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\SubSonic;
6
7
use DateTimeInterface;
8
use Traversable;
9
use Usox\HyperSonic\FeatureSet\V1161\Contract\AlbumList2DataProviderInterface;
10
use Uxmp\Core\Component\SubSonic\AlbumList\AlbumListRetrieverInterface;
11
12
/**
13
 * Provides custom ordered album lists for the subsonic api
14
 */
15
final class AlbumListDataProvider implements AlbumList2DataProviderInterface
16
{
17 1
    public function __construct(
18
        private readonly AlbumListRetrieverInterface $albumListRetriever,
19
    ) {
20 1
    }
21
22
    /**
23
     * If $orderType is `byGenre` or `byYear`, the optional values from $orderParameter MUST be considered
24
     *
25
     * @param array{
26
     *  year?: array{from: int, to: int},
27
     *  genre?: string
28
     * } $orderParameter
29
     *
30
     * @return Traversable<array{
31
     *  id: string,
32
     *  name: string,
33
     *  coverArtId: string,
34
     *  songCount: int,
35
     *  createDate: DateTimeInterface,
36
     *  duration: int,
37
     *  artistName: string,
38
     *  artistId: string,
39
     * }>
40
     */
41 1
    public function getAlbums(
42
        string $orderType,
43
        int $limit,
44
        int $offset,
45
        array $orderParameter,
46
        ?string $musicFolderId
47
    ): Traversable {
48 1
        if ($musicFolderId !== null) {
49 1
            $musicFolderId = (int) $musicFolderId;
50
        }
51
52 1
        foreach ($this->albumListRetriever->retrieve($orderType, $limit, $offset, $orderParameter, $musicFolderId) as $album) {
53 1
            $artist = $album->getArtist();
54 1
            $albumId = (string) $album->getId();
55
56 1
            yield [
57 1
                'id' => $albumId,
58 1
                'name' => (string) $album->getTitle(),
59 1
                'coverArtId' => sprintf('album-%d', $albumId),
60 1
                'songCount' => $album->getSongCount(),
61 1
                'createDate' => $album->getLastModified(), // @todo add create date,
62 1
                'duration' => $album->getLength(),
63 1
                'artistName' => (string) $artist->getTitle(),
64 1
                'artistId' => (string) $artist->getId(),
65 1
            ];
66
        }
67
    }
68
}
69