Greeting   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 87.88%

Importance

Changes 0
Metric Value
eloc 33
c 0
b 0
f 0
dl 0
loc 88
ccs 29
cts 33
cp 0.8788
rs 10
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A unknown() 0 6 1
A getSalt() 0 21 5
A __construct() 0 3 1
A getServerVersion() 0 11 3
A equals() 0 11 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\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 725
    private function __construct($greeting)
38
    {
39 725
        $this->greeting = $greeting;
40
    }
41
42 765
    public static function parse(string $greeting) : self
43
    {
44 765
        if (0 === \strpos($greeting, 'Tarantool')) {
45 725
            return new self($greeting);
46
        }
47
48 40
        throw new UnexpectedResponse('Unable to recognize Tarantool server');
49
    }
50
51 4
    public static function unknown() : self
52
    {
53 4
        $self = new self('');
54 4
        $self->unknown = true;
55
56 4
        return $self;
57
    }
58
59 68
    public function getSalt() : string
60
    {
61 68
        if (null !== $this->salt) {
62 16
            return $this->salt;
63
        }
64
65 68
        if ($this->unknown) {
66
            throw new \BadMethodCallException('Salt is unknown for persistent connections');
67
        }
68
69 68
        if (false === $salt = \base64_decode(\substr($this->greeting, 64, 44), true)) {
70 8
            throw new UnexpectedResponse('Unable to decode salt');
71
        }
72
73 60
        $salt = \substr($salt, 0, 20);
74
75 60
        if (isset($salt[19])) {
76 48
            return $this->salt = $salt;
77
        }
78
79 12
        throw new UnexpectedResponse('Salt is too short');
80
    }
81
82 40
    public function getServerVersion() : string
83
    {
84 40
        if (null !== $this->serverVersion) {
85
            return $this->serverVersion;
86
        }
87
88 40
        if ($this->unknown) {
89
            throw new \BadMethodCallException('Server version is unknown for persistent connections');
90
        }
91
92 40
        return $this->serverVersion = \substr($this->greeting, 10, \strspn($this->greeting, '0123456789.', 10));
93
    }
94
95 64
    public function equals(?self $greeting) : bool
96
    {
97 64
        if (!$greeting || $greeting->unknown) {
98 64
            return $this->unknown;
99
        }
100
101 8
        if ($this->unknown) {
102
            return true;
103
        }
104
105 8
        return $greeting->getSalt() === $this->getSalt();
106
    }
107
}
108