Passed
Push — master ( 5d0b97...9f4c0c )
by Guillaume
02:33
created

RequestQueryStringValueValidator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 163
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 163
loc 163
rs 10
c 0
b 0
f 0
ccs 23
cts 24
cp 0.9583

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A extractValueFromRequestQueryString() 16 16 2
A mustBeString() 11 11 1
A mustBeBoolean() 11 11 1
A mustBeArray() 11 11 1
A mustBeFloat() 11 11 1
A mustBeInteger() 11 11 1
A mustBeDate() 11 11 1
A mustBeDateTime() 11 11 1
A createUIValidationException() 4 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 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
     */
72
    public function mustBeBoolean(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): bool
73
    {
74 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
75
76 1
        return $this->rawValueValidator->mustBeBoolean(
77
            $value,
78
            $queryStringKey,
79
            $this,
80
            $exceptionMessage
81
        );
82
    }
83
84
    /**
85
     * Exceptions are caught in order to be processed later
86
     *
87
     * @throws CommandMappingException If any mapping validation failed
88
     */
89
    public function mustBeArray(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): array
90
    {
91 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
92
93 1
        return $this->rawValueValidator->mustBeArray(
94
            $value,
95
            $queryStringKey,
96
            $this,
97
            $exceptionMessage
98
        );
99
    }
100
101
    /**
102
     * Exceptions are caught in order to be processed later
103
     *
104
     * @throws CommandMappingException If any mapping validation failed
105
     */
106
    public function mustBeFloat(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): float
107
    {
108 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
109
110 1
        return $this->rawValueValidator->mustBeFloat(
111
            $value,
112
            $queryStringKey,
113
            $this,
114
            $exceptionMessage
115
        );
116
    }
117
118
    /**
119
     * Exceptions are caught in order to be processed later
120
     *
121
     * @throws CommandMappingException If any mapping validation failed
122
     */
123
    public function mustBeInteger(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): int
124
    {
125 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
126
127 1
        return $this->rawValueValidator->mustBeInteger(
128
            $value,
129
            $queryStringKey,
130
            $this,
131
            $exceptionMessage
132
        );
133
    }
134
135
    /**
136
     * Exceptions are caught in order to be processed later
137
     *
138
     * @throws CommandMappingException If any mapping validation failed
139
     */
140
    public function mustBeDate(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): \DateTime
141
    {
142 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
143
144 1
        return $this->rawValueValidator->mustBeDate(
145
            $value,
146
            $queryStringKey,
147
            $this,
148
            $exceptionMessage
149
        );
150
    }
151
152
    /**
153
     * Exceptions are caught in order to be processed later
154
     *
155
     * @throws CommandMappingException If any mapping validation failed
156
     */
157
    public function mustBeDateTime(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): \DateTime
158
    {
159 1
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
160
161 1
        return $this->rawValueValidator->mustBeDateTime(
162
            $value,
163
            $queryStringKey,
164
            $this,
165
            $exceptionMessage
166
        );
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172
    public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException
173
    {
174
        return UIValidationException::fromParameter($message, $propertyPath);
175
    }
176
}
177