Completed
Push — master ( abd78d...b723d6 )
by Guillaume
26:11 queued 17:30
created

mustBeIntegerOrEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
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 View Code Duplication
class RequestQueryStringValueValidator implements UIValidatorInterface
0 ignored issues
show
Duplication introduced by
This class 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...
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
    public function mustBeString(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): string
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
    public function mustBeStringOrEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
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
    public function mustBeBoolean(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): bool
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
     */
107
    public function mustBeArray(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): array
108
    {
109 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
110
111 1
        return $this->rawValueValidator->mustBeArray(
112
            $value,
113
            $queryStringKey,
114
            $this,
115
            $exceptionMessage
116
        );
117
    }
118
119
    /**
120
     * Exceptions are caught in order to be processed later
121
     *
122
     * @throws CommandMappingException If any mapping validation failed
123
     */
124
    public function mustBeFloat(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): float
125
    {
126 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
127
128 1
        return $this->rawValueValidator->mustBeFloat(
129
            $value,
130
            $queryStringKey,
131
            $this,
132
            $exceptionMessage
133
        );
134
    }
135
136
    /**
137
     * Exceptions are caught in order to be processed later
138
     *
139
     * @throws CommandMappingException If any mapping validation failed
140
     */
141
    public function mustBeInteger(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): int
142
    {
143 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
144
145 1
        return $this->rawValueValidator->mustBeInteger(
146
            $value,
147
            $queryStringKey,
148
            $this,
149
            $exceptionMessage
150
        );
151
    }
152
153
    /**
154
     * Exceptions are caught in order to be processed later
155
     *
156
     * @throws CommandMappingException If any mapping validation failed
157
     * @return int|null
158
     */
159
    public function mustBeIntegerOrEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
160
    {
161 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
162
163 1
        return $this->rawValueValidator->mustBeIntegerOrEmpty(
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
     */
176
    public function mustBeDate(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): \DateTime
177
    {
178 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
179
180 1
        return $this->rawValueValidator->mustBeDate(
181
            $value,
182
            $queryStringKey,
183
            $this,
184
            $exceptionMessage
185
        );
186
    }
187
188
    /**
189
     * Exceptions are caught in order to be processed later
190
     *
191
     * @throws CommandMappingException If any mapping validation failed
192
     */
193
    public function mustBeDateTime(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): \DateTime
194
    {
195 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
196
197 1
        return $this->rawValueValidator->mustBeDateTime(
198
            $value,
199
            $queryStringKey,
200
            $this,
201
            $exceptionMessage
202
        );
203
    }
204
205
    /**
206
     * Exceptions are caught in order to be processed later
207
     *
208
     * @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...
209
     * @throws CommandMappingException If any mapping validation failed
210
     */
211
    public function mustBeDateTimeOrEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
212
    {
213 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
214
215 1
        return $this->rawValueValidator->mustBeDateTimeOrEmpty(
216
            $value,
217
            $queryStringKey,
218
            $this,
219
            $exceptionMessage
220
        );
221
    }
222
223
    /**
224
     * @inheritdoc
225
     */
226
    public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException
227
    {
228
        return UIValidationException::fromParameter($message, $propertyPath);
229
    }
230
}
231