Passed
Pull Request — master (#54)
by Eugene
05:24
created

Response   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 73.68%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 15
c 2
b 0
f 0
dl 0
loc 47
ccs 14
cts 19
cp 0.7368
rs 10

7 Methods

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