@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @var array |
| 59 | 59 | */ |
| 60 | - private $tokens = []; |
|
| 60 | + private $tokens = [ ]; |
|
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | 63 | * JSON API BEdita4 client |
@@ -77,16 +77,16 @@ discard block |
||
| 77 | 77 | * @param array $tokens JWT Autorization tokens as associative array ['jwt' => '###', 'renew' => '###'] |
| 78 | 78 | * @return void |
| 79 | 79 | */ |
| 80 | - public function __construct(string $apiUrl, ?string $apiKey = null, array $tokens = []) |
|
| 80 | + public function __construct(string $apiUrl, ?string $apiKey = null, array $tokens = [ ]) |
|
| 81 | 81 | { |
| 82 | 82 | $this->apiBaseUrl = $apiUrl; |
| 83 | 83 | $this->apiKey = $apiKey; |
| 84 | 84 | |
| 85 | - $this->defaultHeaders['X-Api-Key'] = $this->apiKey; |
|
| 85 | + $this->defaultHeaders[ 'X-Api-Key' ] = $this->apiKey; |
|
| 86 | 86 | $this->setupTokens($tokens); |
| 87 | 87 | |
| 88 | 88 | // setup an asynchronous JSON API client |
| 89 | - $guzzleClient = Client::createWithConfig([]); |
|
| 89 | + $guzzleClient = Client::createWithConfig([ ]); |
|
| 90 | 90 | $this->jsonApiClient = new JsonApiClient($guzzleClient); |
| 91 | 91 | } |
| 92 | 92 | |
@@ -99,10 +99,10 @@ discard block |
||
| 99 | 99 | public function setupTokens(array $tokens) : void |
| 100 | 100 | { |
| 101 | 101 | $this->tokens = $tokens; |
| 102 | - if (!empty($tokens['jwt'])) { |
|
| 103 | - $this->defaultHeaders['Authorization'] = sprintf('Bearer %s', $tokens['jwt']); |
|
| 102 | + if (!empty($tokens[ 'jwt' ])) { |
|
| 103 | + $this->defaultHeaders[ 'Authorization' ] = sprintf('Bearer %s', $tokens[ 'jwt' ]); |
|
| 104 | 104 | } else { |
| 105 | - unset($this->defaultHeaders['Authorization']); |
|
| 105 | + unset($this->defaultHeaders[ 'Authorization' ]); |
|
| 106 | 106 | } |
| 107 | 107 | } |
| 108 | 108 | |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | if (empty($this->response)) { |
| 183 | 183 | return null; |
| 184 | 184 | } |
| 185 | - $responseBody = json_decode((string)$this->response->getBody(), true); |
|
| 185 | + $responseBody = json_decode((string) $this->response->getBody(), true); |
|
| 186 | 186 | if (!is_array($responseBody)) { |
| 187 | 187 | return null; |
| 188 | 188 | } |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | { |
| 202 | 202 | $body = json_encode(compact('username', 'password')); |
| 203 | 203 | |
| 204 | - return $this->post('/auth', $body, ['Content-Type' => 'application/json']); |
|
| 204 | + return $this->post('/auth', $body, [ 'Content-Type' => 'application/json' ]); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -307,8 +307,8 @@ discard block |
||
| 307 | 307 | { |
| 308 | 308 | $id = null; |
| 309 | 309 | if (array_key_exists('id', $data)) { |
| 310 | - $id = $data['id']; |
|
| 311 | - unset($data['id']); |
|
| 310 | + $id = $data[ 'id' ]; |
|
| 311 | + unset($data[ 'id' ]); |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | $body = [ |
@@ -317,11 +317,11 @@ discard block |
||
| 317 | 317 | 'attributes' => $data, |
| 318 | 318 | ], |
| 319 | 319 | ]; |
| 320 | - $headers['Content-Type'] = 'application/vnd.api+json'; |
|
| 320 | + $headers[ 'Content-Type' ] = 'application/vnd.api+json'; |
|
| 321 | 321 | if (!$id) { |
| 322 | 322 | return $this->post(sprintf('/%s', $type), json_encode($body), $headers); |
| 323 | 323 | } |
| 324 | - $body['data']['id'] = $id; |
|
| 324 | + $body[ 'data' ][ 'id' ] = $id; |
|
| 325 | 325 | |
| 326 | 326 | return $this->patch(sprintf('/%s/%s', $type, $id), json_encode($body), $headers); |
| 327 | 327 | } |
@@ -357,7 +357,7 @@ discard block |
||
| 357 | 357 | */ |
| 358 | 358 | public function schema(string $type) : ?array |
| 359 | 359 | { |
| 360 | - $h = ['Accept' => 'application/schema+json']; |
|
| 360 | + $h = [ 'Accept' => 'application/schema+json' ]; |
|
| 361 | 361 | |
| 362 | 362 | return $this->get(sprintf('/model/schema/%s', $type), null, $h); |
| 363 | 363 | } |
@@ -377,7 +377,7 @@ discard block |
||
| 377 | 377 | 'type' => $type, |
| 378 | 378 | ], |
| 379 | 379 | ]; |
| 380 | - $headers = ['Content-Type' => 'application/json']; |
|
| 380 | + $headers = [ 'Content-Type' => 'application/json' ]; |
|
| 381 | 381 | |
| 382 | 382 | return $this->patch(sprintf('/%s/%s', 'trash', $id), json_encode($body), $headers); |
| 383 | 383 | } |
@@ -444,14 +444,14 @@ discard block |
||
| 444 | 444 | } catch (BEditaClientException $e) { |
| 445 | 445 | // Handle error. |
| 446 | 446 | $attributes = $e->getAttributes(); |
| 447 | - if ($e->getCode() !== 401 || empty($attributes['code']) || $attributes['code'] !== 'be_token_expired') { |
|
| 447 | + if ($e->getCode() !== 401 || empty($attributes[ 'code' ]) || $attributes[ 'code' ] !== 'be_token_expired') { |
|
| 448 | 448 | // Not an expired token's fault. |
| 449 | 449 | throw $e; |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | // Refresh and retry. |
| 453 | 453 | $this->refreshTokens(); |
| 454 | - unset($headers['Authorization']); |
|
| 454 | + unset($headers[ 'Authorization' ]); |
|
| 455 | 455 | |
| 456 | 456 | return $this->sendRequest($method, $path, $query, $headers, $body); |
| 457 | 457 | } |
@@ -471,11 +471,11 @@ discard block |
||
| 471 | 471 | protected function sendRequest(string $method, string $path, ?array $query = null, ?array $headers = null, $body = null) : ResponseInterface |
| 472 | 472 | { |
| 473 | 473 | $uri = new Uri($this->apiBaseUrl); |
| 474 | - $uri = $uri->withPath($uri->getPath() . '/' . $path); |
|
| 474 | + $uri = $uri->withPath($uri->getPath().'/'.$path); |
|
| 475 | 475 | if ($query) { |
| 476 | - $uri = $uri->withQuery(http_build_query((array)$query)); |
|
| 476 | + $uri = $uri->withQuery(http_build_query((array) $query)); |
|
| 477 | 477 | } |
| 478 | - $headers = array_merge($this->defaultHeaders, (array)$headers); |
|
| 478 | + $headers = array_merge($this->defaultHeaders, (array) $headers); |
|
| 479 | 479 | |
| 480 | 480 | // Send the request synchronously to retrieve the response. |
| 481 | 481 | $this->response = $this->jsonApiClient->sendRequest(new Request($method, $uri, $headers, $body)); |
@@ -484,13 +484,13 @@ discard block |
||
| 484 | 484 | $statusCode = $this->getStatusCode(); |
| 485 | 485 | $response = $this->getResponseBody(); |
| 486 | 486 | |
| 487 | - $code = (string)$statusCode; |
|
| 487 | + $code = (string) $statusCode; |
|
| 488 | 488 | $reason = $this->getStatusMessage(); |
| 489 | - if (!empty($response['error']['code'])) { |
|
| 490 | - $code = $response['error']['code']; |
|
| 489 | + if (!empty($response[ 'error' ][ 'code' ])) { |
|
| 490 | + $code = $response[ 'error' ][ 'code' ]; |
|
| 491 | 491 | } |
| 492 | - if (!empty($response['error']['title'])) { |
|
| 493 | - $reason = $response['error']['title']; |
|
| 492 | + if (!empty($response[ 'error' ][ 'title' ])) { |
|
| 493 | + $reason = $response[ 'error' ][ 'title' ]; |
|
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | throw new BEditaClientException(compact('code', 'reason'), $statusCode); |
@@ -511,20 +511,20 @@ discard block |
||
| 511 | 511 | */ |
| 512 | 512 | public function refreshTokens() : void |
| 513 | 513 | { |
| 514 | - if (empty($this->tokens['renew'])) { |
|
| 514 | + if (empty($this->tokens[ 'renew' ])) { |
|
| 515 | 515 | throw new \BadMethodCallException('You must be logged in to renew token'); |
| 516 | 516 | } |
| 517 | 517 | |
| 518 | 518 | $headers = [ |
| 519 | - 'Authorization' => sprintf('Bearer %s', $this->tokens['renew']), |
|
| 519 | + 'Authorization' => sprintf('Bearer %s', $this->tokens[ 'renew' ]), |
|
| 520 | 520 | ]; |
| 521 | 521 | |
| 522 | - $this->sendRequest('POST', '/auth', [], $headers); |
|
| 522 | + $this->sendRequest('POST', '/auth', [ ], $headers); |
|
| 523 | 523 | $body = $this->getResponseBody(); |
| 524 | - if (empty($body['meta']['jwt'])) { |
|
| 524 | + if (empty($body[ 'meta' ][ 'jwt' ])) { |
|
| 525 | 525 | throw new BEditaClientException('Invalid response from server'); |
| 526 | 526 | } |
| 527 | 527 | |
| 528 | - $this->setupTokens($body['meta']); |
|
| 528 | + $this->setupTokens($body[ 'meta' ]); |
|
| 529 | 529 | } |
| 530 | 530 | } |