GetStarred2Method::buildAlbumsList()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
ccs 13
cts 13
cp 1
crap 2
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Usox\HyperSonic\FeatureSet\V1161\Method;
6
7
use DateTimeInterface;
8
use Generator;
9
use Traversable;
10
use Usox\HyperSonic\FeatureSet\V1161\Contract\GetStarred2DataProviderInterface;
11
use Usox\HyperSonic\FeatureSet\V1161\Responder\ResponderFactoryInterface;
12
use Usox\HyperSonic\Response\ResponderInterface;
13
14
/**
15
 * Retrieve and transforms starred data
16
 *
17
 * This class covers the `getStarred2.view` method
18
 *
19
 * @see http://www.subsonic.org/pages/api.jsp#getStarred2
20
 */
21
final class GetStarred2Method implements V1161MethodInterface
22
{
23 3
    public function __construct(
24
        private readonly ResponderFactoryInterface $responderFactory,
25
    ) {
26 3
    }
27
28
    /**
29
     * @param array<string, scalar> $queryParams
30
     * @param array<string, scalar> $args
31
     */
32 2
    public function __invoke(
33
        GetStarred2DataProviderInterface $getStarred2DataProvider,
34
        array $queryParams,
35
        array $args,
36
    ): ResponderInterface {
37 2
        $musicFolderId = (string) ($queryParams['musicFolderId'] ?? '');
38 2
        if ($musicFolderId === '') {
39 1
            $musicFolderId = null;
40
        }
41
42 2
        $data = $getStarred2DataProvider->getStarred($musicFolderId);
43
44 2
        return $this->responderFactory->createStarred2Responder(
45 2
            $this->buildSongList($data['songs']),
46 2
            $this->buildAlbumsList($data['albums']),
47 2
        );
48
    }
49
50
    /**
51
     * @param Traversable<array{
52
     *   id: int|string,
53
     *   name: string,
54
     *   artistName: string,
55
     *   coverArtId: string,
56
     *   artistId: int|string,
57
     *   length: int,
58
     *   createDate: DateTimeInterface,
59
     *   starredDate: DateTimeInterface,
60
     *   songCount: int,
61
     *   year: int
62
     * }> $albums
63
     *
64
     * @return Generator<array{
65
     *  id: string,
66
     *  name: string,
67
     *  artist: string,
68
     *  artistId: string,
69
     *  songCount: int,
70
     *  coverArt: string,
71
     *  duration: int,
72
     *  created: string,
73
     *  starred: string,
74
     *  year: int
75
     * }>
76
     */
77 1
    private function buildAlbumsList(Traversable $albums): Generator
78
    {
79 1
        foreach ($albums as $album) {
80 1
            yield [
81 1
                'id' => (string) $album['id'],
82 1
                'name' => $album['name'],
83 1
                'artist' => $album['artistName'],
84 1
                'artistId' => (string) $album['artistId'],
85 1
                'songCount' => $album['songCount'],
86 1
                'coverArt' => $album['coverArtId'],
87 1
                'duration' => $album['length'],
88 1
                'created' => $album['createDate']->format(DATE_ATOM),
89 1
                'starred' => $album['starredDate']->format(DATE_ATOM),
90 1
                'year' => $album['year'],
91 1
            ];
92
        }
93
    }
94
95
    /**
96
     * @param Traversable<array{
97
     *  id: int|string,
98
     *  name: string,
99
     *  artistName: string,
100
     *  artistId: int|string,
101
     *  albumName: string,
102
     *  albumId: int|string,
103
     *  coverArtId: string,
104
     *  length: int,
105
     *  createDate: DateTimeInterface,
106
     *  starredDate: DateTimeInterface,
107
     *  filesize: int
108
     * }> $songs
109
     *
110
     * @return Generator<array{
111
     *  id: string,
112
     *  title: string,
113
     *  album: string,
114
     *  artist: string,
115
     *  coverArt: string,
116
     *  albumId: string,
117
     *  artistId: string,
118
     *  duration: int,
119
     *  created: string,
120
     *  starred: string,
121
     *  size: int
122
     * }>
123
     */
124 1
    private function buildSongList(Traversable $songs): Generator
125
    {
126 1
        foreach ($songs as $song) {
127 1
            yield [
128 1
                'id' => (string) $song['id'],
129 1
                'title' => $song['name'],
130 1
                'album' => $song['albumName'],
131 1
                'artist' => $song['artistName'],
132 1
                'coverArt' => $song['coverArtId'],
133 1
                'albumId' => (string) $song['albumId'],
134 1
                'artistId' => (string) $song['artistId'],
135 1
                'duration' => $song['length'],
136 1
                'created' => $song['createDate']->format(DATE_ATOM),
137 1
                'starred' => $song['starredDate']->format(DATE_ATOM),
138 1
                'size' => $song['filesize'],
139 1
            ];
140
        }
141
    }
142
}
143