Completed
Pull Request — master (#37)
by Eugene
03:22
created

Response::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Tarantool Client package.
7
 *
8
 * (c) Eugene Leonovich <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Tarantool\Client;
15
16
final class Response
17
{
18
    public const TYPE_ERROR = 0x8000;
19
20
    private $header;
21
    private $body;
22
23 90
    public function __construct(array $header, array $body)
24
    {
25 90
        $this->header = $header;
26 90
        $this->body = $body;
27 90
    }
28
29 88
    public function isError() : bool
30
    {
31 88
        $code = $this->header[IProto::CODE];
32
33 88
        return $code >= self::TYPE_ERROR;
34
    }
35
36 6
    public function getHeaderField(int $code)
37
    {
38 6
        if (!isset($this->header[$code])) {
39
            throw new \InvalidArgumentException(\sprintf('Invalid header code 0x%x.', $code));
40
        }
41
42 6
        return $this->header[$code];
43
    }
44
45 84
    public function getBodyField(int $code)
46
    {
47 84
        if (!isset($this->body[$code])) {
48
            throw new \InvalidArgumentException(\sprintf('Invalid body code 0x%x.', $code));
49
        }
50
51 84
        return $this->body[$code];
52
    }
53
54
    public function tryGetBodyField(int $code, $default = null)
55
    {
56
        return $this->body[$code] ?? $default;
57
    }
58
}
59