Passed
Push — master ( 47c498...8f00fc )
by Eugene
02:11
created

PeclPacker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 45
ccs 20
cts 20
cp 1
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pack() 0 8 1
A __construct() 0 4 1
A unpack() 0 21 4
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
    /** @var \MessagePack */
23
    private $packer;
24
25
    /** @var \MessagePackUnpacker */
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...
26
    private $unpacker;
27
28 151
    public function __construct(bool $phpOnly = true)
29
    {
30 151
        $this->packer = new \MessagePack($phpOnly);
31 151
        $this->unpacker = new \MessagePackUnpacker($phpOnly);
32 151
    }
33
34 119
    public function pack(Request $request, int $sync) : string
35
    {
36
        // @see https://github.com/msgpack/msgpack-php/issues/45
37 119
        $packet = \pack('C*', 0x82, Keys::CODE, $request->getType(), Keys::SYNC).
38 119
            $this->packer->pack($sync).
39 119
            $this->packer->pack($request->getBody());
40
41 119
        return PacketLength::pack(\strlen($packet)).$packet;
42
    }
43
44 110
    public function unpack(string $packet) : Response
45
    {
46 110
        $this->unpacker->feed($packet);
47
48 110
        if (!$this->unpacker->execute()) {
49 6
            throw new \RuntimeException('Unable to unpack response header.');
50
        }
51 104
        $header = $this->unpacker->data();
52
53 104
        if (!$this->unpacker->execute()) {
54 3
            throw new \RuntimeException('Unable to unpack response body.');
55
        }
56 101
        $body = $this->unpacker->data();
57
58
        // with PHP_ONLY = true an empty array
59
        // will be unpacked to stdClass
60 101
        if ($body instanceof \stdClass) {
61 15
            $body = (array) $body;
62
        }
63
64 101
        return new Response($header, $body);
65
    }
66
}
67