FirewallMiddleware::andAllow()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0185
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\Exception\RequestDenied;
17
use Tarantool\Client\Handler\Handler;
18
use Tarantool\Client\Request\Request;
19
use Tarantool\Client\RequestTypes;
20
use Tarantool\Client\Response;
21
22
final class FirewallMiddleware implements Middleware
23
{
24
    /** @var array<int, true> */
25
    private $allowed;
26
27
    /** @var array<int, true> */
28
    private $denied;
29
30
    /**
31
     * @param array $allowed
32
     * @param array $denied
33
     */
34 60
    private function __construct($allowed, $denied)
35
    {
36 60
        $this->allowed = $allowed ? \array_fill_keys($allowed, true) : [];
37 60
        $this->denied = $denied ? \array_fill_keys($denied, true) : [];
38
    }
39
40 24
    public static function allow(int $requestType, int ...$requestTypes) : self
41
    {
42 24
        return new self([-1 => $requestType] + $requestTypes, []);
43
    }
44
45 28
    public static function deny(int $requestType, int ...$requestTypes) : self
46
    {
47 28
        return new self([], [-1 => $requestType] + $requestTypes);
48
    }
49
50 8
    public static function allowReadOnly() : self
51
    {
52 8
        $self = new self([], []);
53 8
        $self->allowed = [
54 8
            RequestTypes::AUTHENTICATE => true,
55 8
            RequestTypes::PING => true,
56 8
            RequestTypes::SELECT => true,
57 8
        ];
58
59 8
        return $self;
60
    }
61
62 8
    public function andAllow(int $requestType, int ...$requestTypes) : self
63
    {
64 8
        $new = clone $this;
65 8
        $new->allowed += $requestTypes
66
            ? \array_fill_keys([-1 => $requestType] + $requestTypes, true)
67 8
            : [$requestType => true];
68
69 8
        return $new;
70
    }
71
72 8
    public function andAllowOnly(int $requestType, int ...$requestTypes) : self
73
    {
74 8
        $new = clone $this;
75 8
        $new->allowed = $requestTypes
76
            ? \array_fill_keys([-1 => $requestType] + $requestTypes, true)
77 8
            : [$requestType => true];
78
79 8
        return $new;
80
    }
81
82 8
    public function andDeny(int $requestType, int ...$requestTypes) : self
83
    {
84 8
        $new = clone $this;
85 8
        $new->denied += $requestTypes
86
            ? \array_fill_keys([-1 => $requestType] + $requestTypes, true)
87 8
            : [$requestType => true];
88
89 8
        return $new;
90
    }
91
92 8
    public function andDenyOnly(int $requestType, int ...$requestTypes) : self
93
    {
94 8
        $new = clone $this;
95 8
        $new->denied = $requestTypes
96
            ? \array_fill_keys([-1 => $requestType] + $requestTypes, true)
97 8
            : [$requestType => true];
98
99 8
        return $new;
100
    }
101
102 68
    public function process(Request $request, Handler $handler) : Response
103
    {
104 68
        $requestType = $request->getType();
105
106 68
        if (isset($this->denied[$requestType])) {
107 20
            throw RequestDenied::fromObject($request);
108
        }
109
110 48
        if ([] !== $this->allowed && !isset($this->allowed[$requestType])) {
111 16
            throw RequestDenied::fromObject($request);
112
        }
113
114 32
        return $handler->handle($request);
115
    }
116
}
117