Passed
Push — master ( da4196...4ec9fc )
by Eugene
18:03 queued 08:05
created

Greeting::getServerVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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;
15
16
use Tarantool\Client\Exception\InvalidGreeting;
17
18
final class Greeting
19
{
20
    public const SIZE_BYTES = 128;
21
22
    private $greeting;
23
    private $salt;
24
    private $serverVersion;
25
26 260
    private function __construct($greeting)
27
    {
28 260
        $this->greeting = $greeting;
29 42
    }
30
31
    public static function parse(string $greeting) : self
32 218
    {
33 8
        if (0 === \strpos($greeting, 'Tarantool')) {
34
            return new self($greeting);
35
        }
36 210
37
        throw InvalidGreeting::invalidServerName();
38 210
    }
39 198
40
    public function getSalt() : string
41
    {
42 12
        if (null !== $this->salt) {
43
            return $this->salt;
44
        }
45
46
        if (false === $salt = \base64_decode(\substr($this->greeting, 64, 44), true)) {
47
            throw InvalidGreeting::invalidSalt();
48
        }
49
50
        $salt = \substr($salt, 0, 20);
51
52
        if (isset($salt[19])) {
53
            return $this->salt = $salt;
54
        }
55
56
        throw InvalidGreeting::invalidSalt();
57
    }
58
59
    public function getServerVersion() : int
60
    {
61
        if (null !== $this->serverVersion) {
62
            return $this->serverVersion;
63
        }
64
65
        [$major, $minor, $patch] = \sscanf($this->greeting, 'Tarantool %d.%d.%d');
66
67
        return $this->serverVersion = $major * 10000 + $minor * 100 + $patch;
68
    }
69
}
70