Passed
Push — master ( 4a2326...ddc22b )
by Eugene
02:08
created

ErrorExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 33
ccs 13
cts 14
cp 0.9286
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A pack() 0 16 3
A unpackExt() 0 3 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\Packer\Extension;
15
16
use MessagePack\BufferUnpacker;
17
use MessagePack\Extension;
18
use MessagePack\Packer;
19
use Tarantool\Client\Error;
20
use Tarantool\Client\Keys;
21
22
final class ErrorExtension implements Extension
23
{
24
    private const TYPE = 3;
25
26 17
    public function getType() : int
27
    {
28 17
        return self::TYPE;
29
    }
30
31 1
    public function pack(Packer $packer, $value) : ?string
32
    {
33 1
        if (!$value instanceof Error) {
34
            return null;
35
        }
36
37 1
        [Keys::ERROR_STACK => $errorStack] = $value->toMap();
38
39 1
        $packedError = $packer->packMapHeader(1);
40 1
        $packedError .= $packer->packInt(Keys::ERROR_STACK);
41 1
        $packedError .= $packer->packArrayHeader(\count($errorStack));
42 1
        foreach ($errorStack as $error) {
43 1
            $packedError .= $packer->packMap($error);
44
        }
45
46 1
        return $packer->packExt(self::TYPE, $packedError);
47
    }
48
49
    /**
50
     * @return Error
51
     */
52 1
    public function unpackExt(BufferUnpacker $unpacker, int $extLength)
53
    {
54 1
        return Error::fromMap($unpacker->unpackMap());
55
    }
56
}
57