Passed
Pull Request — master (#47)
by Eugene
03:06
created

Greeting   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

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

4 Methods

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