GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ArtistService::getInfo()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\LastFm\Service;
13
14
use Core23\LastFm\Builder\ArtistInfoBuilder;
15
use Core23\LastFm\Builder\ArtistTagsBuilder;
16
use Core23\LastFm\Builder\ArtistTopAlbumsBuilder;
17
use Core23\LastFm\Builder\ArtistTopTagsBuilder;
18
use Core23\LastFm\Builder\ArtistTopTracksBuilder;
19
use Core23\LastFm\Builder\SimilarArtistBuilder;
20
use Core23\LastFm\Client\ApiClientInterface;
21
use Core23\LastFm\Model\Album;
22
use Core23\LastFm\Model\Artist;
23
use Core23\LastFm\Model\ArtistInfo;
24
use Core23\LastFm\Model\Song;
25
use Core23\LastFm\Model\Tag;
26
use Core23\LastFm\Session\SessionInterface;
27
use Core23\LastFm\Util\ApiHelper;
28
use InvalidArgumentException;
29
30
final class ArtistService implements ArtistServiceInterface
31
{
32
    /**
33
     * @var ApiClientInterface
34
     */
35
    private $client;
36
37
    public function __construct(ApiClientInterface $client)
38
    {
39
        $this->client = $client;
40
    }
41
42
    public function addTags(SessionInterface $session, string $artist, array $tags): void
43
    {
44
        $count = \count($tags);
45
46
        if (0 === $count) {
47
            throw new InvalidArgumentException('No tags given');
48
        }
49
        if ($count > 10) {
50
            throw new InvalidArgumentException('A maximum of 10 tags is allowed');
51
        }
52
53
        array_filter($tags, static function ($tag) {
54
            if (null === $tag || !\is_string($tag)) {
55
                throw new InvalidArgumentException(sprintf('Invalid tag given'));
56
            }
57
        });
58
59
        $this->client->signedCall('artist.addTags', [
60
            'artist' => $artist,
61
            'tags'   => implode(',', $tags),
62
        ], $session, 'POST');
63
    }
64
65
    public function getCorrection(string $artist): ?Artist
66
    {
67
        $response = $this->client->unsignedCall('artist.getCorrection', [
68
            'artist' => $artist,
69
        ]);
70
71
        if (!isset($response['corrections']['correction']['artist'])) {
72
            return null;
73
        }
74
75
        return Artist::fromApi($response['corrections']['correction']['artist']);
76
    }
77
78
    public function getInfo(ArtistInfoBuilder $builder): ?ArtistInfo
79
    {
80
        $response = $this->client->unsignedCall('artist.getInfo', $builder->getQuery());
81
82
        if (!isset($response['artist'])) {
83
            return null;
84
        }
85
86
        return ArtistInfo::fromApi($response['artist']);
87
    }
88
89
    public function getSimilar(SimilarArtistBuilder $builder): array
90
    {
91
        $response = $this->client->unsignedCall('artist.getSimilar', $builder->getQuery());
92
93
        if (!isset($response['similarartists']['artist'])) {
94
            return [];
95
        }
96
97
        return ApiHelper::mapList(
98
            static function ($data) {
99
                return Artist::fromApi($data);
100
            },
101
            $response['similarartists']['artist']
102
        );
103
    }
104
105
    public function getTags(ArtistTagsBuilder $builder): array
106
    {
107
        $response = $this->client->unsignedCall('artist.getTags', $builder->getQuery());
108
109
        if (!isset($response['tags']['tag'])) {
110
            return [];
111
        }
112
113
        return ApiHelper::mapList(
114
            static function ($data) {
115
                return Tag::fromApi($data);
116
            },
117
            $response['tags']['tag']
118
        );
119
    }
120
121
    public function getTopAlbums(ArtistTopAlbumsBuilder $builder): array
122
    {
123
        $response =  $this->client->unsignedCall('artist.getTopAlbums', $builder->getQuery());
124
125
        if (!isset($response['topalbums']['album'])) {
126
            return [];
127
        }
128
129
        return ApiHelper::mapList(
130
            static function ($data) {
131
                return Album::fromApi($data);
132
            },
133
            $response['topalbums']['album']
134
        );
135
    }
136
137
    public function getTopTags(ArtistTopTagsBuilder $builder): array
138
    {
139
        $response = $this->client->unsignedCall('artist.getTopTags', $builder->getQuery());
140
141
        if (!isset($response['toptags']['tag'])) {
142
            return [];
143
        }
144
145
        return ApiHelper::mapList(
146
            static function ($data) {
147
                return Tag::fromApi($data);
148
            },
149
            $response['toptags']['tag']
150
        );
151
    }
152
153
    public function getTopTracks(ArtistTopTracksBuilder $builder): array
154
    {
155
        $response = $this->client->unsignedCall('artist.getTopTracks', $builder->getQuery());
156
157
        if (!isset($response['toptracks']['track'])) {
158
            return [];
159
        }
160
161
        return ApiHelper::mapList(
162
            static function ($data) {
163
                return Song::fromApi($data);
164
            },
165
            $response['toptracks']['track']
166
        );
167
    }
168
169
    public function removeTag(SessionInterface $session, string $artist, string $tag): void
170
    {
171
        $this->client->signedCall('artist.removeTag', [
172
            'artist' => $artist,
173
            'tag'    => $tag,
174
        ], $session, 'POST');
175
    }
176
177
    public function search(string $artist, int $limit = 50, int $page = 1): array
178
    {
179
        $response = $this->client->unsignedCall('artist.search', [
180
            'artist' => $artist,
181
            'limit'  => $limit,
182
            'page'   => $page,
183
        ]);
184
185
        if (!isset($response['results']['artistmatches']['artist'])) {
186
            return [];
187
        }
188
189
        return ApiHelper::mapList(
190
            static function ($data) {
191
                return Artist::fromApi($data);
192
            },
193
            $response['results']['artistmatches']['artist']
194
        );
195
    }
196
}
197