Passed
Push — master ( cc4b9d...31cf14 )
by Eugene
04:20 queued 02:17
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 16
    public function getType() : int
27
    {
28 16
        return self::TYPE;
29
    }
30
31 2
    public function pack(Packer $packer, $value) : ?string
32
    {
33 2
        if (!$value instanceof Error) {
34
            return null;
35
        }
36
37 2
        [Keys::ERROR_STACK => $errorStack] = $value->toMap();
38
39 2
        $packedError = $packer->packMapHeader(1);
40 2
        $packedError .= $packer->packInt(Keys::ERROR_STACK);
41 2
        $packedError .= $packer->packArrayHeader(\count($errorStack));
42 2
        foreach ($errorStack as $error) {
43 2
            $packedError .= $packer->packMap($error);
44
        }
45
46 2
        return $packer->packExt(self::TYPE, $packedError);
47
    }
48
49
    /**
50
     * @return Error
51
     */
52 2
    public function unpackExt(BufferUnpacker $unpacker, int $extLength)
53
    {
54 2
        return Error::fromMap($unpacker->unpackMap());
55
    }
56
}
57