Completed
Push — master ( 28dfad...f53209 )
by
03:00
created

AbstractResourceExtension::load()   D

Complexity

Conditions 11
Paths 192

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 46
rs 4.9629
cc 11
eloc 25
nc 192
nop 2

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
namespace DoS\ResourceBundle\DependencyInjection;
4
5
use Sylius\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractResourceExtension as BaseAbstractResourceExtension;
6
use Symfony\Component\Config\Definition\ConfigurationInterface;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\Config\Loader\DelegatingLoader;
9
use Symfony\Component\Config\Loader\LoaderResolver;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
13
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
14
use Symfony\Component\DependencyInjection\Reference;
15
16
abstract class AbstractResourceExtension extends BaseAbstractResourceExtension
17
{
18
    protected $applicationName = 'dos';
19
20
    protected $configFiles = array(
21
        'parameters.yml',
22
        'services.yml',
23
        'providers.yml',
24
        'controllers.yml',
25
        'templatings.yml',
26
        'listeners.yml',
27
        'settings.yml',
28
        'callbacks.yml',
29
        'forms.yml',
30
        'mails.yml',
31
        'menus.yml',
32
        'twigs.yml',
33
        'workers.yml',
34
    );
35
36
    /**
37
     * DoS translate to Dos preventing Container::underscore => do_s_.
38
     *
39
     * {@inheritdoc}
40
     */
41
    public function getAlias()
42
    {
43
        $className = get_class($this);
44
45
        if (substr($className, -9) != 'Extension') {
46
            throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
47
        }
48
49
        return AbstractResourceBundle::expectedAlias(substr(strrchr($className, '\\'), 1, -9));
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function load(array $config, ContainerBuilder $container)
56
    {
57
        $config = $this->processConfiguration($this->getBundleConfiguration(), $config);
58
        $loader = new YamlFileLoader($container, new FileLocator($this->getConfigDir()));
59
60
        if (empty($config['resources'])) {
61
            $config['resources'] = array();
62
        }
63
64
        if (isset($config['driver'])) {
65
            $this->registerResources($this->applicationName, $config['driver'], $config['resources'], $container);
66
        }
67
68
        $interfaces = array();
69
70
        foreach($config['resources'] as $model => $resource) {
71
            foreach($resource['classes'] as $key => $class) {
72
                if ($key === 'interface') {
73
                    $name = sprintf('%s.%s.%s.class', $this->applicationName, $key, $model);
74
                    $container->setParameter($name, $class);
75
76
                    $interfaces[$class] = sprintf('%s.%s.%s.class', $this->applicationName, 'model', $model);
77
                }
78
            }
79
        }
80
81
        $container->setParameter($this->getAlias() . '_interfaces', $interfaces);
82
83
        foreach($config['resources'] as $model => $resource) {
84
            foreach($resource['classes'] as $key => $class) {
85
                if ($key === 'provider') {
86
                    $name = sprintf('%s.%s.%s.class', $this->applicationName, $key, $model);
87
                    $container->setParameter($name, $class);
88
                    $this->addProvider($container, $class, $model);
89
                }
90
            }
91
        }
92
93
        foreach ($this->configFiles as $configFile) {
94
            if (file_exists(sprintf('%s/%s', $this->getConfigDir(), $configFile))) {
95
                $loader->load($configFile);
96
            }
97
        }
98
99
        return $config;
100
    }
101
102
    protected function addProvider(ContainerBuilder $container, $providerClass, $modelName)
103
    {
104
        $providerReflection = new \ReflectionClass($providerClass);
105
        $definition = new Definition($providerClass);
106
107
        $definition->setArguments([
108
            new Reference(sprintf('%s.repository.%s', $this->applicationName, $modelName)),
109
            new Reference(sprintf('%s.factory.%s', $this->applicationName, $modelName)),
110
        ]);
111
112
        $definition->setLazy(!$providerReflection->isFinal());
113
        $container->setDefinition(sprintf('%s.provider.%s', $this->applicationName, $modelName), $definition);
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    protected function getConfigDir()
120
    {
121
        $reflector = new \ReflectionClass($this);
122
        $fileName = $reflector->getFileName();
123
124
        return sprintf('%s/../Resources/config', dirname($fileName));
125
    }
126
127
    /**
128
     * @return ConfigurationInterface
129
     */
130
    abstract protected function getBundleConfiguration();
131
}
132