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

Response   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 63.16%

Importance

Changes 0
Metric Value
wmc 8
eloc 16
dl 0
loc 48
rs 10
c 0
b 0
f 0
ccs 12
cts 19
cp 0.6316

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isError() 0 5 1
A getCode() 0 3 1
A tryGetBodyField() 0 3 1
A getBodyField() 0 7 2
A getSchemaId() 0 3 1
A getSync() 0 3 1
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