Completed
Push — master ( bfde3c...df2f00 )
by Beñat
01:59
created

TacticianEventsBusPass::loadAllSubscribers()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 8.7972
cc 4
eloc 14
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Shared Kernel library.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[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
declare(strict_types=1);
13
14
namespace LIN3S\SharedKernel\Infrastructure\Symfony\Bundle\DependencyInjection\Compiler;
15
16
use BornFree\TacticianDomainEventBundle\TacticianDomainEventBundle;
17
use LIN3S\SharedKernel\Infrastructure\Application\Tactician\TacticianEventBus;
18
use LIN3S\SharedKernel\Infrastructure\Application\Tactician\TacticianEventSubscriber;
19
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
use Symfony\Component\DependencyInjection\Reference;
23
24
/**
25
 * @author Beñat Espiña <[email protected]>
26
 */
27
class TacticianEventsBusPass implements CompilerPassInterface
28
{
29
    public function process(ContainerBuilder $container) : void
30
    {
31
        if (!class_exists(TacticianDomainEventBundle::class)
32
            || !$container->hasDefinition('tactician_domain_events.dispatcher')) {
33
            return;
34
        }
35
36
        $container->setDefinition(
37
            'lin3s.application.tactician_event_bus',
38
            new Definition(TacticianEventBus::class, [
39
                new Reference('tactician_domain_events.dispatcher'),
40
            ])
41
        );
42
        $container->setAlias('lin3s.event_bus', 'lin3s.application.tactician_event_bus');
43
44
        $this->loadAllSubscribers($container);
45
    }
46
47
    private function loadAllSubscribers(ContainerBuilder $container) : void
48
    {
49
        $taggedServices = $container->findTaggedServiceIds('event_subscriber');
50
51
        foreach ($taggedServices as $id => $tags) {
52
            foreach ($tags as $attributes) {
53
                if (!isset($attributes['subscribes_to'])) {
54
                    throw new \Exception(sprintf(
55
                        '"subscribes_to" parameter not found in %s service definition tagged with "event_subscriber"',
56
                        $id
57
                    ));
58
                }
59
60
                $container->setDefinition(
61
                    'tactician_event_subscriber.' . $id,
62
                    new Definition(
63
                        TacticianEventSubscriber::class,
64
                        [new Reference($id)]
65
                    )
66
                )->addTag('tactician.event_listener', ['event' => $attributes['subscribes_to']]);
67
            }
68
        }
69
    }
70
}
71