Completed
Push — 3.x-dev-kit ( 4fe8f1 )
by
unknown
11:30 queued 08:33
created

AddTemplatesCompilerPass   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
c 2
b 0
f 1
lcom 0
cbo 2
dl 0
loc 60
rs 10

2 Methods

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