Completed
Pull Request — master (#37)
by Eugene
08:53
created

IProto::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 71
    public const ERROR = 0x31;
39
    public const SQL_TEXT = 0x40;
40 71
    public const SQL_BIND = 0x41;
41 21
    public const SQL_INFO = 0x42;
42
43
    public const GREETING_SIZE = 128;
44 50
    public const LENGTH_SIZE = 5;
45
46 50
    private function __construct()
47 40
    {
48
    }
49
50 10
    public static function parseGreeting(string $greeting) : string
51
    {
52
        if (0 !== \strpos($greeting, 'Tarantool')) {
53
            throw new Exception('Invalid greeting: unable to recognize Tarantool server.');
54
        }
55
56
        if (false === $salt = \base64_decode(\substr($greeting, 64, 44), true)) {
57
            throw new Exception('Invalid greeting: unable to parse salt.');
58
        }
59
60
        $salt = \substr($salt, 0, 20);
61
62
        if (isset($salt[19])) {
63
            return $salt;
64
        }
65
66
        throw new Exception('Invalid greeting: unable to parse salt.');
67
    }
68
}
69