RequestQueryStringValueValidator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 235
Duplicated Lines 42.13 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 99
loc 235
ccs 31
cts 32
cp 0.9688
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A mustBeString() 11 11 1
A mustBeStringOrEmpty() 11 11 1
A mustBeBoolean() 11 11 1
A __construct() 0 4 1
A extractValueFromRequestQueryString() 0 16 2
A mustBeBooleanOrEmpty() 11 11 1
A mustBeArray() 11 11 1
A mustBeFloat() 11 11 1
A mustBeInteger() 11 11 1
A mustBeIntegerOrEmpty() 11 11 1
A mustBeDate() 0 11 1
A mustBeDateTime() 0 11 1
A mustBeDateTimeOrEmpty() 11 11 1
A createUIValidationException() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace Imedia\Ammit\UI\Resolver\Validator;
5
6
use Imedia\Ammit\UI\Resolver\Exception\CommandMappingException;
7
use Imedia\Ammit\UI\Resolver\Exception\UIValidationException;
8
use Imedia\Ammit\UI\Resolver\ValueExtractor;
9
use Psr\Http\Message\ServerRequestInterface;
10
11
/**
12
 * @author Guillaume MOREL <[email protected]>
13
 */
14
class RequestQueryStringValueValidator implements UIValidatorInterface
15
{
16
    /** @var RawValueValidator */
17
    protected $rawValueValidator;
18
19
    public function __construct(RawValueValidator $rawValueValidator)
20
    {
21 1
        $this->rawValueValidator = $rawValueValidator;
22 1
    }
23
24
    /**
25
     * Validate if request query string $_GET field can be mapped to a Command
26
     * Throw CommandMappingException directly if any mapping issue
27
     * @param ServerRequestInterface $request
28
     * @param string $queryStringKey
29
     *
30
     * @return mixed
31
     * @throws CommandMappingException If any mapping validation failed
32
     */
33
    public function extractValueFromRequestQueryString(ServerRequestInterface $request, string $queryStringKey)
34
    {
35 1
        $valueExtractor = new ValueExtractor();
36
37
        try {
38 1
            return $valueExtractor->fromArray(
39 1
                $request->getQueryParams(),
40
                $queryStringKey
41
            );
42 1
        } catch (InvalidArgumentException $exception) {
43 1
            throw CommandMappingException::fromParameter(
44 1
                $exception->getMessage(),
45 1
                $exception->getPropertyPath()
46
            );
47
        }
48
    }
49
50
    /**
51
     * Exceptions are caught in order to be processed later
52
     *
53
     * @throws CommandMappingException If any mapping validation failed
54
     */
55 View Code Duplication
    public function mustBeString(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
58
59 1
        return $this->rawValueValidator->mustBeString(
60
            $value,
61
            $queryStringKey,
62
            $this,
63
            $exceptionMessage
64
        );
65
    }
66
67
    /**
68
     * Exceptions are caught in order to be processed later
69
     *
70
     * @throws CommandMappingException If any mapping validation failed
71
     * @return string|null
72
     */
73 View Code Duplication
    public function mustBeStringOrEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
76
77 1
        return $this->rawValueValidator->mustBeStringOrEmpty(
78
            $value,
79
            $queryStringKey,
80
            $this,
81
            $exceptionMessage
82
        );
83
    }
84
85
    /**
86
     * Exceptions are caught in order to be processed later
87
     *
88
     * @throws CommandMappingException If any mapping validation failed
89
     */
90 View Code Duplication
    public function mustBeBoolean(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
93
94 1
        return $this->rawValueValidator->mustBeBoolean(
95
            $value,
96
            $queryStringKey,
97
            $this,
98
            $exceptionMessage
99
        );
100
    }
101
102
    /**
103
     * Exceptions are caught in order to be processed later
104
     *
105
     * @throws CommandMappingException If any mapping validation failed
106
     * @return bool|null
107
     */
108 View Code Duplication
    public function mustBeBooleanOrEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
111
112 1
        return $this->rawValueValidator->mustBeBooleanOrEmpty(
113
            $value,
114
            $queryStringKey,
115
            $this,
116
            $exceptionMessage
117
        );
118
    }
119
120
    /**
121
     * Exceptions are caught in order to be processed later
122
     *
123
     * @throws CommandMappingException If any mapping validation failed
124
     */
125 View Code Duplication
    public function mustBeArray(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
128
129 1
        return $this->rawValueValidator->mustBeArray(
130
            $value,
131
            $queryStringKey,
132
            $this,
133
            $exceptionMessage
134
        );
135
    }
136
137
    /**
138
     * Exceptions are caught in order to be processed later
139
     *
140
     * @throws CommandMappingException If any mapping validation failed
141
     */
142 View Code Duplication
    public function mustBeFloat(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): float
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
145
146 1
        return $this->rawValueValidator->mustBeFloat(
147
            $value,
148
            $queryStringKey,
149
            $this,
150
            $exceptionMessage
151
        );
152
    }
153
154
    /**
155
     * Exceptions are caught in order to be processed later
156
     *
157
     * @throws CommandMappingException If any mapping validation failed
158
     */
159 View Code Duplication
    public function mustBeInteger(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
162
163 1
        return $this->rawValueValidator->mustBeInteger(
164
            $value,
165
            $queryStringKey,
166
            $this,
167
            $exceptionMessage
168
        );
169
    }
170
171
    /**
172
     * Exceptions are caught in order to be processed later
173
     *
174
     * @throws CommandMappingException If any mapping validation failed
175
     * @return int|null
176
     */
177 View Code Duplication
    public function mustBeIntegerOrEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
180
181 1
        return $this->rawValueValidator->mustBeIntegerOrEmpty(
182
            $value,
183
            $queryStringKey,
184
            $this,
185
            $exceptionMessage
186
        );
187
    }
188
189
    /**
190
     * Exceptions are caught in order to be processed later
191
     *
192
     * @throws CommandMappingException If any mapping validation failed
193
     */
194
    public function mustBeDate(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): \DateTime
195
    {
196 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
197
198 1
        return $this->rawValueValidator->mustBeDate(
199
            $value,
200
            $queryStringKey,
201
            $this,
202
            $exceptionMessage
203
        );
204
    }
205
206
    /**
207
     * Exceptions are caught in order to be processed later
208
     *
209
     * @throws CommandMappingException If any mapping validation failed
210
     */
211
    public function mustBeDateTime(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): \DateTime
212
    {
213 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
214
215 1
        return $this->rawValueValidator->mustBeDateTime(
216
            $value,
217
            $queryStringKey,
218
            $this,
219
            $exceptionMessage
220
        );
221
    }
222
223
    /**
224
     * Exceptions are caught in order to be processed later
225
     *
226
     * @return \DateTime|null
0 ignored issues
show
Documentation introduced by
Should the return type not be null|\DateTime|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
227
     * @throws CommandMappingException If any mapping validation failed
228
     */
229 View Code Duplication
    public function mustBeDateTimeOrEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
    {
231 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
232
233 1
        return $this->rawValueValidator->mustBeDateTimeOrEmpty(
234
            $value,
235
            $queryStringKey,
236
            $this,
237
            $exceptionMessage
238
        );
239
    }
240
241
    /**
242
     * @inheritdoc
243
     */
244
    public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException
245
    {
246
        return UIValidationException::fromParameter($message, $propertyPath);
247
    }
248
}
249