ParameterBlacklistValidator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 104
c 0
b 0
f 0
ccs 21
cts 27
cp 0.7778
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A validate() 0 20 4
A getParams() 0 18 3
1
<?php
2
3
/**
4
 * antibot
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Antibot
8
 * @subpackage Jkphl\Antibot\Ports\Validators
9
 * @author     Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright  Copyright © 2020 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2020 Joschi Kuphal <[email protected]>
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Antibot\Ports\Validators;
38
39
use Jkphl\Antibot\Domain\Antibot;
40
use Jkphl\Antibot\Domain\Exceptions\BlacklistValidationException;
41
use Jkphl\Antibot\Infrastructure\Model\AbstractLookupValidator;
42
use Jkphl\Antibot\Ports\Contract\LookupStrategyInterface;
43
use Jkphl\Antibot\Ports\Exceptions\InvalidArgumentException;
44
use Psr\Http\Message\ServerRequestInterface;
45
46
/**
47
 * Parameter Blacklist Validator
48
 *
49
 * @package    Jkphl\Antibot
50
 * @subpackage Jkphl\Antibot\Ports\Validators
51
 */
52
class ParameterBlacklistValidator extends AbstractLookupValidator
53
{
54
    /**
55
     * Parameter name
56
     *
57
     * @var string
58
     */
59
    protected $parameter;
60
    /**
61
     * Parameter type
62
     *
63
     * @var string
64
     */
65
    protected $type;
66
    /**
67
     * Use $_GET parameter
68
     *
69
     * @var string
70
     */
71
    const GET = 'GET';
72
    /**
73
     * Use $_POST parameter
74
     *
75
     * @var string
76
     */
77
    const POST = 'POST';
78
    /**
79
     * Validation order position
80
     *
81
     * @var int
82
     */
83
    const POSITION = 20;
84
85
    /**
86
     * Constructor
87
     *
88
     * @param LookupStrategyInterface $strategy Lookup strategy
89
     * @param string $parameter                 Parameter name
90
     * @param string $type                      Parameter type
91
     */
92 2
    public function __construct(LookupStrategyInterface $strategy, string $parameter, string $type = self::GET)
93
    {
94 2
        parent::__construct($strategy);
95 2
        $this->parameter = $parameter;
96 2
        $this->type      = $type;
97 2
    }
98
99
    /**
100
     * Validate a request
101
     *
102
     * @param ServerRequestInterface $request Request
103
     * @param Antibot $antibot                Antibot instance
104
     *
105
     * @return bool
106
     * @throws InvalidArgumentException If the parameter name is invalid
107
     */
108 2
    public function validate(ServerRequestInterface $request, Antibot $antibot): bool
109
    {
110
        // If the parameter name is invalid
111 2
        if (empty($this->parameter)) {
112
            throw new InvalidArgumentException(
113
                InvalidArgumentException::INVALID_PARAMETER_NAME_STR,
114
                InvalidArgumentException::INVALID_PARAMETER_NAME
115
            );
116
        }
117
118 2
        $params = $this->getParams($request);
119
120
        // If the parameter is present
121 2
        if (array_key_exists($this->parameter, $params)
122 2
            && $this->strategy->lookup($params[$this->parameter])) {
123 2
            throw new BlacklistValidationException($this->parameter);
124
        }
125
126 2
        return true;
127
    }
128
129
    /**
130
     * Return the parameter stack to use
131
     *
132
     * @param ServerRequestInterface $request
133
     *
134
     * @return array Parameters
135
     * @throws InvalidArgumentException If the parameter type is invalid
136
     */
137 2
    protected function getParams(ServerRequestInterface $request): array
138
    {
139 2
        switch ($this->type) {
140 2
            case static::GET:
141 1
                $params = $request->getQueryParams();
142 1
                break;
143 1
            case static::POST:
144 1
                $params = $request->getParsedBody();
145 1
                break;
146
            default:
147
                throw new InvalidArgumentException(
148
                    sprintf(InvalidArgumentException::INVALID_PARAMETER_TYPE_STR, $this->type),
149
                    InvalidArgumentException::INVALID_PARAMETER_TYPE
150
                );
151
        }
152
153 2
        return $params;
154
    }
155
}
156