Completed
Push — master ( c458f8...eeebc2 )
by Matze
06:56
created

Controller::getServiceId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BrainExe\Core\Annotations\Builder;
4
5
use BrainExe\Annotations\Builder\ServiceDefinition;
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
     * @todo private!
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
20 2
     * @var string
21
     */
22
    protected $serviceId;
23 2
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function build(ReflectionClass $reflectionClass, $annotation)
28 2
    {
29
        /** @var Definition $definition */
30
        list($serviceId, $definition) = parent::build(
31 2
            $reflectionClass,
32 2
            $annotation
33
        );
34
        $this->serviceId = $serviceId = sprintf('__controller.%s', $serviceId);
35
36
        $definition->addTag(ControllerCompilerPass::CONTROLLER_TAG);
37
        $definition->setPublic(true);
38
        $definition->setShared(false);
39
40 2
        foreach ($reflectionClass->getMethods() as $method) {
41
            /** @var Route $routeAnnotation */
42 2
            $routeAnnotation = $this->reader->getMethodAnnotation($method, Route::class);
43
44 2
            if ($routeAnnotation) {
45
                /** @var Guest $guestAnnotation */
46 2
                $guestAnnotation = $this->reader->getMethodAnnotation($method, Guest::class);
47
48 2
                /** @var Role $roleAnnotation */
49
                $roleAnnotation = $this->reader->getMethodAnnotation($method, Role::class);
50 2
51
                $this->setDefaults(
52
                    $routeAnnotation,
53 2
                    $method,
54
                    $annotation,
55 2
                    $guestAnnotation,
56
                    $roleAnnotation
57
                );
58
59
                $definition->addTag(ControllerCompilerPass::ROUTE_TAG, [$routeAnnotation]);
60
            }
61
        }
62 2
63
        return [$serviceId, $definition];
64
    }
65 2
66
67
    /**
68
     * @param Route $routeAnnotation
69
     * @param ReflectionMethod $method
70 2
     * @param ControllerAnnotation $controller
71
     * @param Guest $guestAnnotation
72 2
     * @param Role $roleAnnotation
73 2
     */
74 2
    protected function setDefaults(
75
        Route $routeAnnotation,
76
        ReflectionMethod $method,
77
        ControllerAnnotation $controller,
78
        Guest $guestAnnotation = null,
79
        Role $roleAnnotation = null
80
    ) {
81
        $defaults = $routeAnnotation->getDefaults();
82
        $defaults['_controller'] = [
83
            $this->serviceId,
84 2
            $method->getName()
85
        ];
86
        if ($guestAnnotation) {
87
            $defaults['_guest'] = true;
88
        }
89
90 2
        if ($roleAnnotation) {
91 2
            $defaults['_role'] = $roleAnnotation->role;
92 2
        }
93 2
94
95 2
        if ($controller->requirements) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $controller->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...
96 2
            $routeAnnotation->setRequirements(
97
                array_merge($routeAnnotation->getRequirements(), $controller->requirements)
98
            );
99 2
        }
100
        $routeAnnotation->setDefaults($defaults);
101
    }
102
}
103