Completed
Push — master ( 669d28...0a1ee7 )
by Beñat
02:12
created

TacticianEventsBusPass::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
    private $subscriberTag;
30
31
    public function __construct($subscriberTag = 'event_subscriber')
32
    {
33
        $this->subscriberTag = $subscriberTag;
34
    }
35
36
    public function process(ContainerBuilder $container) : void
37
    {
38
        if (!class_exists(TacticianDomainEventBundle::class)
39
            || !$container->hasDefinition('tactician_domain_events.dispatcher')) {
40
            return;
41
        }
42
43
        $container->setDefinition(
44
            'lin3s.application.tactician_event_bus',
45
            new Definition(TacticianEventBus::class, [
46
                new Reference('tactician_domain_events.dispatcher'),
47
            ])
48
        );
49
        $container->setAlias('lin3s.event_bus', 'lin3s.application.tactician_event_bus');
50
51
        $this->loadAllSubscribers($container);
52
    }
53
54
    private function loadAllSubscribers(ContainerBuilder $container) : void
55
    {
56
        $taggedServices = $container->findTaggedServiceIds($this->subscriberTag);
57
58
        foreach ($taggedServices as $id => $tags) {
59
            foreach ($tags as $attributes) {
60
                if (!isset($attributes['subscribes_to'])) {
61
                    throw new \Exception(sprintf(
62
                        '"subscribes_to" parameter not found in %s service definition tagged with "%s"',
63
                        $id,
64
                        $this->subscriberTag
65
                    ));
66
                }
67
68
                $container->setDefinition(
69
                    'tactician_event_subscriber.' . $id,
70
                    new Definition(
71
                        TacticianEventSubscriber::class,
72
                        [new Reference($id)]
73
                    )
74
                )->addTag('tactician.event_listener', ['event' => $attributes['subscribes_to']]);
75
            }
76
        }
77
    }
78
}
79