PurePacker::fromAvailableExtensions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

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