Completed
Push — master ( 9e0eff...358dda )
by Guillaume
02:44
created

extractValueFromRequestQueryString()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 18
loc 18
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 5
nop 2
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 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...
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 query string $_GET field can be mapped to a Command
27
     * Throw CommandMappingException directly if any mapping issue
28
     * @param ServerRequestInterface $request
29
     * @param string $queryStringKey
30
     *
31
     * @return mixed
32
     * @throws CommandMappingException If any mapping validation failed
33
     */
34
    public function extractValueFromRequestQueryString(ServerRequestInterface $request, string $queryStringKey)
35
    {
36
        try {
37 1
            $queryParams = $request->getQueryParams();
38
39 1
            Assertion::isArray($queryParams);
40 1
            Assertion::keyExists($queryParams, $queryStringKey);
41
42 1
            $value = $queryParams[$queryStringKey];
43
44 1
            return $value;
45 1
        } catch (AssertionFailedException $exception) {
46 1
            throw CommandMappingException::fromParameter(
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
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use 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...
58
     */
59
    public function mustBeString(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
60
    {
61 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
62
63 1
        return $this->rawValueValidator->mustBeString(
64
            $value,
65
            $queryStringKey,
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
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use boolean.

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...
76
     */
77
    public function mustBeBoolean(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
78
    {
79 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
80
81 1
        return $this->rawValueValidator->mustBeBoolean(
82
            $value,
83
            $queryStringKey,
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 $queryStringKey, string $exceptionMessage = null)
96
    {
97 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
98
99 1
        return $this->rawValueValidator->mustBeArray(
100
            $value,
101
            $queryStringKey,
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
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use double.

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...
112
     */
113
    public function mustBeFloat(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
114
    {
115 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
116
117 1
        return $this->rawValueValidator->mustBeFloat(
118
            $value,
119
            $queryStringKey,
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
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use integer.

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...
130
     */
131
    public function mustBeInteger(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
132
    {
133 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
134
135 1
        return $this->rawValueValidator->mustBeInteger(
136
            $value,
137
            $queryStringKey,
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 $queryStringKey, string $exceptionMessage = null)
150
    {
151 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
152
153 1
        return $this->rawValueValidator->mustBeDate(
154
            $value,
155
            $queryStringKey,
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 $queryStringKey, string $exceptionMessage = null)
168
    {
169 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
170
171 1
        return $this->rawValueValidator->mustBeDateTime(
172
            $value,
173
            $queryStringKey,
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::fromParameter($message, $propertyPath);
185
    }
186
}
187