Completed
Push — master ( d4e15c...f561d0 )
by Pavel
18:12 queued 10s
created

GridPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 8 2
A __construct() 0 4 1
A processGridTypes() 0 15 2
1
<?php
2
3
4
namespace Pfilsx\DataGrid\DependencyInjection\Compiler;
5
6
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
class GridPass implements CompilerPassInterface
13
{
14
    private $gridExtensionService;
15
    private $gridTypeTag;
16
17
    public function __construct(string $gridExtensionService = 'data_grid.extension', string $gridTypeTag = 'data_grid.type')
18
    {
19
        $this->gridExtensionService = $gridExtensionService;
20
        $this->gridTypeTag = $gridTypeTag;
21
    }
22
23
    public function process(ContainerBuilder $container)
24
    {
25
        if (!$container->hasDefinition($this->gridExtensionService)) {
26
            return;
27
        }
28
29
        $definition = $container->getDefinition($this->gridExtensionService);
30
        $definition->replaceArgument(0, $this->processGridTypes($container));
31
    }
32
33
    private function processGridTypes(ContainerBuilder $container)
34
    {
35
        // Get service locator argument
36
        $servicesMap = [];
37
        $namespaces = [];
38
39
        // Builds an array with fully-qualified type class names as keys and service IDs as values
40
        foreach ($container->findTaggedServiceIds($this->gridTypeTag, true) as $serviceId => $tag) {
41
            // Add form type service to the service locator
42
            $serviceDefinition = $container->getDefinition($serviceId);
43
            $servicesMap[$formType = $serviceDefinition->getClass()] = new Reference($serviceId);
44
            $namespaces[substr($formType, 0, strrpos($formType, '\\'))] = true;
45
        }
46
47
        return ServiceLocatorTagPass::register($container, $servicesMap);
48
    }
49
}