Completed
Pull Request — master (#48)
by Claudio
10:22 queued 04:51
created

EnterpriseFilterStubPass::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Flagbit\Bundle\TableAttributeBundle\Test\Kernel;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
class EnterpriseFilterStubPass implements CompilerPassInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $type;
15
16
    /**
17
     * @param string $type
18
     */
19
    public function __construct(string $type)
20
    {
21
        $this->type = $type;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function process(ContainerBuilder $container)
28
    {
29
        $registry = $container->getDefinition(sprintf('pimee_workflow.query.filter.%s_registry', $this->type));
30
        $filterTag = sprintf('pimee_workflow.elasticsearch.query.%s_filter', $this->type);
31
32
        $filters = $this->findTaggedServices($filterTag, $container);
33
        foreach ($filters as $filter) {
34
            $registry->addMethodCall('register', [$filter]);
35
        }
36
    }
37
38
    private function findTaggedServices(string $tagName, ContainerBuilder $container): array
39
    {
40
        $services = $container->findTaggedServiceIds($tagName);
41
42
        $sortedServices = [];
43
        foreach ($services as $serviceId => $tags) {
44
            foreach ($tags as $tag) {
45
                $priority = $tag['priority'] ?? 30;
46
                $sortedServices[$priority][] = new Reference($serviceId);
47
            }
48
        }
49
        krsort($sortedServices);
50
51
        return count($sortedServices) > 0 ? array_merge(...$sortedServices) : [];
52
    }
53
}
54