Passed
Branch php71 (acb31d)
by Eugene
02:53
created

PeclPacker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1.0156
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Tarantool Client package.
7
 *
8
 * (c) Eugene Leonovich <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Tarantool\Client\Packer;
15
16
use Tarantool\Client\Exception\UnpackingFailed;
17
use Tarantool\Client\IProto;
18
use Tarantool\Client\Request\Request;
19
use Tarantool\Client\Response;
20
21
final class PeclPacker implements Packer
22
{
23
    private $packer;
24
    private $unpacker;
25
26 131
    public function __construct(bool $phpOnly = true)
27
    {
28 131
        $this->packer = new \MessagePack($phpOnly);
29 131
        $this->unpacker = new \MessagePackUnpacker($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...
30 131
    }
31
32 106
    public function pack(Request $request, ?int $sync = null) : string
33
    {
34
        // @see https://github.com/msgpack/msgpack-php/issues/45
35 106
        $content = \pack('C*', 0x82, IProto::CODE, $request->getType(), IProto::SYNC).
36 106
            $this->packer->pack($sync ?: 0).
37 106
            $this->packer->pack($request->getBody());
38
39 106
        return PackUtils::packLength(\strlen($content)).$content;
40
    }
41
42 105
    public function unpack(string $data) : Response
43
    {
44 105
        $this->unpacker->feed($data);
45
46 105
        if (!$this->unpacker->execute()) {
47
            throw UnpackingFailed::invalidResponse();
48
        }
49
50 105
        $header = $this->unpacker->data();
51 105
        if (!\is_array($header)) {
52 2
            throw UnpackingFailed::invalidResponse();
53
        }
54
55 103
        if (!$this->unpacker->execute()) {
56
            throw UnpackingFailed::invalidResponse();
57
        }
58
59 103
        $body = $this->unpacker->data();
60 103
        if (\is_array($body)) {
61 101
            return new Response($header, $body);
62
        }
63 3
        if ($body instanceof \stdClass) {
64 3
            return new Response($header, (array) $body);
65
        }
66
67
        throw UnpackingFailed::invalidResponse();
68
    }
69
}
70