@@ -8,119 +8,119 @@ |
||
| 8 | 8 | |
| 9 | 9 | class GuzzleHttpClient implements HttpClient |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var Client |
|
| 13 | - */ |
|
| 14 | - protected $client; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - protected $responseHeaders; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @param Client $client |
|
| 23 | - */ |
|
| 24 | - public function __construct(Client $client) |
|
| 25 | - { |
|
| 26 | - $this->client = $client; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param string $uri |
|
| 31 | - * @param array $params |
|
| 32 | - * @param array $headers |
|
| 33 | - * @return array |
|
| 34 | - */ |
|
| 35 | - public function get($uri, array $params = [], array $headers = []) |
|
| 36 | - { |
|
| 37 | - if (!empty($params)) { |
|
| 38 | - $uri .= '?' . http_build_query($params); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - $response = $this |
|
| 42 | - ->client |
|
| 43 | - ->get($uri, ['headers' => $headers]); |
|
| 44 | - |
|
| 45 | - return $this->parseResponse($response); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param string $uri |
|
| 50 | - * @param array $body |
|
| 51 | - * @param array $headers |
|
| 52 | - * @return array |
|
| 53 | - */ |
|
| 54 | - public function post($uri, array $body = [], array $headers = []) |
|
| 55 | - { |
|
| 56 | - $response = $this |
|
| 57 | - ->client |
|
| 58 | - ->post( |
|
| 59 | - $uri, [ |
|
| 60 | - 'headers' => $headers, |
|
| 61 | - 'form_params' => $body, |
|
| 62 | - ] |
|
| 63 | - ); |
|
| 64 | - |
|
| 65 | - return $this->parseResponse($response); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param string $uri |
|
| 70 | - * @param array $body |
|
| 71 | - * @param array $headers |
|
| 72 | - * @return mixed |
|
| 73 | - */ |
|
| 74 | - public function put($uri, array $body = [], array $headers = []) |
|
| 75 | - { |
|
| 76 | - $response = $this |
|
| 77 | - ->client |
|
| 78 | - ->put( |
|
| 79 | - $uri, [ |
|
| 80 | - 'headers' => $headers, |
|
| 81 | - 'form_params' => $body, |
|
| 82 | - ] |
|
| 83 | - ); |
|
| 84 | - |
|
| 85 | - return $this->parseResponse($response); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param string $uri |
|
| 90 | - * @param array $body |
|
| 91 | - * @param array $headers |
|
| 92 | - * @return mixed |
|
| 93 | - */ |
|
| 94 | - public function delete($uri, array $body = [], array $headers = []) |
|
| 95 | - { |
|
| 96 | - $response = $this |
|
| 97 | - ->client |
|
| 98 | - ->delete($uri, [ |
|
| 99 | - 'headers' => $headers, |
|
| 100 | - 'form_params' => $body, |
|
| 101 | - ]); |
|
| 102 | - |
|
| 103 | - return $this->parseResponse($response); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @param ResponseInterface $response |
|
| 108 | - * @return array|null |
|
| 109 | - */ |
|
| 110 | - protected function parseResponse(ResponseInterface $response) |
|
| 111 | - { |
|
| 112 | - $responseContents = $response->getBody()->getContents(); |
|
| 113 | - |
|
| 114 | - $this->responseHeaders = $response->getHeaders(); |
|
| 115 | - |
|
| 116 | - return json_decode($responseContents, true); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - public function getResponseHeaders() |
|
| 123 | - { |
|
| 124 | - return $this->responseHeaders; |
|
| 125 | - } |
|
| 11 | + /** |
|
| 12 | + * @var Client |
|
| 13 | + */ |
|
| 14 | + protected $client; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + protected $responseHeaders; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @param Client $client |
|
| 23 | + */ |
|
| 24 | + public function __construct(Client $client) |
|
| 25 | + { |
|
| 26 | + $this->client = $client; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param string $uri |
|
| 31 | + * @param array $params |
|
| 32 | + * @param array $headers |
|
| 33 | + * @return array |
|
| 34 | + */ |
|
| 35 | + public function get($uri, array $params = [], array $headers = []) |
|
| 36 | + { |
|
| 37 | + if (!empty($params)) { |
|
| 38 | + $uri .= '?' . http_build_query($params); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + $response = $this |
|
| 42 | + ->client |
|
| 43 | + ->get($uri, ['headers' => $headers]); |
|
| 44 | + |
|
| 45 | + return $this->parseResponse($response); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param string $uri |
|
| 50 | + * @param array $body |
|
| 51 | + * @param array $headers |
|
| 52 | + * @return array |
|
| 53 | + */ |
|
| 54 | + public function post($uri, array $body = [], array $headers = []) |
|
| 55 | + { |
|
| 56 | + $response = $this |
|
| 57 | + ->client |
|
| 58 | + ->post( |
|
| 59 | + $uri, [ |
|
| 60 | + 'headers' => $headers, |
|
| 61 | + 'form_params' => $body, |
|
| 62 | + ] |
|
| 63 | + ); |
|
| 64 | + |
|
| 65 | + return $this->parseResponse($response); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param string $uri |
|
| 70 | + * @param array $body |
|
| 71 | + * @param array $headers |
|
| 72 | + * @return mixed |
|
| 73 | + */ |
|
| 74 | + public function put($uri, array $body = [], array $headers = []) |
|
| 75 | + { |
|
| 76 | + $response = $this |
|
| 77 | + ->client |
|
| 78 | + ->put( |
|
| 79 | + $uri, [ |
|
| 80 | + 'headers' => $headers, |
|
| 81 | + 'form_params' => $body, |
|
| 82 | + ] |
|
| 83 | + ); |
|
| 84 | + |
|
| 85 | + return $this->parseResponse($response); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param string $uri |
|
| 90 | + * @param array $body |
|
| 91 | + * @param array $headers |
|
| 92 | + * @return mixed |
|
| 93 | + */ |
|
| 94 | + public function delete($uri, array $body = [], array $headers = []) |
|
| 95 | + { |
|
| 96 | + $response = $this |
|
| 97 | + ->client |
|
| 98 | + ->delete($uri, [ |
|
| 99 | + 'headers' => $headers, |
|
| 100 | + 'form_params' => $body, |
|
| 101 | + ]); |
|
| 102 | + |
|
| 103 | + return $this->parseResponse($response); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @param ResponseInterface $response |
|
| 108 | + * @return array|null |
|
| 109 | + */ |
|
| 110 | + protected function parseResponse(ResponseInterface $response) |
|
| 111 | + { |
|
| 112 | + $responseContents = $response->getBody()->getContents(); |
|
| 113 | + |
|
| 114 | + $this->responseHeaders = $response->getHeaders(); |
|
| 115 | + |
|
| 116 | + return json_decode($responseContents, true); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + public function getResponseHeaders() |
|
| 123 | + { |
|
| 124 | + return $this->responseHeaders; |
|
| 125 | + } |
|
| 126 | 126 | } |
@@ -4,29 +4,29 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Widgets extends CrudEndpoint |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @param string $itemId |
|
| 9 | - * @param string|null $collectionId |
|
| 10 | - * @return mixed |
|
| 11 | - */ |
|
| 12 | - public function delete($itemId, $collectionId = null) |
|
| 13 | - { |
|
| 14 | - $attributes = $collectionId ? ['collectionId' => $collectionId] : []; |
|
| 7 | + /** |
|
| 8 | + * @param string $itemId |
|
| 9 | + * @param string|null $collectionId |
|
| 10 | + * @return mixed |
|
| 11 | + */ |
|
| 12 | + public function delete($itemId, $collectionId = null) |
|
| 13 | + { |
|
| 14 | + $attributes = $collectionId ? ['collectionId' => $collectionId] : []; |
|
| 15 | 15 | |
| 16 | - return $this |
|
| 17 | - ->http |
|
| 18 | - ->delete( |
|
| 19 | - $this->makeRequestUrl($itemId), |
|
| 20 | - $attributes, |
|
| 21 | - $this->headers() |
|
| 22 | - ); |
|
| 23 | - } |
|
| 16 | + return $this |
|
| 17 | + ->http |
|
| 18 | + ->delete( |
|
| 19 | + $this->makeRequestUrl($itemId), |
|
| 20 | + $attributes, |
|
| 21 | + $this->headers() |
|
| 22 | + ); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * {@inheritdoc} |
|
| 27 | - */ |
|
| 28 | - public function endpoint() |
|
| 29 | - { |
|
| 30 | - return 'widgets'; |
|
| 31 | - } |
|
| 25 | + /** |
|
| 26 | + * {@inheritdoc} |
|
| 27 | + */ |
|
| 28 | + public function endpoint() |
|
| 29 | + { |
|
| 30 | + return 'widgets'; |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -4,52 +4,52 @@ |
||
| 4 | 4 | |
| 5 | 5 | abstract class CrudEndpoint extends Endpoint |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @param array $attributes |
|
| 9 | - * @return array |
|
| 10 | - */ |
|
| 11 | - public function create(array $attributes) |
|
| 12 | - { |
|
| 13 | - return $this |
|
| 14 | - ->http |
|
| 15 | - ->post( |
|
| 16 | - $this->makeRequestUrl(), |
|
| 17 | - $attributes, |
|
| 18 | - $this->headers() |
|
| 19 | - ); |
|
| 20 | - } |
|
| 7 | + /** |
|
| 8 | + * @param array $attributes |
|
| 9 | + * @return array |
|
| 10 | + */ |
|
| 11 | + public function create(array $attributes) |
|
| 12 | + { |
|
| 13 | + return $this |
|
| 14 | + ->http |
|
| 15 | + ->post( |
|
| 16 | + $this->makeRequestUrl(), |
|
| 17 | + $attributes, |
|
| 18 | + $this->headers() |
|
| 19 | + ); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @param string $itemId |
|
| 24 | - * @param array $attributes |
|
| 25 | - * @return mixed |
|
| 26 | - */ |
|
| 27 | - public function update($itemId, array $attributes) |
|
| 28 | - { |
|
| 29 | - return $this |
|
| 30 | - ->http |
|
| 31 | - ->put( |
|
| 32 | - $this->makeRequestUrl($itemId), |
|
| 33 | - $attributes, |
|
| 34 | - $this->headers() |
|
| 35 | - ); |
|
| 36 | - } |
|
| 22 | + /** |
|
| 23 | + * @param string $itemId |
|
| 24 | + * @param array $attributes |
|
| 25 | + * @return mixed |
|
| 26 | + */ |
|
| 27 | + public function update($itemId, array $attributes) |
|
| 28 | + { |
|
| 29 | + return $this |
|
| 30 | + ->http |
|
| 31 | + ->put( |
|
| 32 | + $this->makeRequestUrl($itemId), |
|
| 33 | + $attributes, |
|
| 34 | + $this->headers() |
|
| 35 | + ); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @param string $itemId |
|
| 40 | - * @param bool $everywhere |
|
| 41 | - * @return mixed |
|
| 42 | - */ |
|
| 43 | - public function delete($itemId, $everywhere = false) |
|
| 44 | - { |
|
| 45 | - $params = $everywhere ? ['everywhere' => $everywhere] : []; |
|
| 38 | + /** |
|
| 39 | + * @param string $itemId |
|
| 40 | + * @param bool $everywhere |
|
| 41 | + * @return mixed |
|
| 42 | + */ |
|
| 43 | + public function delete($itemId, $everywhere = false) |
|
| 44 | + { |
|
| 45 | + $params = $everywhere ? ['everywhere' => $everywhere] : []; |
|
| 46 | 46 | |
| 47 | - return $this |
|
| 48 | - ->http |
|
| 49 | - ->delete( |
|
| 50 | - $this->makeRequestUrl($itemId), |
|
| 51 | - $params, |
|
| 52 | - $this->headers() |
|
| 53 | - ); |
|
| 54 | - } |
|
| 47 | + return $this |
|
| 48 | + ->http |
|
| 49 | + ->delete( |
|
| 50 | + $this->makeRequestUrl($itemId), |
|
| 51 | + $params, |
|
| 52 | + $this->headers() |
|
| 53 | + ); |
|
| 54 | + } |
|
| 55 | 55 | } |
@@ -5,30 +5,30 @@ |
||
| 5 | 5 | |
| 6 | 6 | class Organizations extends CrudEndpoint |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * @param string $id |
|
| 10 | - * @return array |
|
| 11 | - */ |
|
| 12 | - public function getById($id) |
|
| 13 | - { |
|
| 14 | - return $this->withOrganization($id)->getById($id); |
|
| 15 | - } |
|
| 8 | + /** |
|
| 9 | + * @param string $id |
|
| 10 | + * @return array |
|
| 11 | + */ |
|
| 12 | + public function getById($id) |
|
| 13 | + { |
|
| 14 | + return $this->withOrganization($id)->getById($id); |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @param string $itemId |
|
| 19 | - * @param array $attributes |
|
| 20 | - * @return mixed |
|
| 21 | - */ |
|
| 22 | - public function update($itemId, array $attributes) |
|
| 23 | - { |
|
| 24 | - return $this->withOrganization($itemId)->update($itemId, $attributes); |
|
| 25 | - } |
|
| 17 | + /** |
|
| 18 | + * @param string $itemId |
|
| 19 | + * @param array $attributes |
|
| 20 | + * @return mixed |
|
| 21 | + */ |
|
| 22 | + public function update($itemId, array $attributes) |
|
| 23 | + { |
|
| 24 | + return $this->withOrganization($itemId)->update($itemId, $attributes); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * {@inheritdoc} |
|
| 29 | - */ |
|
| 30 | - public function endpoint() |
|
| 31 | - { |
|
| 32 | - return 'organizations'; |
|
| 33 | - } |
|
| 27 | + /** |
|
| 28 | + * {@inheritdoc} |
|
| 29 | + */ |
|
| 30 | + public function endpoint() |
|
| 31 | + { |
|
| 32 | + return 'organizations'; |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -6,97 +6,97 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class Endpoint |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var array |
|
| 11 | - */ |
|
| 12 | - protected $rateLimitInfo; |
|
| 9 | + /** |
|
| 10 | + * @var array |
|
| 11 | + */ |
|
| 12 | + protected $rateLimitInfo; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @var array |
|
| 16 | - */ |
|
| 17 | - protected $headers = []; |
|
| 14 | + /** |
|
| 15 | + * @var array |
|
| 16 | + */ |
|
| 17 | + protected $headers = []; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var HttpClient |
|
| 21 | - */ |
|
| 22 | - protected $http; |
|
| 19 | + /** |
|
| 20 | + * @var HttpClient |
|
| 21 | + */ |
|
| 22 | + protected $http; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $organizationId; |
|
| 24 | + /** |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $organizationId; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @param HttpClient $http |
|
| 31 | - */ |
|
| 32 | - public function __construct(HttpClient $http) |
|
| 33 | - { |
|
| 34 | - $this->http = $http; |
|
| 35 | - } |
|
| 29 | + /** |
|
| 30 | + * @param HttpClient $http |
|
| 31 | + */ |
|
| 32 | + public function __construct(HttpClient $http) |
|
| 33 | + { |
|
| 34 | + $this->http = $http; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @param string $verb |
|
| 39 | - * @return string |
|
| 40 | - */ |
|
| 41 | - public function makeRequestUrl($verb = '') |
|
| 42 | - { |
|
| 43 | - return "https://favro.com/api/v1/{$this->endpoint()}/$verb"; |
|
| 44 | - } |
|
| 37 | + /** |
|
| 38 | + * @param string $verb |
|
| 39 | + * @return string |
|
| 40 | + */ |
|
| 41 | + public function makeRequestUrl($verb = '') |
|
| 42 | + { |
|
| 43 | + return "https://favro.com/api/v1/{$this->endpoint()}/$verb"; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @param array $params |
|
| 48 | - * @return array |
|
| 49 | - */ |
|
| 50 | - public function getAll(array $params = []) |
|
| 51 | - { |
|
| 52 | - return $this |
|
| 53 | - ->http |
|
| 54 | - ->get( |
|
| 55 | - $this->makeRequestUrl(), |
|
| 56 | - $params, |
|
| 57 | - $this->headers() |
|
| 58 | - ); |
|
| 59 | - } |
|
| 46 | + /** |
|
| 47 | + * @param array $params |
|
| 48 | + * @return array |
|
| 49 | + */ |
|
| 50 | + public function getAll(array $params = []) |
|
| 51 | + { |
|
| 52 | + return $this |
|
| 53 | + ->http |
|
| 54 | + ->get( |
|
| 55 | + $this->makeRequestUrl(), |
|
| 56 | + $params, |
|
| 57 | + $this->headers() |
|
| 58 | + ); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @param string $id |
|
| 63 | - * @return array |
|
| 64 | - */ |
|
| 65 | - public function getById($id) |
|
| 66 | - { |
|
| 67 | - return $this |
|
| 68 | - ->http |
|
| 69 | - ->get( |
|
| 70 | - $this->makeRequestUrl($id), |
|
| 71 | - [], |
|
| 72 | - $this->headers() |
|
| 73 | - ); |
|
| 74 | - } |
|
| 61 | + /** |
|
| 62 | + * @param string $id |
|
| 63 | + * @return array |
|
| 64 | + */ |
|
| 65 | + public function getById($id) |
|
| 66 | + { |
|
| 67 | + return $this |
|
| 68 | + ->http |
|
| 69 | + ->get( |
|
| 70 | + $this->makeRequestUrl($id), |
|
| 71 | + [], |
|
| 72 | + $this->headers() |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @return array |
|
| 78 | - */ |
|
| 79 | - protected function headers() |
|
| 80 | - { |
|
| 81 | - return array_merge( |
|
| 82 | - ['organizationId' => $this->organizationId], |
|
| 83 | - $this->headers |
|
| 84 | - ); |
|
| 85 | - } |
|
| 76 | + /** |
|
| 77 | + * @return array |
|
| 78 | + */ |
|
| 79 | + protected function headers() |
|
| 80 | + { |
|
| 81 | + return array_merge( |
|
| 82 | + ['organizationId' => $this->organizationId], |
|
| 83 | + $this->headers |
|
| 84 | + ); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * @param string $organizationId |
|
| 89 | - * @return $this |
|
| 90 | - */ |
|
| 91 | - public function withOrganization($organizationId) |
|
| 92 | - { |
|
| 93 | - $this->organizationId = $organizationId; |
|
| 87 | + /** |
|
| 88 | + * @param string $organizationId |
|
| 89 | + * @return $this |
|
| 90 | + */ |
|
| 91 | + public function withOrganization($organizationId) |
|
| 92 | + { |
|
| 93 | + $this->organizationId = $organizationId; |
|
| 94 | 94 | |
| 95 | - return $this; |
|
| 96 | - } |
|
| 95 | + return $this; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * @return string |
|
| 100 | - */ |
|
| 101 | - abstract public function endpoint(); |
|
| 98 | + /** |
|
| 99 | + * @return string |
|
| 100 | + */ |
|
| 101 | + abstract public function endpoint(); |
|
| 102 | 102 | } |
@@ -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 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | */ |
| 77 | 77 | public function useOrganization($organizationName) |
| 78 | 78 | { |
| 79 | - if($organization = $this->getOrganizationByName($organizationName)) { |
|
| 79 | + if ($organization = $this->getOrganizationByName($organizationName)) { |
|
| 80 | 80 | $this->organizationId = $organization['organizationId']; |
| 81 | 81 | } |
| 82 | 82 | |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | { |
| 110 | 110 | $alias = strtolower($alias); |
| 111 | 111 | |
| 112 | - if(isset($this->endpoints[$alias])) { |
|
| 112 | + if (isset($this->endpoints[$alias])) { |
|
| 113 | 113 | return $this->endpoints[$alias]; |
| 114 | 114 | } |
| 115 | 115 | |