RequestAttributeValueValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
class RequestAttributeValueValidator 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 attribute $_POST field can be mapped to a Command
26
     * Throw CommandMappingException directly if any mapping issue
27
     * @param ServerRequestInterface $request
28
     * @param string $attributeKey
29
     *
30
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object|integer|double|null|array|boolean|string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
31
     * @throws CommandMappingException If any mapping validation failed
32
     */
33
    public function extractValueFromRequestAttribute(ServerRequestInterface $request, string $attributeKey)
34
    {
35 1
        $valueExtractor = new ValueExtractor();
36
37
        try {
38 1
            $value = $valueExtractor->fromArray(
39 1
                $request->getParsedBody(),
40
                $attributeKey
41
            );
42
43 1
            if (is_string($value)) {
44 1
                $value = rawurldecode($value);
45
            }
46
47 1
            return $value;
48 1
        } catch (InvalidArgumentException $exception) {
49 1
            throw CommandMappingException::fromAttribute(
50 1
                $exception->getMessage(),
51 1
                $exception->getPropertyPath()
52
            );
53
        }
54
    }
55
56
    /**
57
     * Exceptions are caught in order to be processed later
58
     *
59
     * @throws CommandMappingException If any mapping validation failed
60
     */
61 View Code Duplication
    public function mustBeString(ServerRequestInterface $request, string $attributeKey, 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...
62
    {
63 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
64
65 1
        return $this->rawValueValidator->mustBeString(
66
            $value,
67
            $attributeKey,
68
            $this,
69
            $exceptionMessage
70
        );
71
    }
72
73
    /**
74
     * Exceptions are caught in order to be processed later
75
     *
76
     * @throws CommandMappingException If any mapping validation failed
77
     * @return string|null
78
     */
79 View Code Duplication
    public function mustBeStringOrEmpty(ServerRequestInterface $request, string $attributeKey, 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...
80
    {
81 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
82
83 1
        return $this->rawValueValidator->mustBeStringOrEmpty(
84
            $value,
85
            $attributeKey,
86
            $this,
87
            $exceptionMessage
88
        );
89
    }
90
91
    /**
92
     * Exceptions are caught in order to be processed later
93
     *
94
     * @throws CommandMappingException If any mapping validation failed
95
     */
96 View Code Duplication
    public function mustBeBoolean(ServerRequestInterface $request, string $attributeKey, 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...
97
    {
98 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
99
100 1
        return $this->rawValueValidator->mustBeBoolean(
101
            $value,
102
            $attributeKey,
103
            $this,
104
            $exceptionMessage
105
        );
106
    }
107
108
    /**
109
     * Exceptions are caught in order to be processed later
110
     *
111
     * @throws CommandMappingException If any mapping validation failed
112
     * @return bool|null
113
     */
114 View Code Duplication
    public function mustBeBooleanOrEmpty(ServerRequestInterface $request, string $attributeKey, 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...
115
    {
116 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
117
118 1
        return $this->rawValueValidator->mustBeBooleanOrEmpty(
119
            $value,
120
            $attributeKey,
121
            $this,
122
            $exceptionMessage
123
        );
124
    }
125
126
    /**
127
     * Exceptions are caught in order to be processed later
128
     *
129
     * @throws CommandMappingException If any mapping validation failed
130
     */
131 View Code Duplication
    public function mustBeArray(ServerRequestInterface $request, string $attributeKey, 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...
132
    {
133 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
134
135 1
        return $this->rawValueValidator->mustBeArray(
136
            $value,
137
            $attributeKey,
138
            $this,
139
            $exceptionMessage
140
        );
141
    }
142
143
    /**
144
     * Exceptions are caught in order to be processed later
145
     *
146
     * @throws CommandMappingException If any mapping validation failed
147
     */
148 View Code Duplication
    public function mustBeFloat(ServerRequestInterface $request, string $attributeKey, 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...
149
    {
150 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
151
152 1
        return $this->rawValueValidator->mustBeFloat(
153
            $value,
154
            $attributeKey,
155
            $this,
156
            $exceptionMessage
157
        );
158
    }
159
160
    /**
161
     * Exceptions are caught in order to be processed later
162
     *
163
     * @throws CommandMappingException If any mapping validation failed
164
     */
165 View Code Duplication
    public function mustBeInteger(ServerRequestInterface $request, string $attributeKey, 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...
166
    {
167 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
168
169 1
        return $this->rawValueValidator->mustBeInteger(
170
            $value,
171
            $attributeKey,
172
            $this,
173
            $exceptionMessage
174
        );
175
    }
176
177
    /**
178
     * Exceptions are caught in order to be processed later
179
     *
180
     * @throws CommandMappingException If any mapping validation failed
181
     * @return int|null
182
     */
183 View Code Duplication
    public function mustBeIntegerOrEmpty(ServerRequestInterface $request, string $attributeKey, 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...
184
    {
185 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
186
187 1
        return $this->rawValueValidator->mustBeIntegerOrEmpty(
188
            $value,
189
            $attributeKey,
190
            $this,
191
            $exceptionMessage
192
        );
193
    }
194
195
    /**
196
     * Exceptions are caught in order to be processed later
197
     *
198
     * @throws CommandMappingException If any mapping validation failed
199
     */
200
    public function mustBeDate(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): \DateTime
201
    {
202 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
203
204 1
        return $this->rawValueValidator->mustBeDate(
205
            $value,
206
            $attributeKey,
207
            $this,
208
            $exceptionMessage
209
        );
210
    }
211
212
    /**
213
     * Exceptions are caught in order to be processed later
214
     *
215
     * @throws CommandMappingException If any mapping validation failed
216
     */
217
    public function mustBeDateTime(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): \DateTime
218
    {
219 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
220
221 1
        return $this->rawValueValidator->mustBeDateTime(
222
            $value,
223
            $attributeKey,
224
            $this,
225
            $exceptionMessage
226
        );
227
    }
228
229
    /**
230
     * Exceptions are caught in order to be processed later
231
     *
232
     * @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...
233
     * @throws CommandMappingException If any mapping validation failed
234
     */
235 View Code Duplication
    public function mustBeDateTimeOrEmpty(ServerRequestInterface $request, string $attributeKey, 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...
236
    {
237 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
238
239 1
        return $this->rawValueValidator->mustBeDateTimeOrEmpty(
240
            $value,
241
            $attributeKey,
242
            $this,
243
            $exceptionMessage
244
        );
245
    }
246
247
    /**
248
     * @inheritdoc
249
     */
250
    public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException
251
    {
252
        return UIValidationException::fromAttribute($message, $propertyPath);
253
    }
254
}
255