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\Exception\ErrorCodeEnum; |
11
|
|
|
use Usox\HyperSonic\Exception\MethodCallFailedException; |
12
|
|
|
use Usox\HyperSonic\FeatureSet\V1161\Contract\AlbumList2DataProviderInterface; |
13
|
|
|
use Usox\HyperSonic\FeatureSet\V1161\Responder\ResponderFactoryInterface; |
14
|
|
|
use Usox\HyperSonic\Response\ResponderInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Retrieve and transforms album data for the album list |
18
|
|
|
* |
19
|
|
|
* This class covers the `getAlbumList2.view` method |
20
|
|
|
* |
21
|
|
|
* @see http://www.subsonic.org/pages/api.jsp#getAlbumList2 |
22
|
|
|
*/ |
23
|
|
|
final class GetAlbumList2Method implements V1161MethodInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* List of allowed types for ordering |
27
|
|
|
* |
28
|
|
|
* @var string[] |
29
|
|
|
*/ |
30
|
|
|
private const ORDER_TYPES = [ |
31
|
|
|
'random', |
32
|
|
|
'newest', |
33
|
|
|
'frequent', |
34
|
|
|
'recent', |
35
|
|
|
'starred', |
36
|
|
|
'alphabeticalByName', |
37
|
|
|
'alphabeticalByArtist', |
38
|
|
|
'byYear', |
39
|
|
|
'byGenre', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
private const DEFAULT_LIMIT = 10; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
private const MAX_LIMIT = 500; |
51
|
|
|
|
52
|
14 |
|
public function __construct( |
53
|
|
|
private readonly ResponderFactoryInterface $responderFactory, |
54
|
|
|
) { |
55
|
14 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array<string, scalar> $queryParams |
59
|
|
|
* @param array<string, scalar> $args |
60
|
|
|
* |
61
|
|
|
* @throws MethodCallFailedException |
62
|
|
|
*/ |
63
|
13 |
|
public function __invoke( |
64
|
|
|
AlbumList2DataProviderInterface $albumList2DataProvider, |
65
|
|
|
array $queryParams, |
66
|
|
|
array $args, |
67
|
|
|
): ResponderInterface { |
68
|
13 |
|
$type = $queryParams['type'] ?? ''; |
69
|
13 |
|
$limit = min((int) ($queryParams['size'] ?? self::DEFAULT_LIMIT), self::MAX_LIMIT); |
70
|
13 |
|
$offset = (int) ($queryParams['offset'] ?? 0); |
71
|
13 |
|
$musicFolderId = $queryParams['musicFolderId'] ?? null; |
72
|
|
|
|
73
|
13 |
|
if ($musicFolderId !== null) { |
74
|
7 |
|
$musicFolderId = (string) $musicFolderId; |
75
|
|
|
} |
76
|
|
|
|
77
|
13 |
|
if (!in_array($type, self::ORDER_TYPES, true)) { |
78
|
1 |
|
throw new MethodCallFailedException( |
79
|
1 |
|
ErrorCodeEnum::MISSING_PARAMETER |
80
|
1 |
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
12 |
|
$orderParameter = []; |
84
|
|
|
|
85
|
12 |
|
if ($type === 'byYear') { |
86
|
3 |
|
$fromYear = $queryParams['fromYear'] ?? null; |
87
|
3 |
|
$toYear = $queryParams['toYear'] ?? null; |
88
|
|
|
|
89
|
3 |
|
if ($fromYear === null || $toYear === null) { |
90
|
2 |
|
throw new MethodCallFailedException( |
91
|
2 |
|
ErrorCodeEnum::MISSING_PARAMETER |
92
|
2 |
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$orderParameter = ['year' => ['from' => (int) $fromYear, 'to' => (int) $toYear]]; |
96
|
|
|
} |
97
|
|
|
|
98
|
10 |
|
if ($type === 'byGenre') { |
99
|
2 |
|
$genre = $queryParams['genre'] ?? null; |
100
|
2 |
|
if ($genre === null) { |
101
|
1 |
|
throw new MethodCallFailedException( |
102
|
1 |
|
ErrorCodeEnum::MISSING_PARAMETER |
103
|
1 |
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
$orderParameter = ['genre' => (string) $genre]; |
107
|
|
|
} |
108
|
|
|
|
109
|
9 |
|
$albumList = $albumList2DataProvider->getAlbums( |
110
|
9 |
|
$type, |
|
|
|
|
111
|
9 |
|
$limit, |
112
|
9 |
|
$offset, |
113
|
9 |
|
$orderParameter, |
114
|
9 |
|
$musicFolderId, |
115
|
9 |
|
); |
116
|
|
|
|
117
|
9 |
|
return $this->responderFactory->createAlbumList2Responder( |
118
|
9 |
|
$this->transformAlbums($albumList) |
119
|
9 |
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Traversable<array{ |
124
|
|
|
* id: string, |
125
|
|
|
* name: string, |
126
|
|
|
* coverArtId: string, |
127
|
|
|
* songCount: int, |
128
|
|
|
* createDate: DateTimeInterface, |
129
|
|
|
* duration: int, |
130
|
|
|
* artistName: string, |
131
|
|
|
* artistId: string, |
132
|
|
|
* }> $albumList |
133
|
|
|
* |
134
|
|
|
* @return Generator<array{ |
135
|
|
|
* id: string, |
136
|
|
|
* name: string, |
137
|
|
|
* coverArt: string, |
138
|
|
|
* songCount: int, |
139
|
|
|
* created: string, |
140
|
|
|
* duration: int, |
141
|
|
|
* artist: string, |
142
|
|
|
* artistId: string, |
143
|
|
|
* }> |
144
|
|
|
*/ |
145
|
7 |
|
private function transformAlbums(Traversable $albumList): Generator |
146
|
|
|
{ |
147
|
7 |
|
foreach ($albumList as $album) { |
148
|
7 |
|
yield [ |
149
|
7 |
|
'id' => $album['id'], |
150
|
7 |
|
'name' => $album['name'], |
151
|
7 |
|
'coverArt' => $album['coverArtId'], |
152
|
7 |
|
'songCount' => $album['songCount'], |
153
|
7 |
|
'created' => $album['createDate']->format(DATE_ATOM), |
154
|
7 |
|
'duration' => $album['duration'], |
155
|
7 |
|
'artist' => $album['artistName'], |
156
|
7 |
|
'artistId' => $album['artistId'], |
157
|
7 |
|
]; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|