Passed
Push — master ( f423b9...e8cce1 )
by Pulkit
10:09 queued 07:24
created

Geo::getTopArtists()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.6
c 0
b 0
f 0
cc 2
nc 4
nop 3
crap 2
1
<?php
2
namespace Sourceout\LastFm\Providers\LastFm\Resources;
3
4
use Sourceout\LastFm\Http\Response;
5
use Sourceout\LastFm\Http\HttpInterface;
6
use Sourceout\LastFm\Providers\GeoInterface;
7
use Sourceout\LastFm\Providers\ProviderInterface;
8
use Sourceout\LastFm\Providers\LastFm\Exception\LastFmException;
9
10
class Geo implements GeoInterface
11
{
12
    const TOP_ARTISTS_URI = "http://ws.audioscrobbler.com/2.0/";
13
    const TOP_TRACKS_URI = "http://ws.audioscrobbler.com/2.0/";
14
15
    /** @var HttpInterface */
16
    private $http;
17
18
    /** @var ProviderInterface */
19
    private $provider;
20
21
    /** @var string */
22
    private $format;
23
24
    /**
25
     * Constructor for Geo Class
26
     *
27
     * @param ProviderInterface $provider
28
     * @param HttpInterface $http
29
     * @param string $format
30
     * @todo Support for $format is yet not enabled, add that in future
31
     */
32 5
    public function __construct(
33
        ProviderInterface $provider,
34
        HttpInterface $http,
35
        $format = 'json'
36
    ) {
37 5
        $this->http = $http;
38 5
        $this->provider = $provider;
39 5
        $this->format = $format;
40 5
    }
41
42
    /** @inheritDoc */
43 2
    public function getTopArtists(
44
        string $country,
45
        int $page = 1,
46
        int $limit = 50
47
    ) {
48
        try {
49 2
            $query = http_build_query(
50
                [
51 2
                    'method' => 'geo.gettopartists',
52 2
                    'api_key' => $this->provider->getConfig()['api_key'],
53 2
                    'country' => $country,
54 2
                    'limit' => $limit,
55 2
                    'page' => $page,
56 2
                    'format' => 'json'
57
                ]
58
            );
59 2
            $response = $this->http->sendRequest('GET', Geo::TOP_ARTISTS_URI, $query);
0 ignored issues
show
Bug introduced by
$query of type string is incompatible with the type array expected by parameter $body of Sourceout\LastFm\Http\HttpInterface::sendRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            $response = $this->http->sendRequest('GET', Geo::TOP_ARTISTS_URI, /** @scrutinizer ignore-type */ $query);
Loading history...
60 1
            return Response::send($response);
61 1
        } catch (\Exception $e) {
62 1
            throw new LastFmException($e->getMessage(), $e->getCode(), $e);
63
        }
64
    }
65
66
    /** @inheritDoc */
67 2
    public function getTopTracks(
68
        string $country,
69
        string $location = null,
70
        int $limit = 50,
71
        int $page = 1
72
    ) {
73
        try {
74 2
            $query = http_build_query(
75
                [
76 2
                    'method' => 'geo.gettoptracks',
77 2
                    'api_key' => $this->provider->getConfig()['api_key'],
78 2
                    'country' => $country,
79 2
                    'location' => $location,
80 2
                    'limit' => $limit,
81 2
                    'page' => $page,
82 2
                    'format' => 'json'
83
                ]
84
            );
85 2
            $response = $this->http->sendRequest('GET', Geo::TOP_TRACKS_URI, $query);
0 ignored issues
show
Bug introduced by
$query of type string is incompatible with the type array expected by parameter $body of Sourceout\LastFm\Http\HttpInterface::sendRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
            $response = $this->http->sendRequest('GET', Geo::TOP_TRACKS_URI, /** @scrutinizer ignore-type */ $query);
Loading history...
86 1
            return Response::send($response);
87 1
        } catch (\Exception $e) {
88 1
            throw new LastFmException($e->getMessage(), $e->getCode(), $e);
89
        }
90
    }
91
}