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

AlbumService::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 3
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\AlbumInfoBuilder;
15
use Core23\LastFm\Builder\AlbumTagsBuilder;
16
use Core23\LastFm\Builder\AlbumTopTagsBuilder;
17
use Core23\LastFm\Client\ApiClientInterface;
18
use Core23\LastFm\Model\Album;
19
use Core23\LastFm\Model\AlbumInfo;
20
use Core23\LastFm\Model\Tag;
21
use Core23\LastFm\Session\SessionInterface;
22
use Core23\LastFm\Util\ApiHelper;
23
use InvalidArgumentException;
24
25
final class AlbumService implements AlbumServiceInterface
26
{
27
    /**
28
     * @var ApiClientInterface
29
     */
30
    private $client;
31
32
    /**
33
     * @param ApiClientInterface $client
34
     */
35
    public function __construct(ApiClientInterface $client)
36
    {
37
        $this->client = $client;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function addTags(SessionInterface $session, string $artist, string $album, array $tags): void
44
    {
45
        $count = \count($tags);
46
47
        if (0 === $count) {
48
            throw new InvalidArgumentException('No tags given');
49
        }
50
51
        if ($count > 10) {
52
            throw new InvalidArgumentException('A maximum of 10 tags is allowed');
53
        }
54
55
        array_filter($tags, static function ($tag) {
56
            if (null === $tag || !\is_string($tag)) {
57
                throw new InvalidArgumentException(sprintf('Invalid tag given'));
58
            }
59
        });
60
61
        $this->client->signedCall('album.addTags', [
62
            'artist' => $artist,
63
            'album'  => $album,
64
            'tags'   => implode(',', $tags),
65
        ], $session, 'POST');
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getInfo(AlbumInfoBuilder $builder): AlbumInfo
72
    {
73
        $response = $this->client->unsignedCall('album.getInfo', $builder->getQuery());
74
75
        return AlbumInfo::fromApi($response['album']);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 View Code Duplication
    public function getTags(AlbumTagsBuilder $builder): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
82
    {
83
        $response = $this->client->unsignedCall('album.getTags', $builder->getQuery());
84
85
        if (!isset($response['tags']['tag'])) {
86
            return [];
87
        }
88
89
        return ApiHelper::mapList(
90
            static function ($data) {
91
                return Tag::fromApi($data);
92
            },
93
            $response['tags']['tag']
94
        );
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 View Code Duplication
    public function getTopTags(AlbumTopTagsBuilder $builder): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
101
    {
102
        $response = $this->client->unsignedCall('album.getTopTags', $builder->getQuery());
103
104
        if (!isset($response['toptags']['tag'])) {
105
            return [];
106
        }
107
108
        return ApiHelper::mapList(
109
            static function ($data) {
110
                return Tag::fromApi($data);
111
            },
112
            $response['toptags']['tag']
113
        );
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function removeTag(SessionInterface $session, string $artist, string $album, string $tag): void
120
    {
121
        $this->client->signedCall('album.removeTag', [
122
            'artist' => $artist,
123
            'album'  => $album,
124
            'tag'    => $tag,
125
        ], $session, 'POST');
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function search(string $album, int $limit = 50, int $page = 1): array
132
    {
133
        $response = $this->client->unsignedCall('album.search', [
134
            'album' => $album,
135
            'limit' => $limit,
136
            'page'  => $page,
137
        ]);
138
139
        if (!isset($response['results']['albummatches']['album'])) {
140
            return [];
141
        }
142
143
        return ApiHelper::mapList(
144
            static function ($data) {
145
                return Album::fromApi($data);
146
            },
147
            $response['results']['albummatches']['album']
148
        );
149
    }
150
}
151