Completed
Push — 3.x ( 98806d...d929f7 )
by
unknown
9s
created

NotificationCompilerPass::process()   C

Complexity

Conditions 10
Paths 27

Size

Total Lines 60
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 6.5333
c 0
b 0
f 0
cc 10
eloc 33
nc 27
nop 1

How to fix   Long Method    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 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(
40
                        'Service "%s" must define the "type" attribute on "sonata.notification" tags.',
41
                        $id
42
                    ));
43
                }
44
45
                if (!isset($informations[$event['type']])) {
46
                    $informations[$event['type']] = array();
47
                }
48
49
                $informations[$event['type']][] = $id;
50
51
                $definition->addMethodCall(
52
                    'addListenerService',
53
                    array(
54
                        $event['type'],
55
                        array($id, 'process'),
56
                        $priority,
57
                    )
58
                );
59
            }
60
        }
61
62
        $container->getDefinition('sonata.notification.consumer.metadata')->replaceArgument(0, $informations);
63
64
        if ($container->getParameter('sonata.notification.event.iteration_listeners')) {
65
            $ids = $container->getParameter('sonata.notification.event.iteration_listeners');
66
67
            foreach ($ids as $serviceId) {
68
                $definition = $container->getDefinition($serviceId);
69
70
                $class = new \ReflectionClass($definition->getClass());
71
                if (!$class->implementsInterface('Sonata\NotificationBundle\Event\IterationListener')) {
72
                    throw new RuntimeException(
73
                        'Iteration listeners must implement Sonata\NotificationBundle\Event\IterationListener'
74
                    );
75
                }
76
77
                $definition->addTag(
78
                    'kernel.event_listener',
79
                    array('event' => IterateEvent::EVENT_NAME, 'method' => 'iterate')
80
                );
81
            }
82
        }
83
    }
84
}
85