1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Paysera\Bundle\ApiBundle\Service; |
5
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Paysera\Bundle\ApiBundle\Entity\RestRequestOptions; |
8
|
|
|
use Paysera\Bundle\ApiBundle\Exception\ConfigurationException; |
9
|
|
|
use Paysera\Bundle\ApiBundle\Service\PathAttributeResolver\PathAttributeResolverRegistry; |
10
|
|
|
use Paysera\Component\Normalization\NormalizerRegistryInterface; |
11
|
|
|
|
12
|
|
|
class RestRequestOptionsValidator |
13
|
|
|
{ |
14
|
|
|
private $normalizerRegistry; |
15
|
|
|
private $pathAttributeResolverRegistry; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param NormalizerRegistryInterface $normalizerRegistry |
19
|
|
|
* @param PathAttributeResolverRegistry $pathAttributeResolverRegistry |
20
|
|
|
* @internal |
21
|
|
|
*/ |
22
|
109 |
|
public function __construct( |
23
|
|
|
NormalizerRegistryInterface $normalizerRegistry, |
24
|
|
|
PathAttributeResolverRegistry $pathAttributeResolverRegistry |
25
|
|
|
) { |
26
|
109 |
|
$this->normalizerRegistry = $normalizerRegistry; |
27
|
109 |
|
$this->pathAttributeResolverRegistry = $pathAttributeResolverRegistry; |
28
|
109 |
|
} |
29
|
|
|
|
30
|
109 |
|
public function validateRestRequestOptions(RestRequestOptions $options, string $methodName) |
31
|
|
|
{ |
32
|
109 |
|
$normalizer = $options->getResponseNormalizationType(); |
33
|
109 |
|
if ($normalizer !== null && !$this->normalizerRegistry->hasNormalizer($normalizer)) { |
34
|
1 |
|
throw new ConfigurationException(sprintf( |
35
|
1 |
|
'Normalizer %s does not exist (configured for %s)', |
36
|
1 |
|
$normalizer, |
37
|
1 |
|
$methodName |
38
|
|
|
)); |
39
|
|
|
} |
40
|
|
|
|
41
|
108 |
|
if ($options->hasBodyDenormalization()) { |
42
|
100 |
|
$this->validateDenormalizer($options->getBodyDenormalizationType(), $methodName); |
43
|
|
|
} |
44
|
|
|
|
45
|
106 |
|
foreach ($options->getQueryResolverOptionsList() as $queryResolverOptions) { |
46
|
100 |
|
$this->validateDenormalizer($queryResolverOptions->getDenormalizationType(), $methodName); |
47
|
|
|
} |
48
|
|
|
|
49
|
104 |
|
foreach ($options->getPathAttributeResolverOptionsList() as $pathAttributeResolverOptions) { |
50
|
99 |
|
$this->validatePathAttributeResolver( |
51
|
99 |
|
$pathAttributeResolverOptions->getPathAttributeResolverType(), |
52
|
99 |
|
$methodName |
53
|
|
|
); |
54
|
|
|
} |
55
|
102 |
|
} |
56
|
|
|
|
57
|
104 |
|
private function validateDenormalizer(string $type, string $methodName) |
58
|
|
|
{ |
59
|
104 |
|
$denormalizerType = $this->normalizerRegistry->getDenormalizerType($type); |
60
|
104 |
|
if ($denormalizerType === NormalizerRegistryInterface::DENORMALIZER_TYPE_NONE) { |
61
|
4 |
|
throw new ConfigurationException(sprintf( |
62
|
4 |
|
'Denormalizer "%s" does not exist (configured for "%s")', |
63
|
4 |
|
$type, |
64
|
4 |
|
$methodName |
65
|
|
|
)); |
66
|
|
|
} |
67
|
101 |
|
} |
68
|
|
|
|
69
|
99 |
|
private function validatePathAttributeResolver(string $type, string $methodName) |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
99 |
|
$this->pathAttributeResolverRegistry->getResolverByType($type); |
73
|
2 |
|
} catch (InvalidArgumentException $exception) { |
74
|
2 |
|
throw new ConfigurationException(sprintf( |
75
|
2 |
|
'Path attribute resolver "%s" does not exist (configured for "%s")', |
76
|
2 |
|
$type, |
77
|
2 |
|
$methodName |
78
|
|
|
)); |
79
|
|
|
} |
80
|
98 |
|
} |
81
|
|
|
} |
82
|
|
|
|