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