Query::isSeveralSupported()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Maba\Bundle\RestBundle\Annotation;
5
6
use Maba\Bundle\RestBundle\Entity\QueryResolverOptions;
7
use Maba\Bundle\RestBundle\Entity\RestRequestOptions;
8
use Maba\Bundle\RestBundle\Exception\ConfigurationException;
9
use Maba\Bundle\RestBundle\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 81 View Code Duplication
    public function __construct(array $options)
0 ignored issues
show
Duplication introduced by
This method 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...
38
    {
39 81
        $this->setParameterName($options['parameterName']);
40 81
        $this->setDenormalizationType($options['denormalizationType'] ?? null);
41 81
        $this->setDenormalizationGroup($options['denormalizationGroup'] ?? null);
42 81
        $this->setValidation($options['validation'] ?? null);
43 81
    }
44
45
    /**
46
     * @param string $parameterName
47
     * @return $this
48
     */
49 81
    private function setParameterName(string $parameterName): self
50
    {
51 81
        $this->parameterName = $parameterName;
52 81
        return $this;
53
    }
54
55
    /**
56
     * @param string|null $denormalizationType
57
     * @return $this
58
     */
59 81
    private function setDenormalizationType($denormalizationType): self
60
    {
61 81
        $this->denormalizationType = $denormalizationType;
62 81
        return $this;
63
    }
64
65
    /**
66
     * @param string|null $denormalizationGroup
67
     * @return $this
68
     */
69 81
    public function setDenormalizationGroup($denormalizationGroup): self
70
    {
71 81
        $this->denormalizationGroup = $denormalizationGroup;
72 81
        return $this;
73
    }
74
75
    /**
76
     * @param Validation|null $validation
77
     * @return $this
78
     */
79 81
    private function setValidation($validation): self
80
    {
81 81
        $this->validation = $validation;
82 81
        return $this;
83
    }
84
85 81
    public function isSeveralSupported(): bool
86
    {
87 81
        return true;
88
    }
89
90 81
    public function apply(RestRequestOptions $options, ReflectionMethodWrapper $reflectionMethod)
91
    {
92 81
        $resolverOptions = (new QueryResolverOptions())
93 81
            ->setParameterName($this->parameterName)
94 81
            ->setDenormalizationType($this->resolveDenormalizationType($reflectionMethod))
95 81
            ->setDenormalizationGroup($this->denormalizationGroup)
96
        ;
97
98 81
        $this->setValidationOptions($reflectionMethod, $resolverOptions);
99
100 81
        $options->addQueryResolverOptions($resolverOptions);
101 81
    }
102
103 81 View Code Duplication
    private function resolveDenormalizationType(ReflectionMethodWrapper $reflectionMethod): string
0 ignored issues
show
Duplication introduced by
This method 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...
104
    {
105 81
        if ($this->denormalizationType !== null) {
106 81
            return $this->denormalizationType;
107
        }
108
109
        try {
110 81
            $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 81
        return $typeName;
120
    }
121
122 81
    private function setValidationOptions(ReflectionMethodWrapper $reflectionMethod, QueryResolverOptions $options)
123
    {
124 81
        if ($this->validation === null) {
125 81
            return;
126
        }
127
128 81
        $restRequestOptions = new RestRequestOptions();
129 81
        $this->validation->apply($restRequestOptions, $reflectionMethod);
130
131 81
        if (!$restRequestOptions->isBodyValidationNeeded()) {
132 81
            $options->disableValidation();
133 81
            return;
134
        }
135
136 81
        $options->setValidationOptions($restRequestOptions->getBodyValidationOptions());
137 81
    }
138
}
139