Completed
Pull Request — master (#10)
by Matze
07:31
created

Controller::setDefaults()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.0263

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
ccs 12
cts 14
cp 0.8571
rs 9.3143
cc 3
eloc 14
nc 4
nop 4
crap 3.0263
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 ReflectionClass;
11
use ReflectionMethod;
12
use Symfony\Component\DependencyInjection\Definition;
13
14
class Controller extends ServiceDefinition
15
{
16
17
    /**
18
     * {@inheritdoc}
19
     */
20 2
    public function build(ReflectionClass $reflectionClass, $annotation)
21
    {
22
        /** @var Definition $definition */
23 2
        list(, $definition) = parent::build(
24 2
            $reflectionClass,
25
            $annotation
26 2
        );
27
28 2
        $definition->addTag(ControllerCompilerPass::CONTROLLER_TAG);
29
30
        return [
31 2
            $this->getServiceId(),
32
            $definition
33 2
        ];
34
    }
35
36
    /**
37
     * @param ReflectionMethod[] $methods
38
     * @param Definition $definition
39
     */
40 2
    protected function processMethods($methods, Definition $definition)
41
    {
42 2
        parent::processMethods($methods, $definition);
43
44 2
        foreach ($methods as $method) {
45
            /** @var Route $routeAnnotation */
46 2
            $routeAnnotation = $this->reader->getMethodAnnotation($method, Route::class);
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $routeAnnotation is correct as $this->reader->getMethod...notations\Route::class) (which targets Doctrine\Common\Annotati...::getMethodAnnotation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
47
48 2
            if ($routeAnnotation) {
49
                /** @var Guest $guestAnnotation */
50 2
                $guestAnnotation = $this->reader->getMethodAnnotation($method, Guest::class);
51
52
                /** @var Role $roleAnnotation */
53 2
                $roleAnnotation = $this->reader->getMethodAnnotation($method, Role::class);
54
55 2
                $this->setDefaults(
56 2
                    $routeAnnotation,
57 2
                    $method,
58 2
                    $guestAnnotation,
59
                    $roleAnnotation
60 2
                );
61
62 2
                $definition->addTag(ControllerCompilerPass::ROUTE_TAG, [$routeAnnotation]);
63 2
            }
64 2
        }
65 2
    }
66
67
    /**
68
     * @return string
69
     */
70 2
    private function getServiceId()
71
    {
72 2
        return sprintf(
73 2
            '__Controller.%s',
74 2
            $this->serviceId
75 2
        );
76
    }
77
78
    /**
79
     * @param Route $routeAnnotation
80
     * @param ReflectionMethod $method
81
     * @param Guest $guestAnnotation
82
     * @param Role $roleAnnotation
83
     */
84 2
    protected function setDefaults(
85
        Route $routeAnnotation,
86
        ReflectionMethod $method,
87
        Guest $guestAnnotation = null,
88
        Role $roleAnnotation = null
89
    ) {
90 2
        $defaults = $routeAnnotation->getDefaults();
91 2
        $defaults['_controller'] = [
92 2
            $this->getServiceId(),
93 2
            $method->getName()
94 2
        ];
95 2
        if ($guestAnnotation) {
96 2
            $defaults['_guest'] = true;
97 2
        }
98
99 2
        if ($roleAnnotation) {
100
            $defaults['_role'] = $roleAnnotation->role;
101
        }
102
103 2
        $routeAnnotation->setDefaults($defaults);
104 2
    }
105
}
106