Completed
Pull Request — master (#37)
by Eugene
02:54
created

RawResponse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 0
dl 0
loc 43
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0

5 Methods

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