Completed
Pull Request — master (#37)
by Eugene
03:12
created

IProto   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 51
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 3 1
A parseGreeting() 0 18 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\Exception;
17
18
final class IProto
19
{
20
    public const CODE = 0x00;
21
    public const SYNC = 0x01;
22
    public const SPACE_ID = 0x10;
23
    public const INDEX_ID = 0x11;
24
    public const LIMIT = 0x12;
25
    public const OFFSET = 0x13;
26
    public const ITERATOR = 0x14;
27
    public const KEY = 0x20;
28
    public const TUPLE = 0x21;
29
    public const FUNCTION_NAME = 0x22;
30
    public const USER_NAME = 0x23;
31
    public const SERVER_UUID = 0x24;
32
    public const CLUSTER_UUID = 0x25;
33
    public const VCLOCK = 0x26;
34
    public const EXPR = 0x27;
35
    public const OPERATIONS = 0x28;
36
    public const DATA = 0x30;
37
    public const METADATA = 0x32;
38
    public const ERROR = 0x31;
39
    public const SQL_TEXT = 0x40;
40
    public const SQL_BIND = 0x41;
41
    public const SQL_INFO = 0x42;
42
43
    public const GREETING_SIZE = 128;
44
    public const LENGTH_SIZE = 5;
45
46
    private function __construct()
47
    {
48
    }
49
50 137
    public static function parseGreeting(string $greeting) : string
51
    {
52 137
        if (0 !== \strpos($greeting, 'Tarantool')) {
53 21
            throw new Exception('Invalid greeting: unable to recognize Tarantool server.');
54
        }
55
56 116
        if (false === $salt = \base64_decode(\substr($greeting, 64, 44), true)) {
57 4
            throw new Exception('Invalid greeting: unable to parse salt.');
58
        }
59
60 112
        $salt = \substr($salt, 0, 20);
61
62 112
        if (isset($salt[19])) {
63 106
            return $salt;
64
        }
65
66 6
        throw new Exception('Invalid greeting: unable to parse salt.');
67
    }
68
}
69