Passed
Push — master ( 3d7afe...8baea4 )
by Eugene
05:38
created

Greeting::parse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 17
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 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
    private function __construct()
21
    {
22
    }
23
24
    public static function parse(string $greeting) : string
25
    {
26
        if (0 !== \strpos($greeting, 'Tarantool')) {
27
            throw InvalidGreeting::invalidServerName();
28
        }
29
30
        if (false === $salt = \base64_decode(\substr($greeting, 64, 44), true)) {
31
            throw InvalidGreeting::invalidSalt();
32
        }
33
34
        $salt = \substr($salt, 0, 20);
35
36
        if (isset($salt[19])) {
37
            return $salt;
38
        }
39
40
        throw InvalidGreeting::invalidSalt();
41
    }
42
}
43