Completed
Push — master ( 0f75be...b86c40 )
by Eugene
08:39 queued 07:16
created

PurePacker::fromExtensions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.9666
c 0
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\Packer;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Tarantool\Client\Packer\Packer.

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...
18
use MessagePack\PackOptions;
19
use MessagePack\TypeTransformer\Extension;
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\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 385
    public function __construct(?Packer $packer = null, ?BufferUnpacker $unpacker = null)
38
    {
39 385
        $this->packer = $packer ?: new Packer(PackOptions::FORCE_STR);
40 385
        $this->unpacker = $unpacker ?: new BufferUnpacker();
41 385
    }
42
43 40
    public static function fromExtensions(Extension $extension, Extension ...$extensions) : self
44
    {
45 40
        $extensions = [-1 => $extension] + $extensions;
46
47 40
        return new self(
48 40
            new Packer(PackOptions::FORCE_STR, $extensions),
49 40
            new BufferUnpacker('', \extension_loaded('decimal') ? UnpackOptions::BIGINT_AS_DEC : null, $extensions)
50
        );
51
    }
52
53 14
    public static function fromAvailableExtensions() : self
54
    {
55 14
        $extensions = [];
56 14
        if (\class_exists(Uuid::class)) {
57 14
            $extensions[] = new UuidExtension();
58
        }
59 14
        if (\extension_loaded('decimal')) {
60 14
            $extensions[] = new DecimalExtension();
61
62 14
            return new self(
63 14
                new Packer(PackOptions::FORCE_STR, $extensions),
64 14
                new BufferUnpacker('', UnpackOptions::BIGINT_AS_DEC, $extensions)
65
            );
66
        }
67
68
        return new self(
69
            new Packer(PackOptions::FORCE_STR, $extensions),
70
            new BufferUnpacker('', null, $extensions)
71
        );
72
    }
73
74 315
    public function pack(Request $request, int $sync) : string
75
    {
76
        // hot path optimization
77 315
        $packet = \pack('C*', 0x82, Keys::CODE, $request->getType(), Keys::SYNC).
78 315
            $this->packer->packInt($sync).
79 315
            $this->packer->packMap($request->getBody());
80
81 315
        return PacketLength::pack(\strlen($packet)).$packet;
82
    }
83
84 288
    public function unpack(string $packet) : Response
85
    {
86 288
        $this->unpacker->reset($packet);
87
88 288
        return new Response(
89 288
            $this->unpacker->unpackMap(),
90 282
            $this->unpacker->unpackMap()
91
        );
92
    }
93
}
94