Completed
Push — master ( 97f6b5...9ef69c )
by Matze
08:25
created

Controller::setDefaults()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 10
cts 11
cp 0.9091
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 15
nc 4
nop 5
crap 3.0067
1
<?php
2
3
namespace BrainExe\Core\Annotations\Builder;
4
5
use BrainExe\Core\Annotations\Service;
6
use BrainExe\Core\Annotations\Guest;
7
use BrainExe\Core\Annotations\Role;
8
use BrainExe\Core\Annotations\Route;
9
use BrainExe\Core\DependencyInjection\CompilerPass\ControllerCompilerPass;
10
use BrainExe\Core\Annotations\Controller as ControllerAnnotation;
11
use ReflectionClass;
12
use ReflectionMethod;
13
use Symfony\Component\DependencyInjection\Definition;
14
15
class Controller extends ServiceDefinition
16
{
17
18
    /**
19
     * @param ReflectionClass $reflectionClass
20
     * @param ControllerAnnotation|Service $annotation
21
     * @param Definition $definition
22
     * @return array
23
     */
24 1
    public function build(ReflectionClass $reflectionClass, Service $annotation, Definition $definition)
25
    {
26
        /** @var Definition $definition */
27 1
        [, $definition] = parent::build(
28
            $reflectionClass,
29
            $annotation,
30 1
            $definition
31
        );
32 1
        $serviceId = $reflectionClass->getName();
33
34 1
        $definition->addTag(ControllerCompilerPass::CONTROLLER_TAG);
35 1
        $definition->setPublic(false);
36 1
        $definition->setShared(false);
37
38 1
        foreach ($reflectionClass->getMethods() as $method) {
39
            /** @var Route $routeAnnotation */
40 1
            $routeAnnotation = $this->reader->getMethodAnnotation(
41
                $method,
42 1
                Route::class
43
            );
44
45 1
            if ($routeAnnotation) {
46 1
                $this->handleRouteAnnotation(
47 1
                    $annotation,
0 ignored issues
show
Compatibility introduced by
$annotation of type object<BrainExe\Core\Annotations\Service> is not a sub-type of object<BrainExe\Core\Annotations\Controller>. It seems like you assume a child class of the class BrainExe\Core\Annotations\Service to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
48 1
                    $method,
49 1
                    $routeAnnotation,
50 1
                    $serviceId,
51 1
                    $definition
52
                );
53
            }
54
        }
55
56 1
        return [$serviceId, $definition];
57
    }
58
59
60
    /**
61
     * @param Route $routeAnnotation
62
     * @param ReflectionMethod $method
63
     * @param string $serviceId
64
     * @param Guest $guestAnnotation
65
     * @param Role $roleAnnotation
66
     */
67 1
    protected function setDefaults(
68
        Route $routeAnnotation,
69
        ReflectionMethod $method,
70
        string $serviceId,
71
        Guest $guestAnnotation = null,
72
        Role $roleAnnotation = null
73
    ) {
74 1
        $defaults = $routeAnnotation->getDefaults();
75 1
        $defaults['_controller'] = [
76 1
            $serviceId,
77 1
            $method->getName()
78
        ];
79
80 1
        if ($guestAnnotation) {
81 1
            $defaults['_guest'] = true;
82
        }
83
84 1
        if ($roleAnnotation) {
85
            $defaults['_role'] = $roleAnnotation->role;
86
        }
87
88 1
        $routeAnnotation->setDefaults($defaults);
89 1
    }
90
91
    /**
92
     * @param ControllerAnnotation $annotation
93
     * @param ReflectionMethod $method
94
     * @param Route $routeAnnotation
95
     * @param string $serviceId
96
     * @param Definition $definition
97
     */
98 1
    private function handleRouteAnnotation(
99
        ControllerAnnotation $annotation,
100
        ReflectionMethod$method,
101
        Route $routeAnnotation,
102
        string $serviceId,
103
        Definition $definition
104
    ) {
105
        /** @var Guest $guestAnnotation */
106 1
        $guestAnnotation = $this->reader->getMethodAnnotation($method, Guest::class);
107
108
        /** @var Role $roleAnnotation */
109 1
        $roleAnnotation = $this->reader->getMethodAnnotation($method, Role::class);
110
111 1
        $this->setDefaults($routeAnnotation, $method, $serviceId, $guestAnnotation, $roleAnnotation);
112
113 1
        if ($annotation->requirements) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $annotation->requirements of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
114
            $routeAnnotation->setRequirements(
115
                array_merge(
116
                    $routeAnnotation->getRequirements(),
117
                    $annotation->requirements
118
                )
119
            );
120
        }
121
122 1
        $definition->addTag(ControllerCompilerPass::ROUTE_TAG, [$routeAnnotation]);
123 1
    }
124
}
125