1
|
|
|
<?php namespace Vindi\Exceptions; |
2
|
|
|
|
3
|
|
|
use Exception; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class RateLimitException |
7
|
|
|
* |
8
|
|
|
* @package Vindi\Exceptions |
9
|
|
|
*/ |
10
|
|
|
class RateLimitException extends Exception |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
protected $limit; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
protected $reset; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $remaining; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $retryAfter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* RateLimitException constructor. |
34
|
|
|
* |
35
|
|
|
* @param mixed $response API response. |
36
|
|
|
*/ |
37
|
4 |
|
public function __construct($response) |
38
|
|
|
{ |
39
|
4 |
|
$this->limit = $this->getHeaderValue($response, 'Rate-Limit-Limit'); |
40
|
4 |
|
$this->reset = $this->getHeaderValue($response, 'Rate-Limit-Reset'); |
41
|
4 |
|
$this->remaining = $this->getHeaderValue($response, 'Rate-Limit-Remaining'); |
42
|
4 |
|
$this->retryAfter = $this->getHeaderValue($response, 'Retry-After'); |
43
|
|
|
|
44
|
4 |
|
$this->message = "O limite de {$this->limit} requisições por minuto para a API foi atingido."; |
45
|
4 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get first array value from specified header as integer. |
49
|
|
|
* |
50
|
|
|
* @param mixed $response API response. |
51
|
|
|
* @param string $name Header name. |
52
|
|
|
* |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
4 |
|
private function getHeaderValue($response, $name) |
56
|
|
|
{ |
57
|
4 |
|
$header = $response->getHeader($name); |
58
|
|
|
|
59
|
4 |
|
if (! $header || ! count($header)) { |
60
|
2 |
|
return 0; |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
return (int) array_shift($header); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Maximum request number per minute. |
68
|
|
|
* |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
2 |
|
public function getLimit() |
72
|
|
|
{ |
73
|
2 |
|
return $this->limit; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Timestamp until there are new remaining requests. |
78
|
|
|
* |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
2 |
|
public function getReset() |
82
|
|
|
{ |
83
|
2 |
|
return $this->reset; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Remaining requests. |
88
|
|
|
* |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
2 |
|
public function getRemaining() |
92
|
|
|
{ |
93
|
2 |
|
return $this->remaining; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* When it's possible to make the next request. |
98
|
|
|
* |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
2 |
|
public function getRetryAfter() |
102
|
|
|
{ |
103
|
2 |
|
return $this->retryAfter; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|