Completed
Push — master ( 3ff7e6...cf5edc )
by Kamil
96:30 queued 63:14
created

PrioritizedCompositeServicePass   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 102
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A process() 0 9 2
A injectTaggedServicesIntoComposite() 0 9 2
A addAliasForCompositeIfServiceDoesNotExist() 0 8 2
A addMethodCalls() 0 6 2
A addMethodCall() 0 10 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
/**
20
 * @author Kamil Kokot <[email protected]>
21
 */
22
abstract class PrioritizedCompositeServicePass implements CompilerPassInterface
0 ignored issues
show
Coding Style introduced by
PrioritizedCompositeServicePass does not seem to conform to the naming convention (^Abstract).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
23
{
24
    /**
25
     * @var string
26
     */
27
    private $serviceId;
28
29
    /**
30
     * @var string
31
     */
32
    private $compositeId;
33
34
    /**
35
     * @var string
36
     */
37
    private $tagName;
38
39
    /**
40
     * @var string
41
     */
42
    private $methodName;
43
44
    /**
45
     * @param string $serviceId
46
     * @param string $compositeId
47
     * @param string $tagName
48
     * @param string $methodName
49
     */
50
    public function __construct($serviceId, $compositeId, $tagName, $methodName)
51
    {
52
        $this->serviceId = $serviceId;
53
        $this->compositeId = $compositeId;
54
        $this->tagName = $tagName;
55
        $this->methodName = $methodName;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function process(ContainerBuilder $container)
62
    {
63
        if (!$container->hasDefinition($this->compositeId)) {
64
            return;
65
        }
66
67
        $this->injectTaggedServicesIntoComposite($container);
68
        $this->addAliasForCompositeIfServiceDoesNotExist($container);
69
    }
70
71
    /**
72
     * @param ContainerBuilder $container
73
     */
74
    private function injectTaggedServicesIntoComposite(ContainerBuilder $container)
75
    {
76
        $channelContextDefinition = $container->findDefinition($this->compositeId);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $channelContextDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
77
78
        $taggedServices = $container->findTaggedServiceIds($this->tagName);
79
        foreach ($taggedServices as $id => $tags) {
80
            $this->addMethodCalls($channelContextDefinition, $id, $tags);
81
        }
82
    }
83
84
    /**
85
     * @param ContainerBuilder $container
86
     */
87
    private function addAliasForCompositeIfServiceDoesNotExist(ContainerBuilder $container)
88
    {
89
        if ($container->has($this->serviceId)) {
90
            return;
91
        }
92
93
        $container->setAlias($this->serviceId, $this->compositeId);
94
    }
95
96
    /**
97
     * @param Definition $channelContextDefinition
98
     * @param string $id
99
     * @param array $tags
100
     */
101
    private function addMethodCalls(Definition $channelContextDefinition, $id, $tags)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $channelContextDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
102
    {
103
        foreach ($tags as $attributes) {
104
            $this->addMethodCall($channelContextDefinition, $id, $attributes);
105
        }
106
    }
107
108
    /**
109
     * @param Definition $channelContextDefinition
110
     * @param string $id
111
     * @param array $attributes
112
     */
113
    private function addMethodCall(Definition $channelContextDefinition, $id, $attributes)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $channelContextDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
114
    {
115
        $arguments = [new Reference($id)];
116
117
        if (isset($attributes['priority'])) {
118
            $arguments[] = $attributes['priority'];
119
        }
120
121
        $channelContextDefinition->addMethodCall($this->methodName, $arguments);
122
    }
123
}
124