EventSubscribersPass   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 29
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 23 7
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