Completed
Push — master ( 7a9fd5...02d65c )
by Eugene
03:41
created

IProto::parseGreeting()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
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
    const LENGTH_SIZE = 5;
29
30
    /**
31
     * @param string $greeting
32
     *
33
     * @return string A session salt
34
     *
35
     * @throws Exception
36
     */
37 70
    public static function parseGreeting($greeting)
38
    {
39 70
        if ('Tarantool' !== substr($greeting, 0, 9)) {
40 21
            throw new Exception('Invalid greeting: unable to recognize Tarantool server.');
41
        }
42
43 49
        $salt = substr(base64_decode(substr($greeting, 64, 44), true), 0, 20);
44
45 49
        if (isset($salt[19])) {
46 39
            return $salt;
47
        }
48
49 10
        throw new Exception('Invalid greeting: unable to parse salt.');
50
    }
51
}
52