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

PacketLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
final class PacketLength
17
{
18
    public const SIZE_BYTES = 5;
19
20
    private function __construct()
21
    {
22
    }
23
24 473
    public static function pack(int $length) : string
25
    {
26 473
        return \pack('CN', 0xce, $length);
27
    }
28
29 423
    public static function unpack(string $data) : int
30
    {
31 423
        if (!isset($data[4]) || "\xce" !== $data[0]) {
32 3
            throw new \RuntimeException('Unable to unpack packet length');
33
        }
34
35 420
        return \ord($data[1]) << 24
36 420
            | \ord($data[2]) << 16
37 420
            | \ord($data[3]) << 8
38 420
            | \ord($data[4]);
39
    }
40
}
41