Completed
Push — 3.x-dev-kit ( cc4dbe )
by
unknown
03:15
created

NotificationCompilerPass   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
c 1
b 1
f 0
lcom 0
cbo 3
dl 0
loc 51
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D process() 0 45 10
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NotificationBundle\DependencyInjection\Compiler;
13
14
use Sonata\NotificationBundle\Event\IterateEvent;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18
19
class NotificationCompilerPass implements CompilerPassInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function process(ContainerBuilder $container)
25
    {
26
        if (!$container->hasDefinition('sonata.notification.dispatcher')) {
27
            return;
28
        }
29
30
        $definition = $container->getDefinition('sonata.notification.dispatcher');
31
32
        $informations = array();
33
34
        foreach ($container->findTaggedServiceIds('sonata.notification.consumer') as $id => $events) {
35
            foreach ($events as $event) {
36
                $priority = isset($event['priority']) ? $event['priority'] : 0;
37
38
                if (!isset($event['type'])) {
39
                    throw new \InvalidArgumentException(sprintf('Service "%s" must define the "type" attribute on "sonata.notification" tags.', $id));
40
                }
41
42
                if (!isset($informations[$event['type']])) {
43
                    $informations[$event['type']] = array();
44
                }
45
46
                $informations[$event['type']][] = $id;
47
48
                $definition->addMethodCall('addListenerService', array($event['type'], array($id, 'process'), $priority));
49
            }
50
        }
51
52
        $container->getDefinition('sonata.notification.consumer.metadata')->replaceArgument(0, $informations);
53
54
        if ($container->getParameter('sonata.notification.event.iteration_listeners')) {
55
            $ids = $container->getParameter('sonata.notification.event.iteration_listeners');
56
57
            foreach ($ids as $serviceId) {
58
                $definition = $container->getDefinition($serviceId);
59
60
                $class = new \ReflectionClass($definition->getClass());
61
                if (!$class->implementsInterface('Sonata\NotificationBundle\Event\IterationListener')) {
62
                    throw new RuntimeException('Iteration listeners must implement Sonata\NotificationBundle\Event\IterationListener');
63
                }
64
65
                $definition->addTag('kernel.event_listener', array('event' => IterateEvent::EVENT_NAME, 'method' => 'iterate'));
66
            }
67
        }
68
    }
69
}
70