Passed
Push — master ( 7cf5b4...231b8d )
by Eugene
05:38
created

PacketLength::unpack()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 3
rs 10
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 262
    public static function pack(int $length) : string
25
    {
26 262
        return \pack('CN', 0xce, $length);
27
    }
28
29 228
    public static function unpack(string $data) : int
30
    {
31 228
        if (!isset($data[4]) || "\xce" !== $data[0]) {
32 2
            throw new \RuntimeException('Unable to unpack packet length.');
33
        }
34
35 226
        return \ord($data[1]) << 24
36 226
            | \ord($data[2]) << 16
37 226
            | \ord($data[3]) << 8
38 226
            | \ord($data[4]);
39
    }
40
}
41