Passed
Push — master ( 1f87bb...62bbca )
by Eugene
03:04
created

PeclPacker::pack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1.0046

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
rs 10
cc 1
nc 1
nop 2
crap 1.0046
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 $packer;
23
    private $unpacker;
24
25 138
    public function __construct(bool $phpOnly = true)
26
    {
27 138
        $this->packer = new \MessagePack($phpOnly);
28 138
        $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...
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 $packet) : Response
42
    {
43 99
        $this->unpacker->feed($packet);
44
45 99
        if (!$this->unpacker->execute()) {
46 2
            throw new \UnexpectedValueException('Unable to unpack response header.');
47
        }
48 97
        $header = $this->unpacker->data();
49
50 97
        if (!$this->unpacker->execute()) {
51 1
            throw new \UnexpectedValueException('Unable to unpack response body.');
52
        }
53 96
        $body = $this->unpacker->data();
54
55
        // with PHP_ONLY = true an empty array
56
        // will be unpacked to stdClass
57 96
        if ($body instanceof \stdClass) {
58 9
            $body = (array) $body;
59
        }
60
61 96
        return new Response($header, $body);
62
    }
63
}
64