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 ( e30cbf...f60278 )
by Christian
01:38
created

AlbumInfo::createTracksFromApi()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
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\Model;
13
14
final class AlbumInfo
15
{
16
    /**
17
     * @var string|null
18
     */
19
    private $name;
20
21
    /**
22
     * @var Artist|null
23
     */
24
    private $artist;
25
26
    /**
27
     * @var string|null
28
     */
29
    private $mbid;
30
31
    /**
32
     * @var string|null
33
     */
34
    private $url;
35
36
    /**
37
     * @var Image[]
38
     */
39
    private $images;
40
41
    /**
42
     * @var int
43
     */
44
    private $listeners;
45
46
    /**
47
     * @var int
48
     */
49
    private $playcount;
50
51
    /**
52
     * @var Song[]
53
     */
54
    private $tracks;
55
56
    /**
57
     * @var Tag[]
58
     */
59
    private $tags;
60
61
    /**
62
     * @var string|null
63
     */
64
    private $wikiSummary;
65
66
    /**
67
     * @param string|null $name
68
     * @param Artist|null $artist
69
     * @param string|null $mbid
70
     * @param string|null $url
71
     * @param Image[]     $images
72
     * @param int         $listeners
73
     * @param int         $playcount
74
     * @param Song[]      $tracks
75
     * @param Tag[]       $tags
76
     * @param string|null $wikiSummary
77
     */
78
    public function __construct(
79
        ?string $name,
80
        ?Artist $artist,
81
        ?string $mbid,
82
        ?string $url,
83
        array $images,
84
        int $listeners,
85
        int $playcount,
86
        array $tracks,
87
        array $tags,
88
        ?string $wikiSummary
89
    ) {
90
        $this->name         = $name;
91
        $this->artist       = $artist;
92
        $this->mbid         = $mbid;
93
        $this->url          = $url;
94
        $this->images       = $images;
95
        $this->listeners    = $listeners;
96
        $this->playcount    = $playcount;
97
        $this->tracks       = $tracks;
98
        $this->tags         = $tags;
99
        $this->wikiSummary  = $wikiSummary;
100
    }
101
102
    /**
103
     * @return string|null
104
     */
105
    public function getName(): ?string
106
    {
107
        return $this->name;
108
    }
109
110
    /**
111
     * @return Artist|null
112
     */
113
    public function getArtist(): ?Artist
114
    {
115
        return $this->artist;
116
    }
117
118
    /**
119
     * @return string|null
120
     */
121
    public function getMbid(): ?string
122
    {
123
        return $this->mbid;
124
    }
125
126
    /**
127
     * @return string|null
128
     */
129
    public function getUrl(): ?string
130
    {
131
        return $this->url;
132
    }
133
134
    /**
135
     * @return Image[]
136
     */
137
    public function getImage(): array
138
    {
139
        return $this->images;
140
    }
141
142
    /**
143
     * @return int
144
     */
145
    public function getListeners(): int
146
    {
147
        return $this->listeners;
148
    }
149
150
    /**
151
     * @return int
152
     */
153
    public function getPlaycount(): int
154
    {
155
        return $this->playcount;
156
    }
157
158
    /**
159
     * @return Song[]
160
     */
161
    public function getTracks(): array
162
    {
163
        return $this->tracks;
164
    }
165
166
    /**
167
     * @return Tag[]
168
     */
169
    public function getTags(): array
170
    {
171
        return $this->tags;
172
    }
173
174
    /**
175
     * @return string|null
176
     */
177
    public function getWikiSummary(): ?string
178
    {
179
        return $this->wikiSummary;
180
    }
181
182
    /**
183
     * @param array $data
184
     *
185
     * @return AlbumInfo
186
     */
187
    public static function fromApi(array $data): self
188
    {
189
        $images = self::createImagesFromApi($data);
190
        $tracks = self::createTracksFromApi($data);
191
        $tags   = self::createTagsFromApi($data);
192
193
        return new self(
194
            $data['name'],
195
            new Artist($data['artist'], null, [], null),
196
            $data['mbid'] ?? null,
197
            $data['url'] ?? null,
198
            $images,
199
            $data['listeners'] ?? 0,
200
            $data['playcount'] ?? 0,
201
            $tracks,
202
            $tags,
203
            $data['wiki']['summary'] ?? null
204
        );
205
    }
206
207
    /**
208
     * @param array $data
209
     *
210
     * @return array
211
     */
212
    private static function createImagesFromApi(array $data): array
213
    {
214
        $images = [];
215
216
        if (\array_key_exists('image', $data)) {
217
            foreach ((array) $data['image'] as $image) {
218
                $images[] = new Image($image['#text']);
219
            }
220
        }
221
222
        return $images;
223
    }
224
225
    /**
226
     * @param array $data
227
     *
228
     * @return array
229
     */
230
    private static function createTracksFromApi(array $data): array
231
    {
232
        $tracks = [];
233
234
        if (\array_key_exists('tracks', $data) && \array_key_exists('track', $data['tracks'])) {
235
            foreach ((array) $data['tracks']['track'] as $track) {
236
                $tracks[] = Song::fromApi($track);
237
            }
238
        }
239
240
        return $tracks;
241
    }
242
243
    /**
244
     * @param array $data
245
     *
246
     * @return array
247
     */
248
    private static function createTagsFromApi(array $data): array
249
    {
250
        $tags = [];
251
252
        if (\array_key_exists('tags', $data) && \array_key_exists('tag', $data['tags'])) {
253
            foreach ((array) $data['tags']['tag'] as $tag) {
254
                $tags[] = Tag::fromApi($tag);
255
            }
256
        }
257
258
        return $tags;
259
    }
260
}
261