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

EnterpriseFilterStubPass   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 16
c 1
b 0
f 0
dl 0
loc 43
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A findTaggedServices() 0 14 4
A process() 0 8 2
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