Completed
Pull Request — master (#41)
by Guillaume
03:05
created

RequestAttributeValueValidator::mustBeDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 0
cts 2
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Imedia\Ammit\UI\Resolver\Validator;
5
6
use Assert\Assertion;
7
use Assert\AssertionFailedException;
8
use Imedia\Ammit\UI\Resolver\Exception\CommandMappingException;
9
use Imedia\Ammit\UI\Resolver\Exception\UIValidationException;
10
use Psr\Http\Message\ServerRequestInterface;
11
12
/**
13
 * @author Guillaume MOREL <[email protected]>
14
 */
15 View Code Duplication
class RequestAttributeValueValidator 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...
16
{
17
    /** @var RawValueValidator */
18
    protected $rawValueValidator;
19
20
    public function __construct(RawValueValidator $rawValueValidator)
21
    {
22 1
        $this->rawValueValidator = $rawValueValidator;
23 1
    }
24
25
    /**
26
     * Validate if request attribute $_POST field can be mapped to a Command
27
     * Throw CommandMappingException directly if any mapping issue
28
     * @param ServerRequestInterface $request
29
     * @param string $attributeKey
30
     *
31
     * @return mixed
32
     * @throws CommandMappingException If any mapping validation failed
33
     */
34
    public function extractValueFromRequestAttribute(ServerRequestInterface $request, string $attributeKey)
35
    {
36
        try {
37 1
            $attributes = $request->getParsedBody();
38
39 1
            Assertion::isArray($attributes);
40 1
            Assertion::keyExists($attributes, $attributeKey);
41
42 1
            $value = $attributes[$attributeKey];
43
44 1
            return $value;
45 1
        } catch (AssertionFailedException $exception) {
46 1
            throw CommandMappingException::fromAttribute(
47 1
                $exception->getMessage(),
48 1
                $exception->getPropertyPath()
49
            );
50
        }
51
    }
52
53
    /**
54
     * Exceptions are caught in order to be processed later
55
     *
56
     * @throws CommandMappingException If any mapping validation failed
57
     * @return mixed Untouched value
58
     */
59
    public function mustBeString(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
60
    {
61 1
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
62
63 1
        return $this->rawValueValidator->mustBeString(
64
            $value,
65
            $attributeKey,
66
            $this,
67
            $exceptionMessage
68
        );
69
    }
70
71
    /**
72
     * Exceptions are caught in order to be processed later
73
     *
74
     * @throws CommandMappingException If any mapping validation failed
75
     * @return mixed Untouched value
76
     */
77
    public function mustBeBoolean(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
78
    {
79
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
80
81
        return $this->rawValueValidator->mustBeBoolean(
82
            $value,
83
            $attributeKey,
84
            $this,
85
            $exceptionMessage
86
        );
87
    }
88
89
    /**
90
     * Exceptions are caught in order to be processed later
91
     *
92
     * @throws CommandMappingException If any mapping validation failed
93
     * @return mixed Untouched value
94
     */
95
    public function mustBeArray(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
96
    {
97
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
98
99
        return $this->rawValueValidator->mustBeArray(
100
            $value,
101
            $attributeKey,
102
            $this,
103
            $exceptionMessage
104
        );
105
    }
106
107
    /**
108
     * Exceptions are caught in order to be processed later
109
     *
110
     * @throws CommandMappingException If any mapping validation failed
111
     * @return mixed Untouched value
112
     */
113
    public function mustBeFloat(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
114
    {
115
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
116
117
        return $this->rawValueValidator->mustBeFloat(
118
            $value,
119
            $attributeKey,
120
            $this,
121
            $exceptionMessage
122
        );
123
    }
124
125
    /**
126
     * Exceptions are caught in order to be processed later
127
     *
128
     * @throws CommandMappingException If any mapping validation failed
129
     * @return mixed Untouched value
130
     */
131
    public function mustBeInteger(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
132
    {
133
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
134
135
        return $this->rawValueValidator->mustBeInteger(
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
     * @return mixed Untouched value
148
     */
149
    public function mustBeDate(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
150
    {
151
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
152
153
        return $this->rawValueValidator->mustBeDate(
154
            $value,
155
            $attributeKey,
156
            $this,
157
            $exceptionMessage
158
        );
159
    }
160
161
    /**
162
     * Exceptions are caught in order to be processed later
163
     *
164
     * @throws CommandMappingException If any mapping validation failed
165
     * @return mixed Untouched value
166
     */
167
    public function mustBeDateTime(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
168
    {
169
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
170
171
        return $this->rawValueValidator->mustBeDateTime(
172
            $value,
173
            $attributeKey,
174
            $this,
175
            $exceptionMessage
176
        );
177
    }
178
179
    /**
180
     * @inheritdoc
181
     */
182
    public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException
183
    {
184
        return UIValidationException::fromAttribute($message, $propertyPath);
185
    }
186
}
187