Passed
Push — master ( 3645ad...705ee3 )
by Jakub
31s
created

ParamMapperPass::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
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
        $mappers = $this->collectMappers($mappersDefinitions);
27
28
        $collector->replaceArgument(0, $mappers);
29
    }
30
31
    private function collectMappers(array $mappersDefinitions): array
32
    {
33
        $queue = new \SplPriorityQueue();
34
35
        foreach ($mappersDefinitions as $mapperId => $mapperTags) {
36
            $tagAttributes = $mapperTags[0];
37
            $priority = $tagAttributes['priority'] ?? 0;
38
            $queue->insert($mapperId, $priority);
39
        }
40
41
        return array_map(
42
            function ($mapperId) {
43
                return new Reference($mapperId);
44
            },
45
            iterator_to_array($queue, false)
46
        );
47
    }
48
}
49