Passed
Push — master ( 904859...a463be )
by Stefano
02:09
created
src/BEditaClient.php 1 patch
Spacing   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -57,7 +57,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 
@@ -179,7 +179,7 @@  discard block
 block discarded – undo
179 179
      */
180 180
     public function getResponseBody()
181 181
     {
182
-        return $this->response ? json_decode((string)$this->response->getBody(), true) : null;
182
+        return $this->response ? json_decode((string) $this->response->getBody(), true) : null;
183 183
     }
184 184
 
185 185
     /**
@@ -193,7 +193,7 @@  discard block
 block discarded – undo
193 193
     {
194 194
         $body = json_encode(compact('username', 'password'));
195 195
 
196
-        return $this->post('/auth', $body, ['Content-Type' => 'application/json']);
196
+        return $this->post('/auth', $body, [ 'Content-Type' => 'application/json' ]);
197 197
     }
198 198
 
199 199
     /**
@@ -299,8 +299,8 @@  discard block
 block discarded – undo
299 299
     {
300 300
         $id = null;
301 301
         if (array_key_exists('id', $data)) {
302
-            $id = $data['id'];
303
-            unset($data['id']);
302
+            $id = $data[ 'id' ];
303
+            unset($data[ 'id' ]);
304 304
         }
305 305
 
306 306
         $body = [
@@ -309,11 +309,11 @@  discard block
 block discarded – undo
309 309
                 'attributes' => $data,
310 310
             ],
311 311
         ];
312
-        $headers['Content-Type'] = 'application/vnd.api+json';
312
+        $headers[ 'Content-Type' ] = 'application/vnd.api+json';
313 313
         if (!$id) {
314 314
             return $this->post(sprintf('/%s', $type), json_encode($body), $headers);
315 315
         }
316
-        $body['data']['id'] = $id;
316
+        $body[ 'data' ][ 'id' ] = $id;
317 317
 
318 318
         return $this->patch(sprintf('/%s/%s', $type, $id), json_encode($body), $headers);
319 319
     }
@@ -349,7 +349,7 @@  discard block
 block discarded – undo
349 349
      */
350 350
     public function schema(string $type)
351 351
     {
352
-        $h = ['Accept' => 'application/schema+json'];
352
+        $h = [ 'Accept' => 'application/schema+json' ];
353 353
 
354 354
         return $this->get(sprintf('/model/schema/%s', $type), null, $h);
355 355
     }
@@ -369,7 +369,7 @@  discard block
 block discarded – undo
369 369
                 'type' => $type,
370 370
             ],
371 371
         ];
372
-        $headers['Content-Type'] = 'application/json';
372
+        $headers[ 'Content-Type' ] = 'application/json';
373 373
 
374 374
         return $this->patch(sprintf('/%s/%s', 'trash', $id), json_encode($body), $headers);
375 375
     }
@@ -436,14 +436,14 @@  discard block
 block discarded – undo
436 436
         } catch (BEditaClientException $e) {
437 437
             // Handle error.
438 438
             $attributes = $e->getAttributes();
439
-            if ($e->getCode() !== 401 || empty($attributes['code']) || $attributes['code'] !== 'be_token_expired') {
439
+            if ($e->getCode() !== 401 || empty($attributes[ 'code' ]) || $attributes[ 'code' ] !== 'be_token_expired') {
440 440
                 // Not an expired token's fault.
441 441
                 throw $e;
442 442
             }
443 443
 
444 444
             // Refresh and retry.
445 445
             $this->refreshTokens();
446
-            unset($headers['Authorization']);
446
+            unset($headers[ 'Authorization' ]);
447 447
 
448 448
             return $this->sendRequest($method, $path, $query, $headers, $body);
449 449
         }
@@ -463,11 +463,11 @@  discard block
 block discarded – undo
463 463
     protected function sendRequest(string $method, string $path, ?array $query = null, ?array $headers = null, $body = null) : ResponseInterface
464 464
     {
465 465
         $uri = new Uri($this->apiBaseUrl);
466
-        $uri = $uri->withPath($uri->getPath() . '/' . $path);
466
+        $uri = $uri->withPath($uri->getPath().'/'.$path);
467 467
         if ($query) {
468
-            $uri = $uri->withQuery(http_build_query((array)$query));
468
+            $uri = $uri->withQuery(http_build_query((array) $query));
469 469
         }
470
-        $headers = array_merge($this->defaultHeaders, (array)$headers);
470
+        $headers = array_merge($this->defaultHeaders, (array) $headers);
471 471
 
472 472
         // Send the request synchronously to retrieve the response.
473 473
         $this->response = $this->jsonApiClient->sendRequest(new Request($method, $uri, $headers, $body));
@@ -476,13 +476,13 @@  discard block
 block discarded – undo
476 476
             $statusCode = $this->getStatusCode();
477 477
             $response = $this->getResponseBody();
478 478
 
479
-            $code = (string)$statusCode;
479
+            $code = (string) $statusCode;
480 480
             $reason = $this->getStatusMessage();
481
-            if (!empty($response['error']['code'])) {
482
-                $code = $response['error']['code'];
481
+            if (!empty($response[ 'error' ][ 'code' ])) {
482
+                $code = $response[ 'error' ][ 'code' ];
483 483
             }
484
-            if (!empty($response['error']['title'])) {
485
-                $reason = $response['error']['title'];
484
+            if (!empty($response[ 'error' ][ 'title' ])) {
485
+                $reason = $response[ 'error' ][ 'title' ];
486 486
             }
487 487
 
488 488
             throw new BEditaClientException(compact('code', 'reason'), $statusCode);
@@ -503,20 +503,20 @@  discard block
 block discarded – undo
503 503
      */
504 504
     public function refreshTokens() : void
505 505
     {
506
-        if (empty($this->tokens['renew'])) {
506
+        if (empty($this->tokens[ 'renew' ])) {
507 507
             throw new \BadMethodCallException(__('You must be logged in to renew token'));
508 508
         }
509 509
 
510 510
         $headers = [
511
-            'Authorization' => sprintf('Bearer %s', $this->tokens['renew']),
511
+            'Authorization' => sprintf('Bearer %s', $this->tokens[ 'renew' ]),
512 512
         ];
513 513
 
514
-        $this->sendRequest('POST', '/auth', [], $headers);
514
+        $this->sendRequest('POST', '/auth', [ ], $headers);
515 515
         $body = $this->getResponseBody();
516
-        if (empty($body['meta']['jwt'])) {
516
+        if (empty($body[ 'meta' ][ 'jwt' ])) {
517 517
             throw new BEditaClientException(__('Invalid response from server'));
518 518
         }
519 519
 
520
-        $this->setupTokens($body['meta']);
520
+        $this->setupTokens($body[ 'meta' ]);
521 521
     }
522 522
 }
Please login to merge, or discard this patch.
src/BEditaClientException.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -22,7 +22,7 @@
 block discarded – undo
22 22
      *
23 23
      * @var array
24 24
      */
25
-    protected $attributes = [];
25
+    protected $attributes = [ ];
26 26
 
27 27
     /**
28 28
      * Template string that has attributes sprintf()'ed into it.
Please login to merge, or discard this patch.