Passed
Push — master ( a3526d...7fe89c )
by Eugene
06:54
created

FirewallMiddleware::deny()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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