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   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 83
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getTrackInfo() 0 34 6
A makeRequest() 0 8 1
A getImage() 0 10 3
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