Completed
Pull Request — master (#20)
by Kevin
02:45
created

ClientCollectorPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 28
ccs 0
cts 13
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 22 5
1
<?php
2
3
/*
4
 * This file is part of the JarvisBundle package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace LinkValue\JarvisBundle\DependencyInjection\Compiler;
11
12
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Reference;
15
16
/**
17
 * Collect all mobile clients and store them into ClientCollection.
18
 *
19
 * @package JarvisBundle
20
 * @author  Jamal Youssefi <[email protected]>
21
 * @author  Valentin Coulon <[email protected]>
22
 */
23
class ClientCollectorPass implements CompilerPassInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function process(ContainerBuilder $container)
29
    {
30
        if (!$container->hasDefinition('link_value_jarvis.clients')) {
31
            return;
32
        }
33
34
        $clientCollectionService = $container->getDefinition('link_value_jarvis.clients');
35
        $taggedClientServices = $container->findTaggedServiceIds('link_value_jarvis.client');
36
37
        foreach ($taggedClientServices as $serviceId => $attributes) {
38
            foreach ($attributes as $attribute) {
39
                if (empty($attribute['name'])) {
40
                    throw new \InvalidArgumentException('"link_value_jarvis.client" tag must define "name" key.');
41
                }
42
43
                $clientCollectionService->addMethodCall('addClient', array(
44
                    $attribute['name'],
45
                    new Reference($serviceId),
46
                ));
47
            }
48
        }
49
    }
50
}
51