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

AuthenticationMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Middleware;
15
16
use Tarantool\Client\Handler\Handler;
17
use Tarantool\Client\Request\AuthenticateRequest;
18
use Tarantool\Client\Request\Request;
19
use Tarantool\Client\Response;
20
21
final class AuthenticationMiddleware implements Middleware
22
{
23
    private $username;
24
    private $password;
25
    private $greeting;
26
27 22
    public function __construct(string $username, string $password = '')
28
    {
29 22
        $this->username = $username;
30 22
        $this->password = $password;
31 22
    }
32
33 22
    public function process(Request $request, Handler $handler) : Response
34
    {
35 22
        $connection = $handler->getConnection();
36
37 22
        if ($this->greeting !== $greeting = $connection->open()) {
38 22
            $handler->handle(new AuthenticateRequest(
39 22
                $greeting->getSalt(),
40 12
                $this->username,
41 12
                $this->password
42
            ));
43
44 6
            $this->greeting = $greeting;
45
        }
46
47 6
        return $handler->handle($request);
48
    }
49
}
50