Completed
Push — master ( 870291...b9fe13 )
by Tomáš
03:25
created

AutowireControllerDependencies   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 33.75 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 27
loc 80
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 9 9 2
B autowireControllerTraits() 18 18 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\ControllerAutowire\DependencyInjection\Compiler;
11
12
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\DependencyInjection\Reference;
16
use Symplify\ControllerAutowire\Contract\DependencyInjection\ControllerClassMapInterface;
17
use Symplify\ControllerAutowire\Controller\Doctrine\ControllerDoctrineTrait;
18
use Symplify\ControllerAutowire\Controller\Form\ControllerFormTrait;
19
use Symplify\ControllerAutowire\Controller\HttpKernel\ControllerHttpKernelTrait;
20
use Symplify\ControllerAutowire\Controller\Routing\ControllerRoutingTrait;
21
use Symplify\ControllerAutowire\Controller\Security\ControllerSecurityTrait;
22
use Symplify\ControllerAutowire\Controller\Serializer\ControllerSerializerTrait;
23
use Symplify\ControllerAutowire\Controller\Session\ControllerFlashTrait;
24
use Symplify\ControllerAutowire\Controller\Templating\ControllerRenderTrait;
25
26
final class AutowireControllerDependencies implements CompilerPassInterface
27
{
28
    /**
29
     * @var ControllerClassMapInterface
30
     */
31
    private $controllerClassMap;
32
33
    /**
34
     * @var ContainerBuilder
35
     */
36
    private $containerBuilder;
37
38
    /**
39
     * @var array[]
40
     */
41
    private $traitsToSettersToServiceNameList = [
42
        ControllerFlashTrait::class => [
43
            'setSession' => 'session',
44
        ],
45
        ControllerDoctrineTrait::class => [
46
            'setDoctrine' => 'doctrine',
47
        ],
48
        ControllerRoutingTrait::class => [
49
            'setRouter' => 'router',
50
        ],
51
        ControllerHttpKernelTrait::class => [
52
            'setHttpKernel' => 'http_kernel',
53
            'setRequestStack' => 'request_stack',
54
        ],
55
        ControllerSerializerTrait::class => [
56
            'setSerializer' => 'serializer',
57
        ],
58
        ControllerSecurityTrait::class => [
59
            'setAuthorizationChecker' => 'security.authorization_checker',
60
            'setTokenStorage' => 'security.token_storage',
61
            'setCsrfTokenManager' => 'security.csrf.token_manager',
62
        ],
63
        ControllerRenderTrait::class => [
64
            'setTemplating' => 'templating',
65
            'setTwig' => 'twig',
66
        ],
67
        ControllerFormTrait::class => [
68
            'setFormFactory' => 'form.factory',
69
        ],
70
    ];
71
72
    public function __construct(ControllerClassMapInterface $controllerClassMap)
73
    {
74
        $this->controllerClassMap = $controllerClassMap;
75
    }
76
77 View Code Duplication
    public function process(ContainerBuilder $containerBuilder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $this->containerBuilder = $containerBuilder;
80
81
        foreach ($this->controllerClassMap->getControllers() as $serviceId => $className) {
82
            $controllerDefinition = $containerBuilder->getDefinition($serviceId);
83
            $this->autowireControllerTraits($controllerDefinition);
84
        }
85
    }
86
87 View Code Duplication
    private function autowireControllerTraits(Definition $controllerDefinition)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $usedTraits = class_uses($controllerDefinition->getClass());
90
91
        foreach ($this->traitsToSettersToServiceNameList as $traitClass => $setterToServiceNames) {
92
            if (! array_key_exists($traitClass, $usedTraits)) {
93
                continue;
94
            }
95
96
            foreach ($setterToServiceNames as $setter => $serviceName) {
97
                if (! $this->containerBuilder->has($serviceName)) {
98
                    continue;
99
                }
100
101
                $controllerDefinition->addMethodCall($setter, [new Reference($serviceName)]);
102
            }
103
        }
104
    }
105
}
106