Passed
Push — main ( 3d11c7...dcd309 )
by Daniel
14:22
created

GetStarred2DataProvider::getStarred()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\SubSonic;
6
7
use DateTime;
8
use DateTimeInterface;
9
use Generator;
10
use Usox\HyperSonic\FeatureSet\V1161\Contract\GetStarred2DataProviderInterface;
11
use Uxmp\Core\Orm\Model\CatalogInterface;
12
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface;
13
use Uxmp\Core\Orm\Repository\CatalogRepositoryInterface;
14
use Uxmp\Core\Orm\Repository\SongRepositoryInterface;
15
16
final class GetStarred2DataProvider implements GetStarred2DataProviderInterface
17
{
18 2
    public function __construct(
19
        private readonly AlbumRepositoryInterface $albumRepository,
20
        private readonly SongRepositoryInterface $songRepository,
21
        private readonly CatalogRepositoryInterface $catalogRepository,
22
        private readonly HypersonicAuthenticationProviderInterface $authenticationProvider,
23
    ) {
24
    }
25
26
    /**
27
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
28
     *  songs: Generator<array{
29
     *    id: int,
30
     *    name: string,
31
     *    artistName: string,
32
     *    artistId: int,
33
     *    albumName: string,
34
     *    albumId: int,
35
     *    coverArtId: string,
36
     *    length: int,
37
     *    createDate: DateTimeInterface,
38
     *    starredDate: DateTimeInterface,
39
     *    filesize: int
40
     *  }>,
41
     *  albums: Generator<array{
42
     *    id: int,
43
     *    name: string,
44
     *    artistName: string,
45
     *    coverArtId: string,
46
     *    artistId: int,
47
     *    length: int,
48
     *    createDate: DateTimeInterface,
49
     *    starredDate: DateTimeInterface,
50
     *    songCount: int,
51
     *    year: int
52
     *  }>
53
     * }
54
     */
55 2
    public function getStarred(?string $musicFolderId): array
56
    {
57 2
        $catalog = null;
58 2
        if ($musicFolderId !== null) {
59 1
            $catalog = $this->catalogRepository->find((int) $musicFolderId);
60
        }
61
62
        return [
63 2
            'songs' => $this->retrieveSongs($catalog),
64 2
            'albums' => $this->retrieveAlbums($catalog),
65
        ];
66
    }
67
68
    /**
69
     * @return Generator<array{
70
     *  id: int,
71
     *  name: string,
72
     *  artistName: string,
73
     *  artistId: int,
74
     *  albumName: string,
75
     *  albumId: int,
76
     *  coverArtId: string,
77
     *  length: int,
78
     *  createDate: DateTimeInterface,
79
     *  starredDate: DateTimeInterface,
80
     *  filesize: int
81
     * }>
82
     */
83 1
    private function retrieveSongs(?CatalogInterface $catalog): Generator
84
    {
85 1
        $songs = $this->songRepository->findFavorites(
86 1
            $this->authenticationProvider->getUser(),
87
            $catalog,
88
        );
89
90 1
        foreach ($songs as $song) {
91 1
            $artist = $song->getArtist();
92 1
            $album = $song->getAlbum();
93
94
            yield [
95 1
                'id' => $song->getId(),
96 1
                'name' => $song->getTitle(),
97 1
                'albumName' => (string) $album->getTitle(),
98 1
                'albumId' => $album->getId(),
99 1
                'artistName' => (string) $artist->getTitle(),
100 1
                'artistId' => $artist->getId(),
101 1
                'coverArtId' => sprintf('song-%d', $song->getId()),
102 1
                'length' => $song->getLength(),
103 1
                'createDate' => new DateTime(), // @todo add date
104 1
                'starredDate' => new DateTime(), // @todo add date,
105 1
                'filesize' => $song->getFileSize(),
106
            ];
107
        }
108
    }
109
110
    /**
111
     * @return Generator<array{
112
     *  id: int,
113
     *  name: string,
114
     *  artistName: string,
115
     *  coverArtId: string,
116
     *  artistId: int,
117
     *  length: int,
118
     *  createDate: DateTimeInterface,
119
     *  starredDate: DateTimeInterface,
120
     *  songCount: int,
121
     *  year: int
122
     * }>
123
     */
124 1
    private function retrieveAlbums(?CatalogInterface $catalog): Generator
125
    {
126 1
        $albums = $this->albumRepository->getFavorites(
127 1
            $this->authenticationProvider->getUser(),
128
            $catalog,
129
        );
130
131 1
        foreach ($albums as $album) {
132 1
            $artist = $album->getArtist();
133
134
            yield [
135 1
                'id' => $album->getId(),
136 1
                'name' => (string) $album->getTitle(),
137 1
                'artistName' => (string) $artist->getTitle(),
138 1
                'artistId' => $artist->getId(),
139 1
                'songCount' => $album->getSongCount(),
140 1
                'length' => $album->getLength(),
141 1
                'coverArtId' => sprintf('album-%d', $album->getId()),
142 1
                'createDate' => $album->getLastModified(), // @todo use create date
143 1
                'starredDate' => new DateTime(),
144 1
                'year' => (int) $album->getYear(),
145
            ];
146
        }
147
    }
148
}
149