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

Greeting::getSalt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
25 220
    private function __construct()
26
    {
27 220
        unset($this->salt);
28 220
    }
29
30 262
    public static function parse(string $greeting) : self
31
    {
32 262
        if (0 !== \strpos($greeting, 'Tarantool')) {
33 42
            throw InvalidGreeting::invalidServerName();
34
        }
35
36 220
        $self = new self();
37 220
        $self->greeting = $greeting;
38
39 220
        return $self;
40
    }
41
42 30
    public function getSalt() : string
43
    {
44 30
        return $this->salt;
45
    }
46
47 30
    public function __get($name) : string
48
    {
49 30
        if (false === $salt = \base64_decode(\substr($this->greeting, 64, 44), true)) {
50 8
            throw InvalidGreeting::invalidSalt();
51
        }
52
53 22
        $salt = \substr($salt, 0, 20);
54
55 22
        if (isset($salt[19])) {
56 10
            return $this->salt = $salt;
57
        }
58
59 12
        throw InvalidGreeting::invalidSalt();
60
    }
61
}
62