Completed
Push — 2.x ( 359f6e )
by Sullivan
14:45
created

AddAuditEntityCompilerPass::process()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 32
rs 4.8196
c 1
b 0
f 1
cc 10
eloc 17
nc 6
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Sonata project.
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\Definition;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
18
/*
19
 *
20
 * @author Thomas Rabaix <[email protected]>
21
 */
22
class AddAuditEntityCompilerPass implements CompilerPassInterface
23
{
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function process(ContainerBuilder $container)
28
    {
29
        if (!$container->hasDefinition('simplethings_entityaudit.config')) {
30
            return;
31
        }
32
33
        $auditedEntities = $container->getParameter('simplethings.entityaudit.audited_entities');
34
        $force = $container->getParameter('sonata_doctrine_orm_admin.audit.force');
35
36
        foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
37
38
            if ($attributes[0]['manager_type'] != 'orm') {
39
                continue;
40
            }
41
42
            if (true === $force && isset($attributes[0]['audit']) && false === $attributes[0]['audit']) {
43
                continue;
44
            }
45
46
            if (false === $force && (!isset($attributes[0]['audit']) || false === $attributes[0]['audit'])) {
47
                continue;
48
            }
49
50
            $definition = $container->getDefinition($id);
51
            $auditedEntities[] = $this->getModelName($container, $definition->getArgument(1));
52
        }
53
54
        $auditedEntities = array_unique($auditedEntities);
55
56
        $container->setParameter('simplethings.entityaudit.audited_entities', $auditedEntities);
57
        $container->getDefinition('sonata.admin.audit.manager')->addMethodCall('setReader', array('sonata.admin.audit.orm.reader', $auditedEntities));
58
    }
59
60
    /**
61
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
62
     * @param string                                                  $name
63
     *
64
     * @return string
65
     */
66
    private function getModelName(ContainerBuilder $container, $name)
67
    {
68
        if ($name[0] == '%') {
69
            return $container->getParameter(substr($name, 1, -1));
70
        }
71
72
        return $name;
73
    }
74
}
75