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.
Completed
Push — master ( 625674...909c28 )
by Christian
01:42
created

ArtistService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
    /**
38
     * @param ApiClientInterface $client
39
     */
40
    public function __construct(ApiClientInterface $client)
41
    {
42
        $this->client = $client;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function addTags(SessionInterface $session, string $artist, array $tags): void
49
    {
50
        $count = \count($tags);
51
52
        if (0 === $count) {
53
            throw new InvalidArgumentException('No tags given');
54
        }
55
        if ($count > 10) {
56
            throw new InvalidArgumentException('A maximum of 10 tags is allowed');
57
        }
58
59
        array_filter($tags, static function ($tag) {
60
            if (null === $tag || !\is_string($tag)) {
61
                throw new InvalidArgumentException(sprintf('Invalid tag given'));
62
            }
63
        });
64
65
        $this->client->signedCall('artist.addTags', [
66
            'artist' => $artist,
67
            'tags'   => implode(',', $tags),
68
        ], $session, 'POST');
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getCorrection(string $artist): ?Artist
75
    {
76
        $response = $this->client->unsignedCall('artist.getCorrection', [
77
            'artist' => $artist,
78
        ]);
79
80
        if (!isset($response['corrections']['correction']['artist'])) {
81
            return null;
82
        }
83
84
        return Artist::fromApi($response['corrections']['correction']['artist']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Core23\LastFm\Mo...orrection']['artist']); (self) is incompatible with the return type declared by the interface Core23\LastFm\Service\Ar...nterface::getCorrection of type Core23\LastFm\Model\Artist|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getInfo(ArtistInfoBuilder $builder): ?ArtistInfo
91
    {
92
        $response = $this->client->unsignedCall('artist.getInfo', $builder->getQuery());
93
94
        if (!isset($response['artist'])) {
95
            return null;
96
        }
97
98
        return ArtistInfo::fromApi($response['artist']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Core23\LastFm\Mo...i($response['artist']); (self) is incompatible with the return type declared by the interface Core23\LastFm\Service\Ar...rviceInterface::getInfo of type Core23\LastFm\Model\ArtistInfo|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getSimilar(SimilarArtistBuilder $builder): array
105
    {
106
        $response = $this->client->unsignedCall('artist.getSimilar', $builder->getQuery());
107
108
        if (!isset($response['similarartists']['artist'])) {
109
            return [];
110
        }
111
112
        return ApiHelper::mapList(
113
            static function ($data) {
114
                return Artist::fromApi($data);
115
            },
116
            $response['similarartists']['artist']
117
        );
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function getTags(ArtistTagsBuilder $builder): array
124
    {
125
        $response = $this->client->unsignedCall('artist.getTags', $builder->getQuery());
126
127
        if (!isset($response['tags']['tag'])) {
128
            return [];
129
        }
130
131
        return ApiHelper::mapList(
132
            static function ($data) {
133
                return Tag::fromApi($data);
134
            },
135
            $response['tags']['tag']
136
        );
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function getTopAlbums(ArtistTopAlbumsBuilder $builder): array
143
    {
144
        $response =  $this->client->unsignedCall('artist.getTopAlbums', $builder->getQuery());
145
146
        if (!isset($response['topalbums']['album'])) {
147
            return [];
148
        }
149
150
        return ApiHelper::mapList(
151
            static function ($data) {
152
                return Album::fromApi($data);
153
            },
154
            $response['topalbums']['album']
155
        );
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function getTopTags(ArtistTopTagsBuilder $builder): array
162
    {
163
        $response = $this->client->unsignedCall('artist.getTopTags', $builder->getQuery());
164
165
        if (!isset($response['toptags']['tag'])) {
166
            return [];
167
        }
168
169
        return ApiHelper::mapList(
170
            static function ($data) {
171
                return Tag::fromApi($data);
172
            },
173
            $response['toptags']['tag']
174
        );
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function getTopTracks(ArtistTopTracksBuilder $builder): array
181
    {
182
        $response = $this->client->unsignedCall('artist.getTopTracks', $builder->getQuery());
183
184
        if (!isset($response['toptracks']['track'])) {
185
            return [];
186
        }
187
188
        return ApiHelper::mapList(
189
            static function ($data) {
190
                return Song::fromApi($data);
191
            },
192
            $response['toptracks']['track']
193
        );
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function removeTag(SessionInterface $session, string $artist, string $tag): void
200
    {
201
        $this->client->signedCall('artist.removeTag', [
202
            'artist' => $artist,
203
            'tag'    => $tag,
204
        ], $session, 'POST');
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    public function search(string $artist, int $limit = 50, int $page = 1): array
211
    {
212
        $response = $this->client->unsignedCall('artist.search', [
213
            'artist' => $artist,
214
            'limit'  => $limit,
215
            'page'   => $page,
216
        ]);
217
218
        if (!isset($response['results']['artistmatches']['artist'])) {
219
            return [];
220
        }
221
222
        return ApiHelper::mapList(
223
            static function ($data) {
224
                return Artist::fromApi($data);
225
            },
226
            $response['results']['artistmatches']['artist']
227
        );
228
    }
229
}
230