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 ( 499c68...2aea15 )
by Krzysztof
02:06
created

Client::getTickerByCoinId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Coinpaprika;
4
5
use Coinpaprika\Exception\RateLimitExceededException;
6
use Coinpaprika\Exception\ResponseErrorException;
7
use Coinpaprika\Model\Coin;
8
use Coinpaprika\Model\GlobalStats;
9
use Coinpaprika\Model\Ticker;
10
use JMS\Serializer\SerializerBuilder;
11
use JMS\Serializer\Serializer;
12
use GuzzleHttp\Exception\GuzzleException;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * Class Client
17
 *
18
 * @package Coinpaprika
19
 *
20
 * @author Krzysztof Przybyszewski <[email protected]>
21
 */
22
class Client
23
{
24
    const BASE_URL = 'https://api.coinpaprika.com/%ver%/';
25
26
    /**
27
     * @var string
28
     */
29
    private $apiVersion;
30
31
    /**
32
     * @var \GuzzleHttp\Client
33
     */
34
    private $httpClient;
35
36
    /**
37
     * @var Serializer
38
     */
39
    private $serializer;
40
41
    /**
42
     * Client constructor.
43
     *
44
     * @param string|null        $cacheDir
45
     * @param \GuzzleHttp\Client $httpClient
46
     */
47 7
    public function __construct(
48
        string $cacheDir = null,
49
        \GuzzleHttp\Client $httpClient = null
50
    ) {
51 7
        $serializerBuilder = SerializerBuilder::create()
52 7
            ->addMetadataDir(__DIR__.'/Resource/config/serializer');
53
54 7
        if ($cacheDir) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cacheDir of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
55 1
            $serializerBuilder->setCacheDir($cacheDir);
56
        }
57
58 7
        $this->apiVersion = 'v1';
59 7
        $this->serializer = $serializerBuilder->build();
60
61 7
        if ($httpClient === null) {
62 1
            $httpClient = new \GuzzleHttp\Client();
63
        }
64
65 7
        $this->httpClient = $httpClient;
66 7
    }
67
68
    /**
69
     * Get global stats
70
     *
71
     * @throws GuzzleException
72
     * @throws ResponseErrorException
73
     * @throws RateLimitExceededException
74
     *
75
     * @return GlobalStats
76
     */
77 1
    public function getGlobalStats(): GlobalStats
78
    {
79 1
        $response = $this->httpClient->request('GET', $this->getEndpointUrl('global'));
80
81 1
        return $this->deserializeResponse($response, GlobalStats::class);
82
    }
83
84
    /**
85
     * Get tickers
86
     *
87
     * @throws GuzzleException
88
     * @throws ResponseErrorException
89
     * @throws RateLimitExceededException
90
     *
91
     * @return array|Ticker[]
92
     */
93 1
    public function getTickers(): array
94
    {
95 1
        $response = $this->httpClient->request('GET', $this->getEndpointUrl('ticker'));
96
97 1
        return $this->deserializeResponse($response, sprintf('array<%s>', Ticker::class));
98
    }
99
100
    /**
101
     * Get coin`s ticker data
102
     *
103
     * @param string $id
104
     *
105
     * @throws GuzzleException
106
     * @throws ResponseErrorException
107
     * @throws RateLimitExceededException
108
     *
109
     * @return Ticker
110
     */
111 3 View Code Duplication
    public function getTickerByCoinId(string $id): Ticker
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...
112
    {
113 3
        $response = $this->httpClient->request(
114 3
            'GET',
115 3
            $this->getEndpointUrl(sprintf('ticker/%s', $id))
116
        );
117
118 3
        return $this->deserializeResponse($response, Ticker::class);
119
    }
120
121
    /**
122
     * Get coins list
123
     *
124
     * @throws GuzzleException
125
     * @throws ResponseErrorException
126
     * @throws RateLimitExceededException
127
     *
128
     * @return array|Coin[]
129
     */
130 1 View Code Duplication
    public function getCoins(): array
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...
131
    {
132 1
        $response = $this->httpClient->request('GET', $this->getEndpointUrl('coins'));
133
134 1
        return $this->deserializeResponse($response, sprintf('array<%s>', Coin::class));
135
136
    }
137
138
    /**
139
     * Get the endpoint URL.
140
     *
141
     * @param string $endpoint
142
     *
143
     * @return string
144
     */
145 6
    protected function getEndpointUrl(string $endpoint): string
146
    {
147 6
        return str_replace('%ver%', $this->getApiVersion(), static::BASE_URL).$endpoint;
148
    }
149
150
    /**
151
     * Get the API version
152
     *
153
     * @return string
154
     */
155 7
    public function getApiVersion(): string
156
    {
157 7
        return $this->apiVersion;
158
    }
159
160
    /**
161
     * Unmarshal JSON
162
     *
163
     * @param ResponseInterface $response
164
     * @param string            $type
165
     *
166
     * @throws ResponseErrorException
167
     * @throws RateLimitExceededException
168
     *
169
     * @return mixed
170
     */
171 6
    private function deserializeResponse(ResponseInterface $response, string $type)
172
    {
173 6
        $body = $response->getBody();
174
175
        // rate limit exceeded
176 6
        if ($response->getStatusCode() === 429) {
177 1
            throw new RateLimitExceededException('Response code from API 429. Rate limit exceeded.');
178
        }
179
180 5
        if (array_key_exists('error', $e = json_decode($body, true))) {
181 1
            throw new ResponseErrorException($e['error']);
182
        }
183
184 4
        return $this->serializer->deserialize($body, $type, 'json');
185
    }
186
}
187