|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Usox\HyperSonic\FeatureSet\V1161\Method; |
|
6
|
|
|
|
|
7
|
|
|
use Generator; |
|
8
|
|
|
use Traversable; |
|
9
|
|
|
use Usox\HyperSonic\FeatureSet\V1161\Contract\GetRandomSongsDataProviderInterface; |
|
10
|
|
|
use Usox\HyperSonic\FeatureSet\V1161\Responder\ResponderFactoryInterface; |
|
11
|
|
|
use Usox\HyperSonic\Response\ResponderInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Retrieve random songs |
|
15
|
|
|
* |
|
16
|
|
|
* This class covers the `getRandomSongs.view` method |
|
17
|
|
|
* |
|
18
|
|
|
* @see http://www.subsonic.org/pages/api.jsp#getRandomSongs |
|
19
|
|
|
*/ |
|
20
|
|
|
final class GetRandomSongsMethod implements V1161MethodInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var int */ |
|
23
|
|
|
private const MAX_SONGS = 500; |
|
24
|
|
|
|
|
25
|
3 |
|
public function __construct( |
|
26
|
|
|
private readonly ResponderFactoryInterface $responderFactory |
|
27
|
|
|
) { |
|
28
|
3 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array{ |
|
32
|
|
|
* musicFolderId?: int|string, |
|
33
|
|
|
* size?: int|string, |
|
34
|
|
|
* genre?: string, |
|
35
|
|
|
* fromYear?: int|string, |
|
36
|
|
|
* toYear?: int|string |
|
37
|
|
|
* } $queryParams |
|
38
|
|
|
* @param array<string, scalar> $args |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function __invoke( |
|
41
|
|
|
GetRandomSongsDataProviderInterface $dataProvider, |
|
42
|
|
|
array $queryParams, |
|
43
|
|
|
array $args, |
|
44
|
|
|
): ResponderInterface { |
|
45
|
2 |
|
$musicFolderId = (string) ($queryParams['musicFolderId'] ?? ''); |
|
46
|
2 |
|
$limit = (int) ($queryParams['size'] ?? 10); |
|
47
|
2 |
|
$genre = $queryParams['genre'] ?? null; |
|
48
|
2 |
|
$fromYear = (int) ($queryParams['fromYear'] ?? 0); |
|
49
|
2 |
|
$toYear = (int) ($queryParams['toYear'] ?? 0); |
|
50
|
|
|
|
|
51
|
2 |
|
if ($musicFolderId === '') { |
|
52
|
1 |
|
$musicFolderId = null; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
if ($fromYear === 0) { |
|
56
|
1 |
|
$fromYear = null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
if ($toYear === 0) { |
|
60
|
1 |
|
$toYear = null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
if ($limit > self::MAX_SONGS) { |
|
64
|
1 |
|
$limit = self::MAX_SONGS; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
return $this->responderFactory->createRandomSongsResponder( |
|
68
|
2 |
|
$this->buildSongList( |
|
69
|
2 |
|
$dataProvider->getRandomSongs( |
|
70
|
2 |
|
$musicFolderId, |
|
71
|
2 |
|
$limit, |
|
72
|
2 |
|
$genre, |
|
73
|
2 |
|
$fromYear, |
|
74
|
2 |
|
$toYear |
|
75
|
2 |
|
) |
|
76
|
2 |
|
) |
|
77
|
2 |
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param Traversable<array{ |
|
82
|
|
|
* id: int|string, |
|
83
|
|
|
* name: string, |
|
84
|
|
|
* albumId: int|string, |
|
85
|
|
|
* artistName: string, |
|
86
|
|
|
* albumName: string, |
|
87
|
|
|
* trackNumber: int, |
|
88
|
|
|
* year: int, |
|
89
|
|
|
* genre: string, |
|
90
|
|
|
* coverArtId: string, |
|
91
|
|
|
* length: int, |
|
92
|
|
|
* filesize: int |
|
93
|
|
|
* }> $songs |
|
94
|
|
|
* |
|
95
|
|
|
* @return Generator<array{ |
|
96
|
|
|
* id: string, |
|
97
|
|
|
* parent: string, |
|
98
|
|
|
* title: string, |
|
99
|
|
|
* isDir: string, |
|
100
|
|
|
* album: string, |
|
101
|
|
|
* artist: string, |
|
102
|
|
|
* track: int, |
|
103
|
|
|
* year: int, |
|
104
|
|
|
* coverArt: string, |
|
105
|
|
|
* duration: int, |
|
106
|
|
|
* size: int |
|
107
|
|
|
* }> |
|
108
|
|
|
*/ |
|
109
|
2 |
|
private function buildSongList(Traversable $songs): Generator |
|
110
|
|
|
{ |
|
111
|
2 |
|
foreach ($songs as $song) { |
|
112
|
2 |
|
yield [ |
|
113
|
2 |
|
'id' => (string) $song['id'], |
|
114
|
2 |
|
'parent' => (string) $song['albumId'], |
|
115
|
2 |
|
'title' => $song['name'], |
|
116
|
2 |
|
'isDir' => 'false', |
|
117
|
2 |
|
'album' => $song['albumName'], |
|
118
|
2 |
|
'artist' => $song['artistName'], |
|
119
|
2 |
|
'track' => $song['trackNumber'], |
|
120
|
2 |
|
'year' => $song['year'], |
|
121
|
2 |
|
'genre' => $song['genre'], |
|
122
|
2 |
|
'coverArt' => $song['coverArtId'], |
|
123
|
2 |
|
'duration' => $song['length'], |
|
124
|
2 |
|
'size' => $song['filesize'], |
|
125
|
2 |
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|