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

TrackService::__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\ScrobbeBuilder;
15
use Core23\LastFm\Builder\SimilarTrackBuilder;
16
use Core23\LastFm\Builder\TrackInfoBuilder;
17
use Core23\LastFm\Builder\TrackTagsBuilder;
18
use Core23\LastFm\Builder\TrackTopTagsBuilder;
19
use Core23\LastFm\Client\ApiClientInterface;
20
use Core23\LastFm\Model\NowPlaying;
21
use Core23\LastFm\Model\Song;
22
use Core23\LastFm\Model\SongInfo;
23
use Core23\LastFm\Model\Tag;
24
use Core23\LastFm\Session\SessionInterface;
25
use Core23\LastFm\Util\ApiHelper;
26
use InvalidArgumentException;
27
28
final class TrackService implements TrackServiceInterface
29
{
30
    /**
31
     * @var ApiClientInterface
32
     */
33
    private $client;
34
35
    /**
36
     * @param ApiClientInterface $client
37
     */
38
    public function __construct(ApiClientInterface $client)
39
    {
40
        $this->client = $client;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function addTags(SessionInterface $session, string $artist, string $track, array $tags): void
47
    {
48
        $count = \count($tags);
49
50
        if (0 === $count) {
51
            throw new InvalidArgumentException('No tags given');
52
        }
53
        if ($count > 10) {
54
            throw new InvalidArgumentException('A maximum of 10 tags is allowed');
55
        }
56
57
        array_filter($tags, static function ($tag) {
58
            if (null === $tag || !\is_string($tag)) {
59
                throw new InvalidArgumentException(sprintf('Invalid tag given'));
60
            }
61
        });
62
63
        $this->client->signedCall('track.addTags', [
64
            'artist' => $artist,
65
            'track'  => $track,
66
            'tags'   => implode(',', $tags),
67
        ], $session, 'POST');
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getCorrection(string $artist, string $track): ?Song
74
    {
75
        $response = $this->client->unsignedCall('track.getCorrection', [
76
            'artist' => $artist,
77
            'track'  => $track,
78
        ]);
79
80
        if (!isset($response['corrections']['correction']['track'])) {
81
            return null;
82
        }
83
84
        return Song::fromApi($response['corrections']['correction']['track']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Core23\LastFm\Mo...correction']['track']); (self) is incompatible with the return type declared by the interface Core23\LastFm\Service\Tr...nterface::getCorrection of type Core23\LastFm\Model\Song|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(TrackInfoBuilder $builder): ?SongInfo
91
    {
92
        $response = $this->client->unsignedCall('track.getInfo', $builder->getQuery());
93
94
        if (!isset($response['track'])) {
95
            return null;
96
        }
97
98
        return SongInfo::fromApi($response['track']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Core23\LastFm\Mo...pi($response['track']); (self) is incompatible with the return type declared by the interface Core23\LastFm\Service\Tr...rviceInterface::getInfo of type Core23\LastFm\Model\SongInfo|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(SimilarTrackBuilder $builder): array
105
    {
106
        $response = $this->client->unsignedCall('track.getSimilar', $builder->getQuery());
107
108
        if (!isset($response['similartracks']['track'])) {
109
            return [];
110
        }
111
112
        return ApiHelper::mapList(
113
            static function ($data) {
114
                return SongInfo::fromApi($data);
115
            },
116
            $response['similartracks']['track']
117
        );
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function getTags(TrackTagsBuilder $builder): array
124
    {
125
        $response = $this->client->unsignedCall('track.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 getTopTags(TrackTopTagsBuilder $builder): array
143
    {
144
        $response = $this->client->unsignedCall('track.getTopTags', $builder->getQuery());
145
146
        if (!isset($response['toptags']['tag'])) {
147
            return [];
148
        }
149
150
        return ApiHelper::mapList(
151
            static function ($data) {
152
                return Tag::fromApi($data);
153
            },
154
            $response['toptags']['tag']
155
        );
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function love(SessionInterface $session, string $artist, string $track): void
162
    {
163
        $this->client->signedCall('track.love', [
164
            'artist' => $artist,
165
            'track'  => $track,
166
        ], $session, 'POST');
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function removeTag(SessionInterface $session, string $artist, string $track, string $tag): void
173
    {
174
        $this->client->signedCall('track.removeTag', [
175
            'artist' => $artist,
176
            'track'  => $track,
177
            'tag'    => $tag,
178
        ], $session, 'POST');
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function scrobble(SessionInterface $session, ScrobbeBuilder $builder): void
185
    {
186
        $count = $builder->count();
187
188
        if (0 === $count) {
189
            return;
190
        }
191
        if ($count > 10) {
192
            throw new InvalidArgumentException('A maximum of 50 tracks is allowed');
193
        }
194
195
        $this->client->signedCall('album.scrobble', $builder->getQuery(), $session, 'POST');
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function search(string $track, int $limit = 50, int $page = 1): array
202
    {
203
        $response = $this->client->unsignedCall('track.search', [
204
            'track' => $track,
205
            'limit' => $limit,
206
            'page'  => $page,
207
        ]);
208
209
        if (!isset($response['results']['trackmatches']['track'])) {
210
            return [];
211
        }
212
213
        return ApiHelper::mapList(
214
            static function ($data) {
215
                return SongInfo::fromApi($data);
216
            },
217
            $response['results']['trackmatches']['track']
218
        );
219
    }
220
221
    /**
222
     * {@inheritdoc}
223
     */
224
    public function unlove(SessionInterface $session, string $artist, string $track): void
225
    {
226
        $this->client->signedCall('track.love', [
227
            'artist' => $artist,
228
            'track'  => $track,
229
        ], $session, 'POST');
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public function updateNowPlaying(SessionInterface $session, NowPlaying $nowPlaying): void
236
    {
237
        $this->client->signedCall('track.updateNowPlaying', $nowPlaying->toArray(), $session, 'POST');
238
    }
239
}
240