LastFm::getLovedTracks()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 6
cp 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
namespace HansOtt\Lastify\Services;
4
5
use Dandelionmood\LastFm\LastFm as LastFmWrapper;
6
use HansOtt\Lastify\TrackInfo;
7
use HansOtt\Lastify\TrackCollection;
8
use HansOtt\Lastify\TrackInfo\Artist;
9
use InvalidArgumentException;
10
11
final class LastFm
12
{
13
    private $api;
14
15
    public function __construct(LastFmWrapper $api)
16
    {
17
        $this->api = $api;
18
    }
19
20
    public static function connect($apiKey)
21
    {
22
        $api = new LastFmWrapper($apiKey);
23
24
        return new static($api);
25
    }
26
27 View Code Duplication
    private function fetchTopTracks($username, $limit)
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...
28
    {
29
        $params = [
30
            'user' => $username,
31
            'limit' => $limit,
32
        ];
33
34
        $response = $this->api->user_getTopTracks($params);
35
36
        $topTracks = isset($response->toptracks) && is_array($response->toptracks->track)
37
            ? $response->toptracks->track
38
            : [];
39
40
        return $topTracks;
41
    }
42
43
    private static function convertToTrackCollection(array $tracks)
44
    {
45
        $convertTrack = function ($track) {
46
            return static::convertToTrack($track);
0 ignored issues
show
Comprehensibility introduced by
Since HansOtt\Lastify\Services\LastFm is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
47
        };
48
49
        $tracks = array_map($convertTrack, $tracks);
50
51
        return new TrackCollection($tracks);
52
    }
53
54
    private static function convertToTrack($track)
55
    {
56
        $artist = new Artist($track->artist->name);
57
58
        return new TrackInfo($track->name, [$artist]);
59
    }
60
61 View Code Duplication
    private function assertValidUsername($username)
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...
62
    {
63
        if (!is_string($username)) {
64
            throw new InvalidArgumentException('The username should be a string, instead got:' . gettype($username));
65
        }
66
        if (empty($username)) {
67
            throw new InvalidArgumentException('The username cannot be empty');
68
        }
69
    }
70
71
    public function getTopTracks($username, $limit = 50)
72
    {
73
        $this->assertValidUsername($username);
74
        $topTracks = $this->fetchTopTracks($username, $limit);
75
        $topTracks = static::convertToTrackCollection($topTracks);
0 ignored issues
show
Comprehensibility introduced by
Since HansOtt\Lastify\Services\LastFm is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
76
77
        return $topTracks;
78
    }
79
80
    public function getLovedTracks($username, $limit = 50)
81
    {
82
        $lovedTracks = $this->fetchLovedTracks($username, $limit);
83
        $lovedTracks = static::convertToTrackCollection($lovedTracks);
0 ignored issues
show
Comprehensibility introduced by
Since HansOtt\Lastify\Services\LastFm is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
84
85
        return $lovedTracks;
86
    }
87
88 View Code Duplication
    private function fetchLovedTracks($username, $limit)
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...
89
    {
90
        $params = [
91
            'user' => $username,
92
            'limit' => $limit,
93
        ];
94
95
        $response = $this->api->user_getLovedTracks($params);
96
97
        $lovedTracks = isset($response->lovedtracks) && is_array($response->lovedtracks->track)
98
            ? $response->lovedtracks->track
99
            : [];
100
101
        return $lovedTracks;
102
    }
103
}
104