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

PurePacker::unpack()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.108

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 7
cts 10
cp 0.7
rs 10
c 0
b 0
f 0
cc 2
nc 3
nop 1
crap 2.108
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 MessagePack\BufferUnpacker;
17
use MessagePack\Exception\UnpackingFailedException;
18
use MessagePack\Packer;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tarantool\Client\Packer\Packer. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
19
use Tarantool\Client\Exception\UnpackingFailed;
20
use Tarantool\Client\IProto;
21
use Tarantool\Client\Packer\Packer as ClientPacker;
22
use Tarantool\Client\Request\Request;
23
use Tarantool\Client\Response;
24
25
final class PurePacker implements ClientPacker
26
{
27
    private $packer;
28
    private $unpacker;
29
30 131
    public function __construct(?Packer $packer = null, ?BufferUnpacker $unpacker = null)
31
    {
32 131
        $this->packer = $packer ?: new Packer();
33 131
        $this->unpacker = $unpacker ?: new BufferUnpacker();
34 131
    }
35
36 106
    public function pack(Request $request, ?int $sync = null) : string
37
    {
38 106
        $content = $this->packer->packMapHeader(2).
39 106
            $this->packer->packInt(IProto::CODE).
40 106
            $this->packer->packInt($request->getType()).
41 106
            $this->packer->packInt(IProto::SYNC).
42 106
            $this->packer->packInt($sync ?: 0).
43 106
            $this->packer->packMap($request->getBody());
44
45 106
        return PackUtils::packLength(\strlen($content)).$content;
46
    }
47
48 105
    public function unpack(string $data) : Response
49
    {
50
        try {
51 105
            $this->unpacker->reset($data);
52
53 105
            return new Response(
54 105
                $this->unpacker->unpackMap(),
55 103
                $this->unpacker->unpackMap()
56
            );
57 2
        } catch (UnpackingFailedException $e) {
58 2
            throw new UnpackingFailed('Unable to unpack response.', 0, $e);
59
        }
60
    }
61
}
62