Completed
Pull Request — master (#37)
by Eugene
09:23 queued 07:55
created

MiddlewareHandler::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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\Handler;
15
16
use Tarantool\Client\Connection\Connection;
17
use Tarantool\Client\Middleware\Middleware;
18
use Tarantool\Client\Packer\Packer;
19
use Tarantool\Client\Request\Request;
20
use Tarantool\Client\Response;
21
22
final class MiddlewareHandler implements Handler
23
{
24
    private $handler;
25
    private $middlewares;
26
27 12
    private function __construct(Handler $handler, array $middlewares)
28
    {
29 12
        $this->handler = $handler;
30 12
        $this->middlewares = $middlewares;
31 12
    }
32
33 12
    public static function create(Handler $handler, Middleware $middleware, Middleware ...$middlewares) : Handler
34
    {
35 12
        if (!$handler instanceof self) {
36 12
            return $middlewares
37 2
                ? new self($handler, \array_merge([$middleware], $middlewares))
38 12
                : new self($handler, [$middleware]);
39
        }
40
41 2
        $handler->middlewares[] = $middleware;
42 2
        if ($middlewares) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $middlewares of type array<integer,Tarantool\...\Middleware\Middleware> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            $handler->middlewares = \array_merge($handler->middlewares, $middlewares);
44
        }
45
46 2
        return $handler;
47
    }
48
49 12
    public function handle(Request $request) : Response
50
    {
51 12
        if ([] === $this->middlewares) {
52 12
            return $this->handler->handle($request);
53
        }
54
55 12
        $middleware = \array_pop($this->middlewares);
56
57 12
        return $middleware->process($request, $this);
58
    }
59
60 6
    public function getConnection() : Connection
61
    {
62 6
        return $this->handler->getConnection();
63
    }
64
65
    public function getPacker() : Packer
66
    {
67
        return $this->handler->getPacker();
68
    }
69
}
70