RegisterFiltersPass::process()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 24
rs 9.2222
cc 6
nc 5
nop 1
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 RegisterFiltersPass implements CompilerPassInterface
13
{
14
    public function process(ContainerBuilder $container): void
15
    {
16
        if (!$container->hasDefinition('setono_sylius_stock_movement.registry.filter') || !$container->hasDefinition('setono_sylius_stock_movement.form_registry.filter')) {
17
            return;
18
        }
19
20
        $registry = $container->getDefinition('setono_sylius_stock_movement.registry.filter');
21
        $formTypeRegistry = $container->getDefinition('setono_sylius_stock_movement.form_registry.filter');
22
23
        $typeToLabelMap = [];
24
        foreach ($container->findTaggedServiceIds('setono_sylius_stock_movement.filter') as $id => $tagged) {
25
            foreach ($tagged as $attributes) {
26
                if (!isset($attributes['type'], $attributes['label'], $attributes['form_type'])) {
27
                    throw new InvalidArgumentException('Tagged filter `' . $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.filters', $typeToLabelMap);
38
    }
39
}
40