Passed
Pull Request — master (#47)
by Eugene
03:06
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;
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 232
    private function __construct($greeting)
27
    {
28 232
        $this->greeting = $greeting;
29 232
    }
30
31 252
    public static function parse(string $greeting) : self
32
    {
33 252
        if (0 === \strpos($greeting, 'Tarantool')) {
34 232
            return new self($greeting);
35
        }
36
37 20
        throw InvalidGreeting::invalidServerName();
38
    }
39
40 24
    public function getSalt() : string
41
    {
42 24
        if (null !== $this->salt) {
43 2
            return $this->salt;
44
        }
45
46 24
        if (false === $salt = \base64_decode(\substr($this->greeting, 64, 44), true)) {
47 4
            throw InvalidGreeting::invalidSalt();
48
        }
49
50 20
        $salt = \substr($salt, 0, 20);
51
52 20
        if (isset($salt[19])) {
53 14
            return $this->salt = $salt;
54
        }
55
56 6
        throw InvalidGreeting::invalidSalt();
57
    }
58
59 20
    public function getServerVersion() : int
60
    {
61 20
        if (null !== $this->serverVersion) {
62
            return $this->serverVersion;
63
        }
64
65 20
        [$major, $minor, $patch] = \sscanf($this->greeting, 'Tarantool %d.%d.%d');
66
67 20
        return $this->serverVersion = $major * 10000 + $minor * 100 + $patch;
68
    }
69
}
70