Completed
Pull Request — master (#9)
by Eugene
06:06
created

IProto::parseGreeting()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Tarantool;
4
5
use Tarantool\Exception\Exception;
6
7
abstract class IProto
8
{
9
    const CODE = 0x00;
10
    const SYNC = 0x01;
11
    const SPACE_ID = 0x10;
12
    const INDEX_ID = 0x11;
13
    const LIMIT = 0x12;
14
    const OFFSET = 0x13;
15
    const ITERATOR = 0x14;
16
    const KEY = 0x20;
17
    const TUPLE = 0x21;
18
    const FUNCTION_NAME = 0x22;
19
    const USER_NAME = 0x23;
20
    const SERVER_UUID = 0x24;
21
    const CLUSTER_UUID = 0x25;
22
    const VCLOCK = 0x26;
23
    const EXPR = 0x27;
24
    const DATA = 0x30;
25
    const ERROR = 0x31;
26
27
    const GREETING_SIZE = 128;
28 38
    const LENGTH_SIZE = 5;
29
30 38
    /**
31
     * @param string $greeting
32
     *
33
     * @return string A session salt
34
     *
35
     * @throws Exception
36
     */
37
    public static function parseGreeting($greeting)
38
    {
39
        if ('Tarantool' !== substr($greeting, 0, 9)) {
40
            throw new Exception('Invalid greeting: unable to recognize Tarantool server.');
41
        }
42
43
        $salt = substr(base64_decode(substr($greeting, 64, 44), true), 0, 20);
44
45
        if (isset($salt[19])) {
46
            return $salt;
47
        }
48
49
        throw new Exception('Invalid greeting: unable to parse salt.');
50
    }
51
}
52