AbstractCollectionPass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 12 3
A getCollection() 0 4 1
A addServiceToCollection() 0 6 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\CoreBundle\DependencyInjection\Compiler;
14
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * Class AbstractCollectionPass
22
 *
23
 * @author Adam Piotrowski <[email protected]>
24
 */
25
abstract class AbstractCollectionPass implements CompilerPassInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $collectionServiceId;
31
32
    /**
33
     * @var string
34
     */
35
    protected $serviceTag;
36
37
    /**
38
     * Processes the container
39
     *
40
     * @param ContainerBuilder $container
41
     */
42
    public function process(ContainerBuilder $container)
43
    {
44
        if (!$container->hasDefinition($this->collectionServiceId)) {
45
            return;
46
        }
47
48
        $collection = $this->getCollection($container);
49
50
        foreach ($container->findTaggedServiceIds($this->serviceTag) as $id => $attributes) {
51
            $this->addServiceToCollection($collection, $id);
52
        }
53
    }
54
55
    /**
56
     * Returns collection service definition
57
     *
58
     * @param ContainerBuilder $container
59
     *
60
     * @return \Symfony\Component\DependencyInjection\Definition
61
     */
62
    protected function getCollection(ContainerBuilder $container)
63
    {
64
        return $container->getDefinition($this->collectionServiceId);
65
    }
66
67
    /**
68
     * Adds new tagged service to collection
69
     *
70
     * @param Definition $collection
71
     * @param string     $id
72
     */
73
    protected function addServiceToCollection(Definition $collection, $id)
74
    {
75
        $collection->addMethodCall('add', [
76
            new Reference($id)
77
        ]);
78
    }
79
}
80