Passed
Push — master ( c12de4...5e6239 )
by Eugene
03:39
created

AuthMiddleware::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.0119
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 AuthMiddleware implements Middleware
22
{
23
    private $username;
24
    private $password;
25
    private $isSucceeded = false;
26
27 7
    public function __construct(string $username, string $password = '')
28
    {
29 7
        $this->username = $username;
30 7
        $this->password = $password;
31 7
    }
32
33 7
    public function process(Request $request, Handler $handler) : Response
34
    {
35 7
        $connection = $handler->getConnection();
36
37 7
        if ($this->isSucceeded && $connection->isClosed()) {
38
            $this->isSucceeded = false;
39
        }
40
41 7
        if (!$this->isSucceeded) {
42 7
            $handler->handle(new AuthenticateRequest(
43 7
                $connection->open(),
44 7
                $this->username,
45 7
                $this->password
46
            ));
47 2
            $this->isSucceeded = true;
48
        }
49
50 2
        return $handler->handle($request);
51
    }
52
}
53