Completed
Pull Request — master (#37)
by Eugene
03:12
created

Response   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 0
dl 0
loc 43
ccs 13
cts 17
cp 0.7647
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;
15
16
final class Response
17
{
18
    public const TYPE_ERROR = 0x8000;
19
20
    private $header;
21
    private $body;
22
23 101
    public function __construct(array $header, array $body)
24
    {
25 101
        $this->header = $header;
26 101
        $this->body = $body;
27 101
    }
28
29 101
    public function isError() : bool
30
    {
31 101
        $code = $this->header[IProto::CODE];
32
33 101
        return $code >= self::TYPE_ERROR;
34
    }
35
36 20
    public function getHeaderField(int $code)
37
    {
38 20
        if (!isset($this->header[$code])) {
39
            throw new \InvalidArgumentException(\sprintf('Invalid header code 0x%x.', $code));
40
        }
41
42 20
        return $this->header[$code];
43
    }
44
45 101
    public function getBodyField(int $code)
46
    {
47 101
        if (!isset($this->body[$code])) {
48
            throw new \InvalidArgumentException(\sprintf('Invalid body code 0x%x.', $code));
49
        }
50
51 101
        return $this->body[$code];
52
    }
53
54
    public function tryGetBodyField(int $code, $default = null)
55
    {
56
        return $this->body[$code] ?? $default;
57
    }
58
}
59