Completed
Push — master ( e75413...39c194 )
by Roni
14:44 queued 13:17
created

XiideaEasyAuditExtension::getExtendedConfig()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 28

Duplication

Lines 12
Ratio 42.86 %

Importance

Changes 0
Metric Value
dl 12
loc 28
c 0
b 0
f 0
rs 8.0555
cc 9
nc 16
nop 1
1
<?php
2
3
/*
4
 * This file is part of the XiideaEasyAuditBundle package.
5
 *
6
 * (c) Xiidea <http://www.xiidea.net>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Xiidea\EasyAuditBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Loader\LoaderInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
18
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19
use Symfony\Component\DependencyInjection\Loader;
20
21
/**
22
 * This is the class that loads and manages your bundle configuration.
23
 *
24
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
25
 */
26
class XiideaEasyAuditExtension extends Extension implements PrependExtensionInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function load(array $configs, ContainerBuilder $container)
32
    {
33
        $configuration = new Configuration();
34
        $config = $this->processConfiguration($configuration, $configs);
35
36
        foreach ($config as $key => $value) {
37
            $container->setParameter('xiidea.easy_audit.'.$key, $value);
38
        }
39
40
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
41
        $loader->load('services.yml');
42
43
        $this->loadDefaultResolverServices($config, $loader);
44
45
        if (false !== $config['doctrine_objects']) {
46
            $loader->load('doctrine_services.yml');
47
        }
48
    }
49
50
    /**
51
     * @param $config
52
     * @param LoaderInterface $loader
53
     *
54
     * @throws \Exception
55
     */
56
    protected function loadDefaultResolverServices($config, LoaderInterface $loader)
57
    {
58
        if ('xiidea.easy_audit.default_event_resolver' === $config['resolver']) {
59
            $loader->load('default/event-resolver.yml');
60
        }
61
62
        if (true === $config['default_logger']) {
63
            $loader->load('default/logger.yml');
64
        }
65
66
        if (false !== $config['doctrine_objects'] && 'xiidea.easy_audit.default_doctrine_event_resolver' === $config['doctrine_event_resolver']) {
67
            $loader->load('default/doctrine-event-resolver.yml');
68
        }
69
    }
70
71
    /**
72
     * Allow an extension to prepend the extension configurations.
73
     */
74
    public function prepend(ContainerBuilder $container)
75
    {
76
        $prependConfig = $this->getExtendedConfig($container);
77
78
        if (!empty($prependConfig)) {
79
            $container->prependExtensionConfig($this->getAlias(), $prependConfig);
80
        }
81
    }
82
83
    /**
84
     * @param ContainerBuilder $container
85
     *
86
     * @return array
87
     */
88
    protected function getExtendedConfig(ContainerBuilder $container)
89
    {
90
        $doctrineConfig = $container->getExtensionConfig('doctrine');
91
        $configs = array_merge(...$container->getExtensionConfig($this->getAlias()));
92
93
        $prependConfig = [];
94
95 View Code Duplication
        if (isset($configs['entity_class']) && !isset($configs['audit_log_class'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            @trigger_error('The "entity_class" option is deprecated since XiideaEasyAuditBundle 1.4.10. and will not be supported anymore in 2.0. Use "audit_log_class" instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
97
            $prependConfig['audit_log_class'] = $configs['entity_class'];
98
        }
99
100 View Code Duplication
        if (isset($configs['entity_event_resolver']) && !isset($configs['doctrine_event_resolver'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
            @trigger_error('The "entity_event_resolver" option is deprecated since XiideaEasyAuditBundle 1.4.10. and will not be supported anymore in 2.0. Use "doctrine_event_resolver" instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
102
            $prependConfig['doctrine_event_resolver'] = $configs['entity_event_resolver'];
103
        }
104
105 View Code Duplication
        if (isset($configs['doctrine_entities']) && !isset($configs['doctrine_objects'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            @trigger_error('The "doctrine_entities" option is deprecated since XiideaEasyAuditBundle 1.4.10. and will not be supported anymore in 2.0. Use "doctrine_objects" instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
107
            $prependConfig['doctrine_objects'] = $configs['doctrine_entities'];
108
        }
109
110
        if (!empty($doctrineConfig) && !isset($configs['doctrine_event_resolver'])) {
111
            $prependConfig['doctrine_event_resolver'] = 'xiidea.easy_audit.default_doctrine_event_resolver';
112
        }
113
114
        return $prependConfig;
115
    }
116
}
117