RegisterTransportsPass   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 26
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 24 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusStockMovementPlugin\DependencyInjection\Compiler;
6
7
use InvalidArgumentException;
8
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
final class RegisterTransportsPass implements CompilerPassInterface
13
{
14
    public function process(ContainerBuilder $container): void
15
    {
16
        if (!$container->hasDefinition('setono_sylius_stock_movement.registry.transport') || !$container->hasDefinition('setono_sylius_stock_movement.form_registry.transport')) {
17
            return;
18
        }
19
20
        $registry = $container->getDefinition('setono_sylius_stock_movement.registry.transport');
21
        $formTypeRegistry = $container->getDefinition('setono_sylius_stock_movement.form_registry.transport');
22
23
        $typeToLabelMap = [];
24
        foreach ($container->findTaggedServiceIds('setono_sylius_stock_movement.transport') as $id => $tagged) {
25
            foreach ($tagged as $attributes) {
26
                if (!isset($attributes['type'], $attributes['label'], $attributes['form_type'])) {
27
                    throw new InvalidArgumentException('Tagged transport `' . $id . '` needs to have `type`, `form_type` and `label` attributes.');
28
                }
29
30
                $typeToLabelMap[$attributes['type']] = $attributes['label'];
31
                $registry->addMethodCall('register', [$attributes['type'], new Reference($id)]);
32
                $formTypeRegistry->addMethodCall('add',
33
                    [$attributes['type'], 'default', $attributes['form_type']]);
34
            }
35
        }
36
37
        $container->setParameter('setono_sylius_stock_movement.transports', $typeToLabelMap);
38
    }
39
}
40