Passed
Push — master ( 1f87bb...62bbca )
by Eugene
03:04
created

Greeting::getSalt()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Connection;
15
16
final class Greeting
17
{
18
    public const SIZE_BYTES = 128;
19
20
    private $greeting;
21
    private $salt;
22
    private $serverVersion;
23
24 218
    private function __construct($greeting)
25
    {
26 218
        $this->greeting = $greeting;
27 218
    }
28
29 238
    public static function parse(string $greeting) : self
30
    {
31 238
        if (0 === \strpos($greeting, 'Tarantool')) {
32 218
            return new self($greeting);
33
        }
34
35 20
        throw new \UnexpectedValueException('Unable to recognize Tarantool server.');
36
    }
37
38 22
    public function getSalt() : string
39
    {
40 22
        if (null !== $this->salt) {
41 2
            return $this->salt;
42
        }
43
44 22
        if (false === $salt = \base64_decode(\substr($this->greeting, 64, 44), true)) {
45 4
            throw new \UnexpectedValueException('Unable to decode salt.');
46
        }
47
48 18
        $salt = \substr($salt, 0, 20);
49
50 18
        if (isset($salt[19])) {
51 12
            return $this->salt = $salt;
52
        }
53
54 6
        throw new \UnexpectedValueException('Salt is too short.');
55
    }
56
57 2
    public function getServerVersion() : int
58
    {
59 2
        if (null !== $this->serverVersion) {
60
            return $this->serverVersion;
61
        }
62
63 2
        [$major, $minor, $patch] = \sscanf($this->greeting, 'Tarantool %d.%d.%d');
64
65 2
        return $this->serverVersion = $major * 10000 + $minor * 100 + $patch;
66
    }
67
}
68