Response::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
namespace Loevgaard\Linkmobility\Response;
3
4
use Assert\Assert;
5
6
abstract class Response implements ResponseInterface
7
{
8
    /**
9
     * @var array
10
     */
11
    protected $data;
12
13
    /**
14
     * @var int
15
     */
16
    protected $status;
17
18 2
    public function __construct(array $data)
19
    {
20 2
        Assert::that($data)->keyExists('status');
21
22 2
        $this->data = $data;
23 2
        $this->status = (int)$this->data['status'];
24
25 2
        if ($this->isSuccessful()) {
26 1
            $this->init();
27
        }
28 2
    }
29
30
    /**
31
     * @return bool
32
     */
33 2
    public function isSuccessful(): bool
34
    {
35 2
        return $this->status >= 200 && $this->status < 300;
36
    }
37
38 1
    public function getError(): string
39
    {
40 1
        return $this->isSuccessful() ? '' : $this->data['message'];
41
    }
42
}
43