Response::getHeaders()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CodeblogPro\GeoCoder\Http;
6
7
use CodeblogPro\GeoCoder\Exceptions\InvalidArgumentException;
8
9
class Response implements ResponseInterface
10
{
11
    private int $statusCode;
12
    private string $body;
13
    private array $headers;
14
15
    public function __construct(int $statusCode, string $body, array $headers = [])
16
    {
17
        $this->setStatusCodeIsValid($statusCode);
18
        $this->setBody($body);
19
        $this->setHeadersIsValid($headers);
20
    }
21
22
    public function getStatusCode(): int
23
    {
24
        return $this->statusCode;
25
    }
26
27
    public function getBody(): string
28
    {
29
        return $this->body;
30
    }
31
32
    public function getHeaders(): array
33
    {
34
        return $this->headers;
35
    }
36
37
    private function setStatusCodeIsValid(int $statusCode): void
38
    {
39
        $statusCode = (int)$statusCode;
40
41
        if ($statusCode < 100 || $statusCode > 599) {
42
            throw new InvalidArgumentException('Invalid status code.');
43
        }
44
45
        $this->statusCode = $statusCode;
46
    }
47
48
    private function setBody(string $body): void
49
    {
50
        $this->body = $body;
51
    }
52
53
    private function setHeadersIsValid(array $headers): void
54
    {
55
        if (!is_array($headers)) {
0 ignored issues
show
introduced by
The condition is_array($headers) is always true.
Loading history...
56
            throw new InvalidArgumentException('Headers must be an array type.');
57
        }
58
59
        $this->headers = $headers;
60
    }
61
}
62