Passed
Pull Request — master (#37)
by Eugene
03:24
created

AuthMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 12
dl 0
loc 24
ccs 12
cts 12
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 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Tarantool Client package.
7
 *
8
 * (c) Eugene Leonovich <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Tarantool\Client\Middleware;
15
16
use Tarantool\Client\Handler\Handler;
17
use Tarantool\Client\Request\Authenticate;
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
26 6
    public function __construct(string $username, string $password = '')
27
    {
28 6
        $this->username = $username;
29 6
        $this->password = $password;
30 6
    }
31
32 6
    public function process(Request $request, Handler $handler) : Response
33
    {
34 6
        $connection = $handler->getConnection();
35
36 6
        if ($connection->isClosed()) {
37 6
            $handler->handle(new Authenticate(
38 6
                $connection->open(),
39 6
                $this->username,
40 6
                $this->password
41
            ));
42
        }
43
44 2
        return $handler->handle($request);
45
    }
46
}
47