SyncEventDispatcherCompilerPass   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 37
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 26 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Explicit Architecture POC,
7
 * which is created on top of the Symfony Demo application.
8
 *
9
 * (c) Herberto Graça <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Acme\App\Infrastructure\EventDispatcher;
16
17
use InvalidArgumentException;
18
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Reference;
21
22
final class SyncEventDispatcherCompilerPass implements CompilerPassInterface
23
{
24
    public const SYNC_EVENT_DISPATCHER_TAG = 'sync_event_dispatcher';
25
    public const TAG_NAME_KEY = 'name';
26
    public const EVENT = 'event';
27
    public const METHOD = 'method';
28
    public const PRIORITY = 'priority';
29
    public const PRIORITY_DEFAULT = 0;
30
    public const METHOD_CALL = 'addDestination';
31
32
    public function process(ContainerBuilder $containerBuilder): void
33
    {
34
        $eventDispatcherDefinition = $containerBuilder->findDefinition(SyncEventDispatcher::class);
35
36
        foreach ($containerBuilder->findTaggedServiceIds(self::SYNC_EVENT_DISPATCHER_TAG) as $listenerId => $tagList) {
37
            $listenerDefinition = $containerBuilder->getDefinition($listenerId);
38
            /** @var array $tagList */
39
            foreach ($tagList as $attributeList) {
40
                if (!method_exists($listenerDefinition->getClass(), $attributeList[self::METHOD])) {
41
                    throw new InvalidArgumentException(
42
                        'The configured listener ' . $listenerId
43
                        . ' does not have the configured method ' . $attributeList[self::METHOD]
44
                    );
45
                }
46
47
                $eventDispatcherDefinition->addMethodCall(
48
                    self::METHOD_CALL,
49
                    [
50
                        $attributeList[self::EVENT],
51
                        [new Reference($listenerId), $attributeList[self::METHOD]],
52
                        $attributeList[self::PRIORITY] ?? self::PRIORITY_DEFAULT,
53
                    ]
54
                );
55
            }
56
        }
57
    }
58
}
59