Completed
Pull Request — master (#37)
by Eugene
05:31
created

AuthMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 26
ccs 9
cts 12
cp 0.75
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 14 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 2
    public function __construct(string $username, string $password = '')
27
    {
28 2
        $this->username = $username;
29 2
        $this->password = $password;
30 2
    }
31
32 2
    public function process(Request $request, Handler $handler) : Response
33
    {
34 2
        $connection = $handler->getConnection();
35
36 2
        if ($connection->isClosed()) {
37 2
            $handler->handle(new Authenticate(
38 2
                $connection->open(),
39
                $this->username,
40
                $this->password
41
            ));
42
        }
43
44
        return $handler->handle($request);
45
    }
46
}
47