Passed
Pull Request — master (#62)
by Eugene
12:23 queued 08:14
created

PeclPacker::unpack()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.3035

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 24
ccs 11
cts 15
cp 0.7332
rs 9.8666
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4.3035
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;
15
16
use Tarantool\Client\Keys;
17
use Tarantool\Client\Request\Request;
18
use Tarantool\Client\Response;
19
20
final class PeclPacker implements Packer
21
{
22
    private $phpOnly;
23
    private $packer;
24
25 138
    public function __construct(bool $phpOnly = true)
26
    {
27 138
        $this->phpOnly = $phpOnly;
28 138
        $this->packer = new \MessagePack($phpOnly);
29 138
    }
30
31 114
    public function pack(Request $request, int $sync) : string
32
    {
33
        // @see https://github.com/msgpack/msgpack-php/issues/45
34 114
        $packet = \pack('C*', 0x82, Keys::CODE, $request->getType(), Keys::SYNC).
35 114
            $this->packer->pack($sync).
36 114
            $this->packer->pack($request->getBody());
37
38 114
        return PacketLength::pack(\strlen($packet)).$packet;
39
    }
40
41 99
    public function unpack(string $data) : Response
42
    {
43
        // @see https://github.com/msgpack/msgpack-php/issues/139
44
        // $unpacker = clone $this->unpacker;
45 99
        $unpacker = new \MessagePackUnpacker($this->phpOnly);
0 ignored issues
show
Bug introduced by
The type MessagePackUnpacker was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46 99
        $unpacker->feed($data);
47
48 99
        if (!$unpacker->execute()) {
49 2
            throw new \UnexpectedValueException('Unable to unpack response header.');
50
        }
51
52 97
        return new Response($unpacker->data(),
53
            static function () use ($unpacker) {
54 92
                if (!$unpacker->execute()) {
55 1
                    throw new \UnexpectedValueException('Unable to unpack response body.');
56
                }
57
58 91
                $body = $unpacker->data();
59
60
                // with PHP_ONLY = true an empty array
61
                // will be unpacked to stdClass
62 91
                return $body instanceof \stdClass
63
                    ? (array) $body
64 91
                    : $body;
65 97
            }
66
        );
67
    }
68
}
69