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

AuthMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 5
eloc 16
dl 0
loc 30
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

2 Methods

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