Passed
Pull Request — master (#47)
by Eugene
09:19
created

AuthenticationMiddleware::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 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
    public function __construct(string $username, string $password = '')
28
    {
29
        $this->username = $username;
30
        $this->password = $password;
31
    }
32
33
    public function process(Request $request, Handler $handler) : Response
34
    {
35
        $connection = $handler->getConnection();
36
37
        if ($this->greeting !== $greeting = $connection->open()) {
38
            $handler->handle(new AuthenticateRequest(
39
                $greeting->getSalt(),
40
                $this->username,
41
                $this->password
42
            ));
43
44
            $this->greeting = $greeting;
45
        }
46
47
        return $handler->handle($request);
48
    }
49
}
50