Completed
Push — master ( 64cc96...5eeedf )
by Jacob
7s
created

EventSubscribersPass::process()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 23
rs 6.7272
cc 7
eloc 14
nc 6
nop 1
1
<?php
2
3
namespace As3\Bundle\ModlrBundle\DependencyInjection\Compiler;
4
5
use As3\Bundle\ModlrBundle\DependencyInjection\Utility;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * Adds event subscribers to the event dispatcher.
12
 *
13
 * @author  Jacob Bare <[email protected]>
14
 */
15
class EventSubscribersPass implements CompilerPassInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function process(ContainerBuilder $container)
21
    {
22
        $dispatcher = $container->getDefinition(Utility::getAliasedName('event_dispatcher'));
23
24
        $sortFunc = function ($a, $b) {
25
            $a = isset($a['priority']) ? (Integer) $a['priority'] : 0;
26
            $b = isset($b['priority']) ? (Integer) $b['priority'] : 0;
27
28
            return $a > $b ? -1 : 1;
29
        };
30
31
        $tagged = $container->findTaggedServiceIds(Utility::getAliasedName('event_subscriber'));
32
        $subscribers = [];
33
        foreach ($tagged as $id => $tags) {
34
            foreach ($tags as $attributes) {
35
                $subscribers[$id] = $attributes;
36
            }
37
        }
38
        uasort($subscribers, $sortFunc);
39
        foreach ($subscribers as $id => $attrs) {
40
            $dispatcher->addMethodCall('addSubscriber', [new Reference($id)]);
41
        }
42
    }
43
}
44