Test Failed
Pull Request — master (#1)
by
unknown
02:09
created

PathAttribute::setParameterName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Maba\Bundle\RestBundle\Annotation;
5
6
use Maba\Bundle\RestBundle\Entity\PathAttributeResolverOptions;
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 PathAttribute implements RestAnnotationInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $parameterName;
21
22
    /**
23
     * @var string
24
     */
25
    private $pathPartName;
26
27
    /**
28
     * @var string|null
29
     */
30
    private $resolverType;
31
32
    /**
33
     * @var bool|null
34
     */
35
    private $resolutionMandatory;
36
37
    public function __construct(array $options)
38
    {
39
        $this->setParameterName($options['parameterName']);
40
        $this->setPathPartName($options['pathPartName']);
41
        $this->setResolverType($options['resolverType'] ?? null);
42
        $this->setResolutionMandatory($options['resolutionMandatory'] ?? null);
43
    }
44
45
    /**
46
     * @param string $parameterName
47
     * @return $this
48
     */
49
    private function setParameterName(string $parameterName): self
50
    {
51
        $this->parameterName = $parameterName;
52
        return $this;
53
    }
54
55
    /**
56
     * @param string $pathPartName
57
     * @return $this
58
     */
59
    private function setPathPartName(string $pathPartName): self
60
    {
61
        $this->pathPartName = $pathPartName;
62
        return $this;
63
    }
64
65
    /**
66
     * @param string|null $resolverType
67
     * @return $this
68
     */
69
    private function setResolverType($resolverType): self
70
    {
71
        $this->resolverType = $resolverType;
72
        return $this;
73
    }
74
75
    /**
76
     * @param bool|null $resolutionMandatory
77
     * @return $this
78
     */
79
    private function setResolutionMandatory($resolutionMandatory): self
80
    {
81
        $this->resolutionMandatory = $resolutionMandatory;
82
        return $this;
83
    }
84
85
    public function isSeveralSupported(): bool
86
    {
87
        return true;
88
    }
89
90
    public function apply(RestRequestOptions $options, ReflectionMethodWrapper $reflectionMethod)
91
    {
92
        $options->addPathAttributeResolverOptions(
93
            (new PathAttributeResolverOptions())
94
                ->setParameterName($this->parameterName)
95
                ->setPathPartName($this->pathPartName)
96
                ->setPathAttributeResolverType($this->resolvePathAttributeResolverType($reflectionMethod))
97
                ->setResolutionMandatory($this->resolveIfResolutionIsMandatory($reflectionMethod))
98
        );
99
    }
100
101
    private function resolvePathAttributeResolverType(ReflectionMethodWrapper $reflectionMethod): string
102
    {
103
        if ($this->resolverType !== null) {
104
            return $this->resolverType;
105
        }
106
107
        try {
108
            return $reflectionMethod->getNonBuiltInTypeForParameter($this->parameterName);
109
        } catch (ConfigurationException $exception) {
110
            throw new ConfigurationException(sprintf(
111
                'Denormalization type could not be guessed for %s in %s',
112
                '$' . $this->parameterName,
113
                $reflectionMethod->getFriendlyName()
114
            ));
115
        }
116
    }
117
118 View Code Duplication
    private function resolveIfResolutionIsMandatory(ReflectionMethodWrapper $reflectionMethod): bool
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...
119
    {
120
        if ($this->resolutionMandatory !== null) {
121
            return $this->resolutionMandatory;
122
        }
123
124
        $parameter = $reflectionMethod->getParameterByName($this->parameterName);
125
126
        return !$parameter->isDefaultValueAvailable();
127
    }
128
}
129