Passed
Push — master ( 793cce...b0cf56 )
by Eugene
07:19
created

PurePacker::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

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
c 1
b 0
f 0
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 MessagePack\BufferUnpacker;
17
use MessagePack\Extension;
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 MessagePack\PackOptions;
20
use MessagePack\UnpackOptions;
21
use Symfony\Component\Uid\Uuid;
22
use Tarantool\Client\Keys;
23
use Tarantool\Client\Packer\Extension\DecimalExtension;
24
use Tarantool\Client\Packer\Extension\ErrorExtension;
25
use Tarantool\Client\Packer\Extension\UuidExtension;
26
use Tarantool\Client\Packer\Packer as ClientPacker;
27
use Tarantool\Client\Request\Request;
28
use Tarantool\Client\Response;
29
30
final class PurePacker implements ClientPacker
31
{
32
    /** @var Packer */
33
    private $packer;
34
35
    /** @var BufferUnpacker */
36
    private $unpacker;
37
38 402
    public function __construct(?Packer $packer = null, ?BufferUnpacker $unpacker = null)
39
    {
40 402
        $this->packer = $packer ?: new Packer(PackOptions::FORCE_STR);
41 402
        $this->unpacker = $unpacker ?: new BufferUnpacker();
42
    }
43
44 48
    public static function fromExtensions(Extension $extension, Extension ...$extensions) : self
45
    {
46 48
        $extensions = [-1 => $extension] + $extensions;
47
48 48
        return new self(
49 48
            new Packer(PackOptions::FORCE_STR, $extensions),
50 48
            new BufferUnpacker('', \extension_loaded('decimal') ? UnpackOptions::BIGINT_AS_DEC : null, $extensions)
51
        );
52
    }
53
54 14
    public static function fromAvailableExtensions() : self
55
    {
56 14
        $extensions = [new ErrorExtension()];
57 14
        if (\class_exists(Uuid::class)) {
58 14
            $extensions[] = new UuidExtension();
59
        }
60 14
        if (\extension_loaded('decimal')) {
61 14
            $extensions[] = new DecimalExtension();
62
63 14
            return new self(
64 14
                new Packer(PackOptions::FORCE_STR, $extensions),
65 14
                new BufferUnpacker('', UnpackOptions::BIGINT_AS_DEC, $extensions)
66
            );
67
        }
68
69
        return new self(
70
            new Packer(PackOptions::FORCE_STR, $extensions),
71
            new BufferUnpacker('', null, $extensions)
72
        );
73
    }
74
75 328
    public function pack(Request $request, int $sync) : string
76
    {
77
        // Hot path optimization
78 328
        $packet = \pack('C*', 0x82, Keys::CODE, $request->getType(), Keys::SYNC).
79 328
            $this->packer->packInt($sync).
80 328
            $this->packer->packMap($request->getBody());
81
82 328
        return PacketLength::pack(\strlen($packet)).$packet;
83
    }
84
85 302
    public function unpack(string $packet) : Response
86
    {
87 302
        $this->unpacker->reset($packet);
88
89 302
        return new Response(
90 302
            $this->unpacker->unpackMap(),
91 296
            $this->unpacker->unpackMap()
92
        );
93
    }
94
}
95