Passed
Push — master ( de02d9...3b3597 )
by Eugene
03:38
created

Response::getBodyField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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