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

AuthMiddleware::__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 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