|
2 | 2 |
|
3 | 3 | namespace Github\Api; |
4 | 4 |
|
| 5 | +use Github\Api\RateLimit\RateLimitResource; |
| 6 | + |
5 | 7 | /** |
6 | 8 | * Get rate limits. |
7 | 9 | * |
|
12 | 14 | class RateLimit extends AbstractApi |
13 | 15 | { |
14 | 16 | /** |
15 | | - * Get rate limits. |
| 17 | + * @var RateLimitResource[] |
| 18 | + */ |
| 19 | + protected $resources = []; |
| 20 | + |
| 21 | + /** |
| 22 | + * Get rate limits data in an array. |
| 23 | + * |
| 24 | + * @deprecated since 2.11.0 Use `->getResources()` instead |
16 | 25 | * |
17 | 26 | * @return array |
18 | 27 | */ |
19 | 28 | public function getRateLimits() |
20 | 29 | { |
21 | | - return $this->get('/rate_limit'); |
| 30 | + return $this->fetchLimits(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Gets the rate limit resource objects. |
| 35 | + * |
| 36 | + * @return RateLimitResource[] |
| 37 | + */ |
| 38 | + public function getResources() |
| 39 | + { |
| 40 | + $this->fetchLimits(); |
| 41 | + |
| 42 | + return $this->resources; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns a rate limit resource object by the given name. |
| 47 | + * |
| 48 | + * @param string $name |
| 49 | + * |
| 50 | + * @return RateLimitResource|false |
| 51 | + */ |
| 52 | + public function getResource($name) |
| 53 | + { |
| 54 | + // Fetch once per instance |
| 55 | + if (empty($this->resources)) { |
| 56 | + $this->fetchLimits(); |
| 57 | + } |
| 58 | + |
| 59 | + if (!isset($this->resources[$name])) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + return $this->resources[$name]; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns the data directly from the GitHub API endpoint. |
| 68 | + * |
| 69 | + * @return array |
| 70 | + */ |
| 71 | + protected function fetchLimits() |
| 72 | + { |
| 73 | + $result = $this->get('/rate_limit') ?: []; |
| 74 | + |
| 75 | + // Assemble Limit instances |
| 76 | + foreach ($result['resources'] as $resourceName => $resource) { |
| 77 | + $this->resources[$resourceName] = new RateLimitResource($resourceName, $resource); |
| 78 | + } |
| 79 | + |
| 80 | + return $result; |
22 | 81 | } |
23 | 82 |
|
24 | 83 | /** |
25 | 84 | * Get core rate limit. |
26 | 85 | * |
| 86 | + * @deprecated since 2.11.0 Use `->getResource('core')->getLimit()` instead |
| 87 | + * |
27 | 88 | * @return int |
28 | 89 | */ |
29 | 90 | public function getCoreLimit() |
30 | 91 | { |
31 | | - $response = $this->getRateLimits(); |
32 | | - |
33 | | - return $response['resources']['core']['limit']; |
| 92 | + return $this->getResource('core')->getLimit(); |
34 | 93 | } |
35 | 94 |
|
36 | 95 | /** |
37 | 96 | * Get search rate limit. |
38 | 97 | * |
| 98 | + * @deprecated since 2.11.0 Use `->getResource('core')->getLimit()` instead |
| 99 | + * |
39 | 100 | * @return int |
40 | 101 | */ |
41 | 102 | public function getSearchLimit() |
42 | 103 | { |
43 | | - $response = $this->getRateLimits(); |
44 | | - |
45 | | - return $response['resources']['search']['limit']; |
| 104 | + return $this->getResource('search')->getLimit(); |
46 | 105 | } |
47 | 106 | } |
0 commit comments