1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Paysera\Bundle\ApiBundle\Annotation; |
5
|
|
|
|
6
|
|
|
use Paysera\Bundle\ApiBundle\Entity\QueryResolverOptions; |
7
|
|
|
use Paysera\Bundle\ApiBundle\Entity\RestRequestOptions; |
8
|
|
|
use Paysera\Bundle\ApiBundle\Exception\ConfigurationException; |
9
|
|
|
use Paysera\Bundle\ApiBundle\Service\Annotation\ReflectionMethodWrapper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @Annotation |
13
|
|
|
* @Target({"METHOD"}) |
14
|
|
|
*/ |
15
|
|
|
class Query implements RestAnnotationInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $parameterName; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
private $denormalizationType; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string|null |
29
|
|
|
*/ |
30
|
|
|
private $denormalizationGroup; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Validation|null |
34
|
|
|
*/ |
35
|
|
|
private $validation; |
36
|
|
|
|
37
|
96 |
View Code Duplication |
public function __construct(array $options) |
|
|
|
|
38
|
|
|
{ |
39
|
96 |
|
$this->setParameterName($options['parameterName']); |
40
|
96 |
|
$this->setDenormalizationType($options['denormalizationType'] ?? null); |
41
|
96 |
|
$this->setDenormalizationGroup($options['denormalizationGroup'] ?? null); |
42
|
96 |
|
$this->setValidation($options['validation'] ?? null); |
43
|
96 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $parameterName |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
96 |
|
private function setParameterName(string $parameterName): self |
50
|
|
|
{ |
51
|
96 |
|
$this->parameterName = $parameterName; |
52
|
96 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string|null $denormalizationType |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
96 |
|
private function setDenormalizationType($denormalizationType): self |
60
|
|
|
{ |
61
|
96 |
|
$this->denormalizationType = $denormalizationType; |
62
|
96 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string|null $denormalizationGroup |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
96 |
|
public function setDenormalizationGroup($denormalizationGroup): self |
70
|
|
|
{ |
71
|
96 |
|
$this->denormalizationGroup = $denormalizationGroup; |
72
|
96 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Validation|null $validation |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
96 |
|
private function setValidation($validation): self |
80
|
|
|
{ |
81
|
96 |
|
$this->validation = $validation; |
82
|
96 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
96 |
|
public function isSeveralSupported(): bool |
86
|
|
|
{ |
87
|
96 |
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
96 |
|
public function apply(RestRequestOptions $options, ReflectionMethodWrapper $reflectionMethod) |
91
|
|
|
{ |
92
|
96 |
|
$resolverOptions = (new QueryResolverOptions()) |
93
|
96 |
|
->setParameterName($this->parameterName) |
94
|
96 |
|
->setDenormalizationType($this->resolveDenormalizationType($reflectionMethod)) |
95
|
96 |
|
->setDenormalizationGroup($this->denormalizationGroup) |
96
|
|
|
; |
97
|
|
|
|
98
|
96 |
|
$this->setValidationOptions($reflectionMethod, $resolverOptions); |
99
|
|
|
|
100
|
96 |
|
$options->addQueryResolverOptions($resolverOptions); |
101
|
96 |
|
} |
102
|
|
|
|
103
|
96 |
View Code Duplication |
private function resolveDenormalizationType(ReflectionMethodWrapper $reflectionMethod): string |
|
|
|
|
104
|
|
|
{ |
105
|
96 |
|
if ($this->denormalizationType !== null) { |
106
|
96 |
|
return $this->denormalizationType; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
try { |
110
|
96 |
|
$typeName = $reflectionMethod->getNonBuiltInTypeForParameter($this->parameterName); |
111
|
|
|
} catch (ConfigurationException $exception) { |
112
|
|
|
throw new ConfigurationException(sprintf( |
113
|
|
|
'Denormalization type could not be guessed for %s in %s', |
114
|
|
|
'$' . $this->parameterName, |
115
|
|
|
$reflectionMethod->getFriendlyName() |
116
|
|
|
)); |
117
|
|
|
} |
118
|
|
|
|
119
|
96 |
|
return $typeName; |
120
|
|
|
} |
121
|
|
|
|
122
|
96 |
|
private function setValidationOptions(ReflectionMethodWrapper $reflectionMethod, QueryResolverOptions $options) |
123
|
|
|
{ |
124
|
96 |
|
if ($this->validation === null) { |
125
|
96 |
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
96 |
|
$restRequestOptions = new RestRequestOptions(); |
129
|
96 |
|
$this->validation->apply($restRequestOptions, $reflectionMethod); |
130
|
|
|
|
131
|
96 |
|
if (!$restRequestOptions->isBodyValidationNeeded()) { |
132
|
96 |
|
$options->disableValidation(); |
133
|
96 |
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
96 |
|
$options->setValidationOptions($restRequestOptions->getBodyValidationOptions()); |
137
|
96 |
|
} |
138
|
|
|
} |
139
|
|
|
|
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.