Completed
Push — master ( 43ebed...079015 )
by Eugene
06:22
created

Response::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 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 432
    public function __construct(array $header, array $body)
24
    {
25 432
        $this->header = $header;
26 432
        $this->body = $body;
27 432
    }
28
29 366
    public function isError() : bool
30
    {
31 366
        $code = $this->header[Keys::CODE];
32
33 366
        return $code >= self::TYPE_ERROR;
34
    }
35
36 63
    public function getCode() : int
37
    {
38 63
        return $this->header[Keys::CODE];
39
    }
40
41 387
    public function getSync() : int
42
    {
43 387
        return $this->header[Keys::SYNC];
44
    }
45
46 3
    public function getSchemaId() : int
47
    {
48 3
        return $this->header[Keys::SCHEMA_ID];
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54 359
    public function getBodyField(int $key)
55
    {
56 359
        if (\array_key_exists($key, $this->body)) {
57 359
            return $this->body[$key];
58
        }
59
60
        throw new \OutOfRangeException(\sprintf('Invalid body key 0x%x', $key));
61
    }
62
63
    /**
64
     * @param mixed $default
65
     *
66
     * @return mixed
67
     */
68 12
    public function tryGetBodyField(int $key, $default = null)
69
    {
70 12
        return \array_key_exists($key, $this->body) ? $this->body[$key] : $default;
71
    }
72
73 6
    public function hasBodyField(int $key) : bool
74
    {
75 6
        return \array_key_exists($key, $this->body);
76
    }
77
}
78