Completed
Pull Request — develop (#152)
by A.
04:44
created

AddEventBusListenersCompilerPass::process()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 32
rs 6.7272
c 1
b 0
f 1
cc 7
eloc 16
nc 7
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\DependencyInjection\CompilerPass;
20
21
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\LogicException;
22
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
23
use Symfony\Component\DependencyInjection\ContainerBuilder;
24
use Symfony\Component\DependencyInjection\Reference;
25
26
class AddEventBusListenersCompilerPass implements CompilerPassInterface
27
{
28
    public function process(ContainerBuilder $container)
29
    {
30
        $definition = $container->getDefinition('surfnet_stepup_middleware_command_handling.event_bus.buffered');
31
        $eventListenerDefinitions = $container->findTaggedServiceIds('event_bus.event_listener');
32
33
        // When replaying events, certain listeners might not be interested in acting upon, for instance
34
        // when they are no longer relevant at the time of replaying (i.e. sending emails)
35
        if (!in_array($container->getParameter('kernel.environment'), ['dev_event_replay', 'prod_event_replay'])) {
36
            foreach (array_keys($eventListenerDefinitions) as $serviceId) {
37
                $definition->addMethodCall('subscribe', [new Reference($serviceId)]);
38
            }
39
40
            return;
41
        }
42
43
        foreach ($eventListenerDefinitions as $serviceId => $tags) {
44
            foreach ($tags as $attributes) {
45
                if (!isset($attributes['disable_for_replay'])) {
46
                    throw new LogicException(sprintf(
47
                        'Cannot replay events: Expected option "disable_for_replay" to be set for service id "%s"',
48
                        $serviceId
49
                    ));
50
                }
51
52
                if ($attributes['disable_for_replay']) {
53
                    continue;
54
                }
55
56
                $definition->addMethodCall('subscribe', [new Reference($serviceId)]);
57
            }
58
        }
59
    }
60
}
61