Passed
Push — master ( 4ca145...e39a6b )
by Jakub
02:24
created

ParamMapperPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 33
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 24 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\DependencyInjection\CompilerPass;
5
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
final class ParamMapperPass implements CompilerPassInterface
11
{
12
    public const COLLECTOR_SVC_ID = 'eps.req2cmd.collector.param_collector';
13
    public const MAPPER_TAG = 'req2cmd.param_mapper';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function process(ContainerBuilder $container): void
19
    {
20
        if (!$container->hasDefinition(self::COLLECTOR_SVC_ID)) {
21
            return;
22
        }
23
24
        $collector = $container->findDefinition(self::COLLECTOR_SVC_ID);
25
        $mappersDefinitions = $container->findTaggedServiceIds(self::MAPPER_TAG);
26
        $queue = new \SplPriorityQueue();
27
28
        foreach ($mappersDefinitions as $mapperId => $mapperTags) {
29
            foreach ($mapperTags as $tagAttributes) {
30
                $priority = $tagAttributes['priority'] ?? 0;
31
                $queue->insert($mapperId, $priority);
32
            }
33
        }
34
35
        $mappers = [];
36
        foreach ($queue as $mapperId) {
37
            $mappers[] = new Reference($mapperId);
38
        }
39
40
        $collector->replaceArgument(0, $mappers);
41
    }
42
}
43