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

AuthenticationMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 14
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 0 15 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\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