Completed
Push — master ( 70f8e5...602a5e )
by Eugene
09:10
created

Greeting::isEqualTo()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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