Passed
Push — master ( cc4b9d...31cf14 )
by Eugene
04:20 queued 02:17
created

ErrorExtension::unpackExt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
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\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