Completed
Push — 2.x-dev-kit ( 8d77e1 )
by
unknown
28:22 queued 25:50
created

NotificationCompilerPass::process()   D

Complexity

Conditions 10
Paths 27

Size

Total Lines 45
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 45
rs 4.8196
cc 10
eloc 23
nc 27
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Sonata project.
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