Passed
Push — main ( 45ffa3...f692a6 )
by Daniel
02:23
created

GetStarred2Method::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 2
rs 10
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
    }
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
        );
48
    }
49
50
    /**
51
     * @param Traversable<array{
52
     *   id: int|string,
53
     *   name: string,
54
     *   albumName: string,
55
     *   artistName: string,
56
     *   coverArtId: string,
57
     *   albumId: int|string,
58
     *   artistId: int|string,
59
     *   length: int,
60
     *   createDate: DateTimeInterface,
61
     *   starredDate: DateTimeInterface,
62
     *   songCount: int,
63
     *   year: int
64
     * }> $albums
65
     *
66
     * @return Generator<array{
67
     *  id: string,
68
     *  name: string,
69
     *  artist: string,
70
     *  artistId: string,
71
     *  songCount: int,
72
     *  coverArt: string,
73
     *  duration: int,
74
     *  created: string,
75
     *  starred: string,
76
     *  year: int
77
     * }>
78
     */
79
    private function buildAlbumsList(Traversable $albums): Generator
80
    {
81
        foreach ($albums as $album) {
82
            yield [
83
                'id' => (string) $album['id'],
84
                'name' => $album['name'],
85
                'artist' => $album['artistName'],
86
                'artistId' => (string) $album['artistId'],
87
                'songCount' => $album['songCount'],
88
                'coverArt' => $album['coverArtId'],
89
                'duration' => $album['length'],
90
                'created' => $album['createDate']->format(DATE_ATOM),
91
                'starred' => $album['starredDate']->format(DATE_ATOM),
92
                'year' => $album['year'],
93
            ];
94
        }
95
    }
96
97
    /**
98
     * @param Traversable<array{
99
     *  id: int|string,
100
     *  name: string,
101
     *  artistName: string,
102
     *  artistId: int|string,
103
     *  albumName: string,
104
     *  albumId: int|string,
105
     *  coverArtId: string,
106
     *  length: int,
107
     *  createDate: DateTimeInterface,
108
     *  starredDate: DateTimeInterface,
109
     *  filesize: int
110
     * }> $songs
111
     *
112
     * @return Generator<array{
113
     *  id: string,
114
     *  name: string,
115
     *  album: string,
116
     *  artist: string,
117
     *  coverArt: string,
118
     *  albumId: string,
119
     *  artistId: string,
120
     *  duration: int,
121
     *  created: string,
122
     *  starred: string,
123
     *  size: int
124
     * }>
125
     */
126
    private function buildSongList(Traversable $songs): Generator
127
    {
128
        foreach ($songs as $song) {
129
            yield [
130
                'id' => (string) $song['id'],
131
                'name' => $song['name'],
132
                'album' => $song['albumName'],
133
                'artist' => $song['artistName'],
134
                'coverArt' => $song['coverArtId'],
135
                'albumId' => (string) $song['albumId'],
136
                'artistId' => (string) $song['artistId'],
137
                'duration' => $song['length'],
138
                'created' => $song['createDate']->format(DATE_ATOM),
139
                'starred' => $song['starredDate']->format(DATE_ATOM),
140
                'size' => $song['filesize'],
141
            ];
142
        }
143
    }
144
}
145