RestRequestOptionsBuilder::buildOptions()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0582

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 13
cp 0.8462
rs 9.568
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4.0582
1
<?php
2
declare(strict_types=1);
3
4
namespace Maba\Bundle\RestBundle\Service\Annotation;
5
6
use Maba\Bundle\RestBundle\Exception\ConfigurationException;
7
use Maba\Bundle\RestBundle\Service\RestRequestOptionsValidator;
8
use ReflectionMethod;
9
use Maba\Bundle\RestBundle\Annotation\RestAnnotationInterface;
10
use Maba\Bundle\RestBundle\Entity\RestRequestOptions;
11
12
/**
13
 * @internal
14
 */
15
class RestRequestOptionsBuilder
16
{
17
    private $validator;
18
19 83
    public function __construct(RestRequestOptionsValidator $validator)
20
    {
21 83
        $this->validator = $validator;
22 83
    }
23
24
    /**
25
     * @param array|RestAnnotationInterface[] $annotations
26
     * @param ReflectionMethod $reflectionMethod
27
     * @return RestRequestOptions
28
     * @throws ConfigurationException
29
     */
30 83
    public function buildOptions(array $annotations, ReflectionMethod $reflectionMethod): RestRequestOptions
31
    {
32 83
        $options = new RestRequestOptions();
33 83
        $wrapper = new ReflectionMethodWrapper($reflectionMethod);
34 83
        $usedAnnotations = [];
35
36 83
        foreach ($annotations as $annotation) {
37 83
            $className = get_class($annotation);
38 83
            if (!$annotation->isSeveralSupported() && isset($usedAnnotations[$className])) {
39
                throw new ConfigurationException(
40
                    sprintf('Only one annotation of type %s is supported', $className)
41
                );
42
            }
43 83
            $usedAnnotations[$className] = true;
44
45 83
            $annotation->apply($options, $wrapper);
46
        }
47
48 82
        $this->validator->validateRestRequestOptions($options, $wrapper->getFriendlyName());
49
50 82
        return $options;
51
    }
52
}
53