Passed
Push — master ( 793cce...b0cf56 )
by Eugene
07:19
created

Greeting::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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\Connection;
15
16
use Tarantool\Client\Exception\UnexpectedResponse;
17
18
final class Greeting
19
{
20
    public const SIZE_BYTES = 128;
21
22
    /** @var string */
23
    private $greeting;
24
25
    /** @var string|null */
26
    private $salt;
27
28
    /** @var string|null */
29
    private $serverVersion;
30
31
    /** @var bool */
32
    private $unknown = false;
33
34
    /**
35
     * @param string $greeting
36
     */
37 486
    private function __construct($greeting)
38
    {
39 486
        $this->greeting = $greeting;
40
    }
41
42 516
    public static function parse(string $greeting) : self
43
    {
44 516
        if (0 === \strpos($greeting, 'Tarantool')) {
45 486
            return new self($greeting);
46
        }
47
48 30
        throw new UnexpectedResponse('Unable to recognize Tarantool server');
49
    }
50
51 3
    public static function unknown() : self
52
    {
53 3
        $self = new self('');
54 3
        $self->unknown = true;
55
56 3
        return $self;
57
    }
58
59 36
    public function getSalt() : string
60
    {
61 36
        if (null !== $this->salt) {
62 9
            return $this->salt;
63
        }
64
65 36
        if ($this->unknown) {
66
            throw new \BadMethodCallException('Salt is unknown for persistent connections');
67
        }
68
69 36
        if (false === $salt = \base64_decode(\substr($this->greeting, 64, 44), true)) {
70 6
            throw new UnexpectedResponse('Unable to decode salt');
71
        }
72
73 30
        $salt = \substr($salt, 0, 20);
74
75 30
        if (isset($salt[19])) {
76 21
            return $this->salt = $salt;
77
        }
78
79 9
        throw new UnexpectedResponse('Salt is too short');
80
    }
81
82 30
    public function getServerVersion() : string
83
    {
84 30
        if (null !== $this->serverVersion) {
85
            return $this->serverVersion;
86
        }
87
88 30
        if ($this->unknown) {
89
            throw new \BadMethodCallException('Server version is unknown for persistent connections');
90
        }
91
92 30
        return $this->serverVersion = \substr($this->greeting, 10, \strspn($this->greeting, '0123456789.', 10));
93
    }
94
95 33
    public function equals(?self $greeting) : bool
96
    {
97 33
        if (!$greeting || $greeting->unknown) {
98 33
            return $this->unknown;
99
        }
100
101 6
        if ($this->unknown) {
102
            return true;
103
        }
104
105 6
        return $greeting->getSalt() === $this->getSalt();
106
    }
107
}
108