Completed
Pull Request — master (#9)
by
unknown
09:56
created

PopulateDebugCommandPass::process()   C

Complexity

Conditions 8
Paths 13

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 20
nc 13
nop 1
1
<?php
2
3
namespace BornFree\TacticianDomainEventBundle\DependencyInjection\Compiler;
4
5
use BornFree\TacticianDomainEvent\EventDispatcher\EventSubscriberInterface;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
class PopulateDebugCommandPass implements CompilerPassInterface
10
{
11
    public function process(ContainerBuilder $container)
12
    {
13
        if (!$container->hasDefinition('tactician_domain_events.command.debug')) {
14
            return;
15
        }
16
17
        $command = $container->getDefinition('tactician_domain_events.command.debug');
18
        $subscribers = $container->findTaggedServiceIds('tactician.event_subscriber');
19
        $listeners = $container->findTaggedServiceIds('tactician.event_listener');
20
21
        $events = [];
22
23
        foreach ($listeners as $serviceId => $tags) {
24
            foreach ($tags as $tag) {
25
                $listener = $container->getDefinition($serviceId);
26
                $method = array_key_exists('method', $tag) ? $tag['method'] : '__invoke';
27
28
                $events[$tag['event']][] = $listener->getClass() . '::' . $method;
29
            }
30
        }
31
32
        foreach ($subscribers as $serviceId => $tags) {
33
            $subscriber = $container->get($serviceId);
34
35
            if (!$subscriber instanceof EventSubscriberInterface) {
36
                continue;
37
            }
38
39
            foreach ($subscriber->getSubscribedEvents() as $event => $method) {
40
                $events[$event][] = get_class($subscriber) . '::' . $method[1];
41
            }
42
        }
43
44
        ksort($events);
45
        $command->setArgument(0, $events);
46
    }
47
}
48