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.

NowPlaying::getImage()   A
last analyzed

Complexity

Conditions 3
Paths 3

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 3
nc 3
nop 2
1
<?php
2
3
namespace Spatie\NowPlaying;
4
5
use Spatie\NowPlaying\Exceptions\BadResponse;
6
7
class NowPlaying
8
{
9
    /** @var string */
10
    protected $apiKey;
11
12
    public function __construct($apiKey)
13
    {
14
        $this->apiKey = $apiKey;
15
    }
16
17
    /**
18
     * @param $user
19
     *
20
     * @return array|bool
21
     *
22
     * @throws \Spatie\NowPlaying\Exceptions\BadResponse
23
     */
24
    public function getTrackInfo($user)
25
    {
26
        $lastFmResponse = $this->makeRequest($user);
27
28
        if (!isset($lastFmResponse['recenttracks'])) {
29
            throw BadResponse::create($lastFmResponse);
30
        }
31
32
        if (!count($lastFmResponse['recenttracks'])) {
33
            return false;
34
        };
35
36
        if (!isset($lastFmResponse['recenttracks']['track'][0])) {
37
            return false;
38
        }
39
40
        $lastTrack = $lastFmResponse['recenttracks']['track'][0];
41
42
        if (!isset($lastTrack['@attr']['nowplaying'])) {
43
            return false;
44
        }
45
46
        if (!$lastTrack['@attr']['nowplaying']) {
47
            return false;
48
        }
49
50
        return [
51
            'artist' => $lastTrack['artist']['#text'],
52
            'album' => $lastTrack['album']['#text'],
53
            'trackName' => $lastTrack['name'],
54
            'url'  => $lastTrack['url'],
55
            'artwork' => $this->getImage($lastTrack, 'extralarge'),
56
        ];
57
    }
58
59
    /**
60
     * @param string $user
61
     *
62
     * @return array
63
     */
64
    public function makeRequest($user)
65
    {
66
        $url = "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=1&user={$user}&api_key={$this->apiKey}&format=json";
67
68
        $response = file_get_contents($url);
69
70
        return json_decode($response, true);
71
    }
72
73
    /**
74
     * @param array $lastTrack
75
     * @param $versionName
76
     *
77
     * @return string
78
     */
79
    protected function getImage(array $lastTrack, $versionName)
80
    {
81
        foreach ($lastTrack['image'] as $image) {
82
            if ($image['size'] == $versionName) {
83
                return $image['#text'];
84
            }
85
        }
86
87
        return '';
88
    }
89
}
90