TacticianDomainEventExtension::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace BornFree\TacticianDomainEventBundle\DependencyInjection;
4
5
use BornFree\TacticianDoctrineDomainEvent\EventListener\CollectsEventsFromAllEntitiesManagedByUnitOfWork;
6
use BornFree\TacticianDoctrineDomainEvent\EventListener\CollectsEventsFromEntities;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Loader;
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
14
15
class TacticianDomainEventExtension extends Extension implements CompilerPassInterface
16
{
17
    public function load(array $configs, ContainerBuilder $container)
18
    {
19
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
20
        $loader->load('services.yml');
21
22
        $configuration = $this->getConfiguration($configs, $container);
23
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Documentation introduced by
$configuration is of type null|object, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
24
25
        $this->registerEventCollector($container, $config['collect_from_all_managed_entities']);
26
    }
27
28
    public function process(ContainerBuilder $container)
29
    {
30
        if (!$container->hasDefinition('tactician_domain_events.dispatcher')) {
31
            return;
32
        }
33
34
        $this->addListeners($container);
35
        $this->addSubscribers($container);
36
    }
37
38
    private function addListeners(ContainerBuilder $container)
39
    {
40
        $definition = $container->getDefinition('tactician_domain_events.dispatcher');
41
        $taggedServices = $container->findTaggedServiceIds('tactician.event_listener');
42
43
        foreach ($taggedServices as $id => $tags) {
44
            foreach ($tags as $attributes) {
45
                if (!isset($attributes['event'])) {
46
                    throw new \Exception('The tactician.event_listener tag must always have an event attribute');
47
                }
48
49
                if (!class_exists($attributes['event'])) {
50
                    throw new \Exception(
51
                        sprintf(
52
                            'Class %s registered as an event class in %s does not exist',
53
                            $attributes['event'],
54
                            $id
55
                        )
56
                    );
57
                }
58
59
                $listener = array_key_exists('method', $attributes)
60
                    ? [new Reference($id), $attributes['method']]
61
                    : new Reference($id);
62
63
                $definition->addMethodCall('addListener', [
64
                    $attributes['event'],
65
                    $listener
66
                ]);
67
            }
68
        }
69
    }
70
71
    private function addSubscribers(ContainerBuilder $container)
72
    {
73
        $definition = $container->getDefinition('tactician_domain_events.dispatcher');
74
        $taggedServices = $container->findTaggedServiceIds('tactician.event_subscriber');
75
76
        foreach ($taggedServices as $id => $tags) {
77
            $definition->addMethodCall('addSubscriber', [
78
                new Reference($id)
79
            ]);
80
        }
81
    }
82
83
    private function registerEventCollector(ContainerBuilder $container, $collectFromAllManagedEntities)
84
    {
85
        $class = CollectsEventsFromEntities::class;
86
        if ($collectFromAllManagedEntities) {
87
            $class = CollectsEventsFromAllEntitiesManagedByUnitOfWork::class;
88
        }
89
90
        $eventCollector = new Definition($class);
91
        $eventCollector->addTag('doctrine.event_subscriber', ['connection' => 'default']);
92
93
        $container->setDefinition('tactician_domain_events.doctrine.event_collector', $eventCollector);
94
    }
95
}
96