Completed
Pull Request — master (#37)
by Eugene
09:24
created

AuthMiddleware::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 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
    public function __construct(string $username, string $password = '')
27
    {
28
        $this->username = $username;
29
        $this->password = $password;
30
    }
31
32
    public function process(Request $request, Handler $handler) : Response
33
    {
34
        $connection = $handler->getConnection();
35
36
        if ($connection->isClosed()) {
37
            $handler->handle(new Authenticate(
38
                $connection->open(),
39
                $this->username,
40
                $this->password
41
            ));
42
        }
43
44
        return $handler->handle($request);
45
    }
46
}
47