RestRequestOptionsBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 38
ccs 14
cts 16
cp 0.875
rs 10
c 0
b 0
f 0

2 Methods

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