Completed
Push — master ( 7ae8bf...83de4e )
by Peter
01:17
created

EventListenerPass::registerSubscribers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Bundle\DomainEvent\DependencyInjection\Compiler;
11
12
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
16
class EventListenerPass implements CompilerPassInterface
17
{
18
    /**
19
     * @param ContainerBuilder $container
20
     */
21 4
    public function process(ContainerBuilder $container)
22
    {
23 4
        if (!$container->has('domain_event.locator')) {
24 1
            return;
25
        }
26
27 3
        $current_locator = $container->findDefinition('domain_event.locator');
28 3
        $symfony_locator = $container->findDefinition('domain_event.locator.symfony');
29 3
        $container_locator = $container->findDefinition('domain_event.locator.container');
30
31 3
        if ($current_locator === $symfony_locator || $current_locator === $container_locator) {
32 2
            $this->registerListeners($container, $current_locator);
33 2
            $this->registerSubscribers($container, $current_locator);
34
        }
35 3
    }
36
37
    /**
38
     * @param ContainerBuilder $container
39
     * @param Definition       $current_locator
40
     */
41 2
    private function registerListeners(ContainerBuilder $container, Definition $current_locator)
42
    {
43 2
        foreach ($container->findTaggedServiceIds('domain_event.listener') as $id => $attributes) {
44 2
            foreach ($attributes as $attribute) {
45 2
                $method = !empty($attribute['method']) ? $attribute['method'] : '__invoke';
46 2
                $current_locator->addMethodCall('registerService', [$attribute['event'], $id, $method]);
47
            }
48
        }
49 2
    }
50
51
    /**
52
     * @param ContainerBuilder $container
53
     * @param Definition       $current_locator
54
     */
55 2
    private function registerSubscribers(ContainerBuilder $container, Definition $current_locator)
56
    {
57 2
        foreach ($container->findTaggedServiceIds('domain_event.subscriber') as $id => $attributes) {
58 2
            $subscriber = $container->findDefinition($id);
59 2
            $current_locator->addMethodCall('registerSubscriberService', [$id, $subscriber->getClass()]);
60
        }
61 2
    }
62
}
63