Passed
Push — master ( 404cfa...4a2326 )
by Eugene
04:47 queued 02:50
created

ErrorExtension::pack()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
rs 9.9666
ccs 0
cts 10
cp 0
crap 12
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 12
    public function getType() : int
27
    {
28 12
        return self::TYPE;
29
    }
30
31
    public function pack(Packer $packer, $value) : ?string
32
    {
33
        if (!$value instanceof Error) {
34
            return null;
35
        }
36
37
        [Keys::ERROR_STACK => $errorStack] = $value->toMap();
38
39
        $packedError = $packer->packMapHeader(1);
40
        $packedError .= $packer->packInt(Keys::ERROR_STACK);
41
        $packedError .= $packer->packArrayHeader(\count($errorStack));
42
        foreach ($errorStack as $error) {
43
            $packedError .= $packer->packMap($error);
44
        }
45
46
        return $packer->packExt(self::TYPE, $packedError);
47
    }
48
49
    /**
50
     * @return Error
51
     */
52
    public function unpackExt(BufferUnpacker $unpacker, int $extLength)
53
    {
54
        return Error::fromMap($unpacker->unpackMap());
55
    }
56
}
57