Passed
Pull Request — master (#47)
by Eugene
09:19
created

Greeting   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 9
eloc 21
dl 0
loc 50
ccs 9
cts 11
cp 0.8182
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getServerVersion() 0 9 2
A getSalt() 0 17 4
A parse() 0 7 2
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