Passed
Pull Request — master (#37)
by Eugene
05:25
created

Response   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 15
dl 0
loc 47
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isError() 0 5 1
A __construct() 0 4 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
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 88
 */
13
14 88
namespace Tarantool\Client;
15 88
16 88
final class Response
17
{
18 5
    public const TYPE_ERROR = 0x8000;
19
20 5
    private $header;
21
    private $body;
22
23 73
    public function __construct(array $header, array $body)
24
    {
25 73
        $this->header = $header;
26
        $this->body = $body;
27
    }
28
29
    public function isError() : bool
30
    {
31
        $code = $this->header[IProto::CODE];
32
33
        return $code >= self::TYPE_ERROR;
34
    }
35
36
    public function getCode() : int
37
    {
38
        return $this->header[IProto::CODE];
39
    }
40
41
    public function getSync() : int
42
    {
43
        return $this->header[IProto::SYNC];
44
    }
45
46
    public function getSchemaId() : int
47
    {
48
        return $this->header[IProto::SCHEMA_ID];
49
    }
50
51
    public function getBodyField(int $code)
52
    {
53
        if (!isset($this->body[$code])) {
54
            throw new \OutOfBoundsException(\sprintf('Invalid body code 0x%x.', $code));
55
        }
56
57
        return $this->body[$code];
58
    }
59
60
    public function tryGetBodyField(int $code, $default = null)
61
    {
62
        return $this->body[$code] ?? $default;
63
    }
64
}
65