Passed
Push — master ( 9ad89a...0c5368 )
by Eugene
07:39
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 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
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 TYPE_ERROR = 0x8000;
19
20
    private $header;
21
    private $body;
22
23 381
    public function __construct(array $header, array $body)
24
    {
25 381
        $this->header = $header;
26 381
        $this->body = $body;
27 381
    }
28
29 324
    public function isError() : bool
30
    {
31 324
        $code = $this->header[Keys::CODE];
32
33 324
        return $code >= self::TYPE_ERROR;
34
    }
35
36 45
    public function getCode() : int
37
    {
38 45
        return $this->header[Keys::CODE];
39
    }
40
41 345
    public function getSync() : int
42
    {
43 345
        return $this->header[Keys::SYNC];
44
    }
45
46
    public function getSchemaId() : int
47
    {
48
        return $this->header[Keys::SCHEMA_ID];
49
    }
50
51 317
    public function getBodyField(int $key)
52
    {
53 317
        if (\array_key_exists($key, $this->body)) {
54
            return $this->body[$key];
55
        }
56
57 317
        throw new \OutOfRangeException(\sprintf('Invalid body key 0x%x.', $key));
58
    }
59
60
    public function tryGetBodyField(int $key, $default = null)
61
    {
62
        return \array_key_exists($key, $this->body) ? $this->body[$key] : $default;
63
    }
64
65
    public function hasBodyField(int $key) : bool
66
    {
67
        return \array_key_exists($key, $this->body);
68
    }
69
}
70