ApiClient   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 11
c 1
b 0
f 0
dl 0
loc 28
ccs 12
cts 13
cp 0.9231
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processRequestBody() 0 3 1
A processErrorResponse() 0 6 3
A processResponse() 0 13 3
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the Bushido\ApiClient package.
4
 *
5
 * (c) Wojciech Nowicki <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.md
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Bushido\ApiClient;
12
13
use Bushido\ApiClient\Exceptions\ErrorResponseException;
14
use Bushido\ApiClient\Exceptions\WrongResponseException;
15
use Psr\Http\Message\RequestInterface;
16
use Psr\Http\Message\ResponseInterface;
17
18
class ApiClient extends AbstractApiClient
19
{
20 33
    protected function processRequestBody(array $body): array
21
    {
22 33
        return ['json' => $body];
23
    }
24
25 24
    protected function processResponse(ResponseInterface $response, RequestInterface $request): array
26
    {
27 24
        if ($response->getStatusCode() == 204) {
28 3
            return [];
29
        }
30
31 21
        $responseBody = json_decode($response->getBody()->getContents(), true);
32
33 21
        if (is_array($responseBody)) {
34 18
            return $responseBody;
35
        }
36
37 3
        throw new WrongResponseException('Response body was malformed JSON', $response->getStatusCode());
38
    }
39
40 3
    protected function processErrorResponse(ResponseInterface $response, RequestInterface $request): void
41
    {
42 3
        if (($responseBody = json_decode($response->getBody()->getContents(), true)) &&
43 3
            array_key_exists('message', $responseBody)
44
        ) {
45
            throw new ErrorResponseException($responseBody['message'], $response->getStatusCode());
46
        }
47 3
    }
48
}
49