AddTemplatesCompilerPass   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 2
dl 0
loc 57
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 29 8
A mergeMethodCall() 0 20 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler;
15
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
20
/**
21
 * @author Thomas Rabaix <[email protected]>
22
 */
23
class AddTemplatesCompilerPass implements CompilerPassInterface
24
{
25
    public function process(ContainerBuilder $container): void
26
    {
27
        $overwrite = $container->getParameter('sonata.admin.configuration.admin_services');
28
        $templates = $container->getParameter('sonata_doctrine_orm_admin.templates');
29
30
        foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
31
            if (!isset($attributes[0]['manager_type']) || 'orm' !== $attributes[0]['manager_type']) {
32
                continue;
33
            }
34
35
            $definition = $container->getDefinition($id);
36
37
            if (!$definition->hasMethodCall('setFormTheme')) {
38
                $definition->addMethodCall('setFormTheme', [$templates['form']]);
39
            }
40
41
            if (isset($overwrite[$id]['templates']['form'])) {
42
                $this->mergeMethodCall($definition, 'setFormTheme', $overwrite[$id]['templates']['form']);
43
            }
44
45
            if (!$definition->hasMethodCall('setFilterTheme')) {
46
                $definition->addMethodCall('setFilterTheme', [$templates['filter']]);
47
            }
48
49
            if (isset($overwrite[$id]['templates']['filter'])) {
50
                $this->mergeMethodCall($definition, 'setFilterTheme', $overwrite[$id]['templates']['filter']);
51
            }
52
        }
53
    }
54
55
    /**
56
     * @param string $name
57
     * @param mixed  $value
58
     */
59
    public function mergeMethodCall(Definition $definition, $name, $value): void
60
    {
61
        $methodCalls = $definition->getMethodCalls();
62
63
        foreach ($methodCalls as &$calls) {
64
            foreach ($calls as &$call) {
65
                if (\is_string($call)) {
66
                    if ($call !== $name) {
67
                        continue 2;
68
                    }
69
70
                    continue 1;
71
                }
72
73
                $call = [array_merge($call[0], $value)];
74
            }
75
        }
76
77
        $definition->setMethodCalls($methodCalls);
78
    }
79
}
80