1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\SetlistFm\Service; |
13
|
|
|
|
14
|
|
|
use Core23\SetlistFm\Builder\SetlistSearchBuilder; |
15
|
|
|
use Core23\SetlistFm\Connection\ConnectionInterface; |
16
|
|
|
use Core23\SetlistFm\Exception\ApiException; |
17
|
|
|
use Core23\SetlistFm\Exception\NotFoundException; |
18
|
|
|
use Core23\SetlistFm\Model\Setlist; |
19
|
|
|
use Core23\SetlistFm\Model\SetlistSearchResult; |
20
|
|
|
|
21
|
|
|
final class SetlistService |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ConnectionInterface |
25
|
|
|
*/ |
26
|
|
|
private $connection; |
27
|
|
|
|
28
|
|
|
public function __construct(ConnectionInterface $connection) |
29
|
|
|
{ |
30
|
|
|
$this->connection = $connection; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get setlist information. |
35
|
|
|
* |
36
|
|
|
* @throws ApiException |
37
|
|
|
* @throws NotFoundException |
38
|
|
|
*/ |
39
|
|
|
public function getSetlist(string $setlistId): Setlist |
40
|
|
|
{ |
41
|
|
|
return Setlist::fromApi( |
42
|
|
|
$this->connection->call('setlist/'.$setlistId) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get setlist information by version id. |
48
|
|
|
* |
49
|
|
|
* @throws ApiException |
50
|
|
|
* @throws NotFoundException |
51
|
|
|
*/ |
52
|
|
|
public function getSetlistByVersion(string $versionId): Setlist |
53
|
|
|
{ |
54
|
|
|
return Setlist::fromApi( |
55
|
|
|
$this->connection->call('setlist/version/'.$versionId) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get artist setlists. |
61
|
|
|
* |
62
|
|
|
* @throws ApiException |
63
|
|
|
* @throws NotFoundException |
64
|
|
|
* |
65
|
|
|
* @return Setlist[] |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function getArtistSetlists(string $mbid, int $page = 1): array |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$response = $this->connection->call('artist/'.$mbid.'/setlists', [ |
70
|
|
|
'p' => $page, |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
if (!\array_key_exists('setlist', $response)) { |
74
|
|
|
return []; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return array_map(static function ($data) { |
78
|
|
|
return Setlist::fromApi($data); |
79
|
|
|
}, $response['setlist']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get venue setlists. |
84
|
|
|
* |
85
|
|
|
* @throws ApiException |
86
|
|
|
* @throws NotFoundException |
87
|
|
|
* |
88
|
|
|
* @return Setlist[] |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function getVenueSetlists(string $venueId, int $page = 1): array |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$response = $this->connection->call('venue/'.$venueId.'/setlists', [ |
93
|
|
|
'p' => $page, |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
if (!\array_key_exists('setlist', $response)) { |
97
|
|
|
return []; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return array_map(static function ($data) { |
101
|
|
|
return Setlist::fromApi($data); |
102
|
|
|
}, $response['setlist']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Search for setlists. |
107
|
|
|
* |
108
|
|
|
* @throws ApiException |
109
|
|
|
* @throws NotFoundException |
110
|
|
|
*/ |
111
|
|
|
public function search(SetlistSearchBuilder $builder): SetlistSearchResult |
112
|
|
|
{ |
113
|
|
|
$response= $this->connection->call('search/setlists', $builder->getQuery()); |
114
|
|
|
|
115
|
|
|
if (!\array_key_exists('setlist', $response)) { |
116
|
|
|
return SetlistSearchResult::createEmpty(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return SetlistSearchResult::fromApi($response); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.