Passed
Pull Request — master (#62)
by Eugene
03:08
created

Response::isError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 $bodyGetter;
22
    private $body;
23
24 216
    public function __construct(array $header, \Closure $bodyGetter)
25
    {
26 216
        $this->header = $header;
27 216
        $this->bodyGetter = $bodyGetter;
28 216
        unset($this->body);
29 216
    }
30
31 188
    public function isError() : bool
32
    {
33 188
        $code = $this->header[Keys::CODE];
34
35 188
        return $code >= self::TYPE_ERROR;
36
    }
37
38 30
    public function getCode() : int
39
    {
40 30
        return $this->header[Keys::CODE];
41
    }
42
43 190
    public function getSync() : int
44
    {
45 190
        return $this->header[Keys::SYNC];
46
    }
47
48
    public function getSchemaId() : int
49
    {
50
        return $this->header[Keys::SCHEMA_ID];
51
    }
52
53 182
    public function getBodyField(int $key)
54
    {
55 182
        if (!isset($this->body[$key])) {
56
            throw new \OutOfRangeException(\sprintf('Invalid body key 0x%x.', $key));
57
        }
58
59 182
        return $this->body[$key];
60
    }
61
62 2
    public function tryGetBodyField(int $key, $default = null)
63
    {
64 2
        return $this->body[$key] ?? $default;
65
    }
66
67 184
    public function __get(string $propertyName)
68
    {
69 184
        return 'body' === $propertyName
70 184
            ? $this->body = ($this->bodyGetter)()
71 182
            : $this->$propertyName;
72
    }
73
}
74