Passed
Push — master ( 35c30e...6daa1b )
by Przemysław eRIZ
04:42
created

SyliusMessageBusPolyfillPass::process()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.9332
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusWishlistPlugin\DependencyInjection;
12
13
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
16
final class SyliusMessageBusPolyfillPass implements CompilerPassInterface
17
{
18
    public const TAG_FALLBACK = [
19
        'sylius.command_bus' => 'sylius_default.bus',
20
        'sylius.event_bus' => 'sylius_event.bus',
21
    ];
22
23
    public const COMMAND_BUS_TAG = 'bitbag.sylius_wishlist_plugin.command_bus';
24
25
    private function setupDefaultCommandBus(array $buses, ContainerBuilder $container): void
26
    {
27
        $targetBusName = in_array('sylius.command_bus', $buses, true) ? 'sylius.command_bus' : 'sylius_default.bus';
28
        $container->setAlias(
29
            self::COMMAND_BUS_TAG,
30
            $targetBusName
31
        );
32
    }
33
34
    public function process(ContainerBuilder $container): void
35
    {
36
        /**
37
         * @var array<string, array> $handlers
38
         */
39
        $handlers = $container->findTaggedServiceIds(self::COMMAND_BUS_TAG);
40
        $buses = array_keys($container->findTaggedServiceIds('messenger.bus'));
41
        $this->setupDefaultCommandBus($buses, $container);
42
43
        foreach ($handlers as $handler => $tagData) {
44
            if (!isset($tagData[0]['bus'])) {
45
                continue;
46
            }
47
48
            $busName = (string) $tagData[0]['bus'];
49
50
            $def = $container->findDefinition($handler);
51
            $def->addTag('messenger.message_handler', [
52
                'bus' => in_array($busName, $buses, true) ? $busName : self::TAG_FALLBACK[$busName],
53
            ]);
54
        }
55
    }
56
}
57