Passed
Pull Request — master (#37)
by Eugene
09:14
created

IProto   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 5
eloc 31
dl 0
loc 47
ccs 9
cts 11
cp 0.8182
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A parseGreeting() 0 17 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Tarantool Client package.
7
 *
8
 * (c) Eugene Leonovich <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Tarantool\Client;
15
16
use Tarantool\Client\Exception\InvalidGreeting;
17
18
/**
19
 * @see https://www.tarantool.io/en/doc/2.1/dev_guide/internals/box_protocol/
20
 */
21
final class IProto
22
{
23
    public const CODE = 0x00;
24
    public const SYNC = 0x01;
25
    public const SCHEMA_ID = 0x05;
26
    public const SPACE_ID = 0x10;
27
    public const INDEX_ID = 0x11;
28
    public const LIMIT = 0x12;
29
    public const OFFSET = 0x13;
30
    public const ITERATOR = 0x14;
31
    public const KEY = 0x20;
32
    public const TUPLE = 0x21;
33
    public const FUNCTION_NAME = 0x22;
34
    public const USER_NAME = 0x23;
35
    public const EXPR = 0x27;
36
    public const OPERATIONS = 0x28;
37
    public const DATA = 0x30;
38
    public const METADATA = 0x32;
39
    public const ERROR = 0x31;
40
    public const SQL_TEXT = 0x40;
41
    public const SQL_BIND = 0x41;
42
    public const SQL_INFO = 0x42;
43
44
    public const GREETING_SIZE = 128;
45
    public const LENGTH_SIZE = 5;
46
47
    private function __construct()
48
    {
49
    }
50
51 256
    public static function parseGreeting(string $greeting) : string
52
    {
53 256
        if (0 !== \strpos($greeting, 'Tarantool')) {
54 42
            throw InvalidGreeting::invalidServerName();
55
        }
56
57 214
        if (false === $salt = \base64_decode(\substr($greeting, 64, 44), true)) {
58 8
            throw InvalidGreeting::invalidSalt();
59
        }
60
61 206
        $salt = \substr($salt, 0, 20);
62
63 206
        if (isset($salt[19])) {
64 194
            return $salt;
65
        }
66
67 12
        throw InvalidGreeting::invalidSalt();
68
    }
69
}
70