AddTemplatesCompilerPass::process()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.9617
c 0
b 0
f 0
cc 6
nc 6
nop 1
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\DoctrinePHPCRAdminBundle\DependencyInjection\Compiler;
15
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
19
/**
20
 * @author Thomas Rabaix <[email protected]>
21
 * @author Nacho Martín <[email protected]>
22
 */
23
class AddTemplatesCompilerPass implements CompilerPassInterface
24
{
25
    public function process(ContainerBuilder $container): void
26
    {
27
        $settings = $this->fixSettings($container);
28
        foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
29
            if (!isset($attributes[0]['manager_type']) || 'doctrine_phpcr' !== $attributes[0]['manager_type']) {
30
                continue;
31
            }
32
33
            $definition = $container->getDefinition($id);
34
35
            if (!$definition->hasMethodCall('setFormTheme')) {
36
                $definition->addMethodCall('setFormTheme', [$settings['templates']['form']]);
37
            }
38
39
            if (!$definition->hasMethodCall('setFilterTheme')) {
40
                $definition->addMethodCall('setFilterTheme', [$settings['templates']['filter']]);
41
            }
42
43
            $definition->addMethodCall('setTemplate', ['pager_results', $settings['templates']['pager_results']]);
44
        }
45
    }
46
47
    /**
48
     * @return bool
49
     */
50
    protected function fixSettings(ContainerBuilder $container)
51
    {
52
        $pool = $container->getDefinition('sonata.admin.manager.doctrine_phpcr');
53
54
        // @todo not very clean but don't know how to do that for now
55
        $settings = false;
56
        $methods = $pool->getMethodCalls();
57
        foreach ($methods as $pos => $calls) {
58
            if ('__hack_doctrine_phpcr__' === $calls[0]) {
59
                $settings = $calls[1];
60
61
                break;
62
            }
63
        }
64
65
        if ($settings) {
66
            unset($methods[$pos]);
67
        }
68
69
        $pool->setMethodCalls($methods);
70
71
        return $settings;
72
    }
73
}
74