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

ParamMapperPass::process()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
c 0
b 0
f 0
rs 8.5125
cc 5
eloc 14
nc 7
nop 1
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