Passed
Push — master ( 1fc8aa...45e169 )
by Anton
02:50
created

ApiResponse::__construct()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 30
ccs 22
cts 22
cp 1
rs 8.6346
cc 7
nc 9
nop 1
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CdekSDK2\Http;
6
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Class ApiResponse
11
 * @package CdekSDK2\Http
12
 */
13
class ApiResponse
14
{
15
    /**
16
     * Тело ответа
17
     * @var string
18
     */
19
    private $body;
20
21
    /**
22
     * Массив ошибок
23
     * @var array
24
     */
25
    private $errors = [];
26
27
    /**
28
     * HTTP-статус ответа
29
     * @var int
30
     */
31
    private $status;
32
33
    /**
34
     * ApiResponse constructor.
35
     * @param ResponseInterface|null $response
36
     */
37 34
    public function __construct(ResponseInterface $response = null)
38
    {
39 34
        if ($response === null) {
40 4
            $this->status = 500;
41 4
            $this->body = '';
42
43 4
            $this->errors[] = [
44 4
                'code' => 'internal_error',
45 4
                'message' => 'Internal Server Error'
46 4
            ];
47
        } else {
48 30
            $this->status = $response->getStatusCode();
49
            if (
50 30
                $response->hasHeader('Content-type') &&
51 30
                strpos(implode(',', $response->getHeader('Content-type')), 'json') !== false
52
            ) {
53 25
                $this->body = (string)$response->getBody()->getContents();
54
            } else {
55 9
                $this->body = (string)$response->getBody();
56
            }
57
58 30
            if ($this->status > 299) {
59 16
                $decode_body = json_decode($this->body, true);
60 16
                if (isset($decode_body['error'])) {
61 2
                    $this->errors[] = [
62 2
                        'code' => $decode_body['error'],
63 2
                        'message' => $decode_body['error_description'] ?? $decode_body['message'] ?? 'unknown_error'
64 2
                    ];
65 14
                } elseif (isset($decode_body['errors'])) {
66 2
                    $this->errors = $decode_body['errors'];
67
                }
68
            }
69
        }
70
    }
71
72
    /**
73
     * Проверка корректности выполненного запроса
74
     * @return bool
75
     */
76 31
    public function isOk(): bool
77
    {
78 31
        return $this->status >= 200 && $this->status <= 299;
79
    }
80
81
    /**
82
     * Проверка наличия ошибок в запросе
83
     * @return bool
84
     */
85 6
    public function hasErrors(): bool
86
    {
87 6
        return count($this->errors) > 0;
88
    }
89
90
    /**
91
     * @return string
92
     */
93 28
    public function getBody(): string
94
    {
95 28
        return $this->body;
96
    }
97
98
    /**
99
     * @return array
100
     */
101 1
    public function getErrors(): array
102
    {
103 1
        return $this->errors;
104
    }
105
106
    /**
107
     * @return int
108
     */
109 4
    public function getStatus(): int
110
    {
111 4
        return $this->status;
112
    }
113
}
114