@@ -31,102 +31,102 @@ |
||
31 | 31 | */ |
32 | 32 | class Api |
33 | 33 | { |
34 | - /** |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - private $organizationId; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var HttpClient |
|
41 | - */ |
|
42 | - private $http; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var Endpoint[] |
|
46 | - */ |
|
47 | - private $endpoints; |
|
48 | - |
|
49 | - public function __construct(HttpClient $http, Endpoint ...$endpoints) |
|
50 | - { |
|
51 | - $this->http = $http; |
|
52 | - foreach ($endpoints as $endpoint) { |
|
53 | - $this->endpoints[$endpoint->endpoint()] = $endpoint; |
|
54 | - } |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Magic method to access different endpoints. |
|
59 | - * |
|
60 | - * @param string $alias |
|
61 | - * |
|
62 | - * @return Endpoint |
|
63 | - * @throws WrongEndpoint |
|
64 | - */ |
|
65 | - public function __get($alias) |
|
66 | - { |
|
67 | - return $this |
|
68 | - ->resolveEndpoint($alias) |
|
69 | - ->withOrganization($this->organizationId); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * @param string $organizationName |
|
74 | - * @return $this |
|
75 | - * @throws WrongOrganizationName |
|
76 | - */ |
|
77 | - public function useOrganization($organizationName) |
|
78 | - { |
|
79 | - if ($organization = $this->getOrganizationByName($organizationName)) { |
|
80 | - $this->organizationId = $organization['organizationId']; |
|
81 | - } |
|
82 | - |
|
83 | - return $this; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @param string $organizationName |
|
88 | - * @return array|bool |
|
89 | - * @throws WrongOrganizationName |
|
90 | - */ |
|
91 | - private function getOrganizationByName($organizationName) |
|
92 | - { |
|
93 | - $organizations = $this->organizations->getAll(); |
|
94 | - foreach ($organizations['entities'] as $entity) { |
|
95 | - if ($entity['name'] === $organizationName) { |
|
96 | - return $entity; |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - throw new WrongOrganizationName("Organization $organizationName not found!"); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @param string $alias |
|
105 | - * @return Endpoint |
|
106 | - * @throws WrongEndpoint |
|
107 | - */ |
|
108 | - private function resolveEndpoint($alias) |
|
109 | - { |
|
110 | - $alias = strtolower($alias); |
|
111 | - |
|
112 | - if (isset($this->endpoints[$alias])) { |
|
113 | - return $this->endpoints[$alias]; |
|
114 | - } |
|
115 | - |
|
116 | - throw new WrongEndpoint("There is no endpoint called $alias."); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @return array |
|
121 | - */ |
|
122 | - public function getRateInfo() |
|
123 | - { |
|
124 | - $responseHeaders = $this->http->getResponseHeaders(); |
|
125 | - |
|
126 | - return [ |
|
127 | - 'reset' => $responseHeaders['X-RateLimit-Reset'][0], |
|
128 | - 'limit' => $responseHeaders['X-RateLimit-Limit'][0], |
|
129 | - 'remaining' => $responseHeaders['X-RateLimit-Remaining'][0], |
|
130 | - ]; |
|
131 | - } |
|
34 | + /** |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + private $organizationId; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var HttpClient |
|
41 | + */ |
|
42 | + private $http; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var Endpoint[] |
|
46 | + */ |
|
47 | + private $endpoints; |
|
48 | + |
|
49 | + public function __construct(HttpClient $http, Endpoint ...$endpoints) |
|
50 | + { |
|
51 | + $this->http = $http; |
|
52 | + foreach ($endpoints as $endpoint) { |
|
53 | + $this->endpoints[$endpoint->endpoint()] = $endpoint; |
|
54 | + } |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Magic method to access different endpoints. |
|
59 | + * |
|
60 | + * @param string $alias |
|
61 | + * |
|
62 | + * @return Endpoint |
|
63 | + * @throws WrongEndpoint |
|
64 | + */ |
|
65 | + public function __get($alias) |
|
66 | + { |
|
67 | + return $this |
|
68 | + ->resolveEndpoint($alias) |
|
69 | + ->withOrganization($this->organizationId); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * @param string $organizationName |
|
74 | + * @return $this |
|
75 | + * @throws WrongOrganizationName |
|
76 | + */ |
|
77 | + public function useOrganization($organizationName) |
|
78 | + { |
|
79 | + if ($organization = $this->getOrganizationByName($organizationName)) { |
|
80 | + $this->organizationId = $organization['organizationId']; |
|
81 | + } |
|
82 | + |
|
83 | + return $this; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @param string $organizationName |
|
88 | + * @return array|bool |
|
89 | + * @throws WrongOrganizationName |
|
90 | + */ |
|
91 | + private function getOrganizationByName($organizationName) |
|
92 | + { |
|
93 | + $organizations = $this->organizations->getAll(); |
|
94 | + foreach ($organizations['entities'] as $entity) { |
|
95 | + if ($entity['name'] === $organizationName) { |
|
96 | + return $entity; |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + throw new WrongOrganizationName("Organization $organizationName not found!"); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @param string $alias |
|
105 | + * @return Endpoint |
|
106 | + * @throws WrongEndpoint |
|
107 | + */ |
|
108 | + private function resolveEndpoint($alias) |
|
109 | + { |
|
110 | + $alias = strtolower($alias); |
|
111 | + |
|
112 | + if (isset($this->endpoints[$alias])) { |
|
113 | + return $this->endpoints[$alias]; |
|
114 | + } |
|
115 | + |
|
116 | + throw new WrongEndpoint("There is no endpoint called $alias."); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @return array |
|
121 | + */ |
|
122 | + public function getRateInfo() |
|
123 | + { |
|
124 | + $responseHeaders = $this->http->getResponseHeaders(); |
|
125 | + |
|
126 | + return [ |
|
127 | + 'reset' => $responseHeaders['X-RateLimit-Reset'][0], |
|
128 | + 'limit' => $responseHeaders['X-RateLimit-Limit'][0], |
|
129 | + 'remaining' => $responseHeaders['X-RateLimit-Remaining'][0], |
|
130 | + ]; |
|
131 | + } |
|
132 | 132 | } |