Completed
Push — master ( 4b13e9...ce6bc8 )
by Nikola
03:50
created

Extension::load()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 17
cts 17
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 4
nop 2
crap 3
1
<?php
2
/*
3
 * This file is part of the QueryResourcesLoaderBundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection;
11
12
use RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection\Configuration\Configuration;
13
use Symfony\Component\Config\FileLocator;
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension;
16
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
17
use Symfony\Component\Config\Resource\FileExistenceResource;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * Class Extension
22
 *
23
 * Bundle extension.
24
 *
25
 * @package RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection
26
 */
27
class Extension extends BaseExtension
28
{
29 8
    public function getAlias()
30
    {
31 8
        return 'run_open_code_query_resources_loader';
32
    }
33
34 8
    public function getNamespace()
35
    {
36 8
        return 'http://www.runopencode.com/xsd-schema/query-resources-loader-bundle';
37
    }
38
39 2
    public function getXsdValidationBasePath()
40
    {
41 2
        return __DIR__.'/../Resources/config/schema';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return __DIR__ . '/../Resources/config/schema'; (string) is incompatible with the return type of the parent method Symfony\Component\Depend...etXsdValidationBasePath of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 6
    public function load(array $configs, ContainerBuilder $container)
48
    {
49 6
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
50 6
        $loader->load('services.xml');
51
52 6
        $configuration = new Configuration();
53 6
        $config = $this->processConfiguration($configuration, $configs);
54
55
        $this
56 6
            ->configureTwigGlobals($config, $container)
57 6
            ->configureTwigEnvironment($config, $container)
58 6
            ->configureTwigWarmUpCommand($config, $container)
59 6
            ->configureTwigResourcePaths($config, $container)
60 6
            ->configureTwigBundlePaths($config, $container)
61
            ;
62
63 6
        if (null !== $config['default_executor']) {
64 1
            $container->setParameter('run_open_code.query_resources_loader.default_executor', $config['default_executor']);
65
        }
66
67 6
        if (isset($config['twig']['autoescape_service'], $config['twig']['autoescape_service_method'])) {
68 1
            $config['twig']['autoescape'] = array(new Reference($config['twig']['autoescape_service']), $config['twig']['autoescape_service_method']);
69
        }
70
71 6
        unset($config['twig']['autoescape_service'], $config['twig']['autoescape_service_method'], $config['twig']['globals']);
72
73 6
        $container->getDefinition('run_open_code.query_resources_loader.twig')->replaceArgument(1, $config['twig']);
74 6
    }
75
76
    /**
77
     * @param array $config
78
     * @param ContainerBuilder $container
79
     * @return Extension $this
80
     */
81 6
    protected function configureTwigGlobals(array $config, ContainerBuilder $container)
82
    {
83 6
        if (false !== $container->hasDefinition('run_open_code.query_resources_loader.twig') && !empty($config['twig']['globals'])) {
84
85 2
            $definition = $container->getDefinition('run_open_code.query_resources_loader.twig');
86
87 2
            foreach ($config['twig']['globals'] as $key => $global) {
88
89 2
                if (isset($global['type']) && 'service' === $global['type']) {
90 1
                    $definition->addMethodCall('addGlobal', array($key, new Reference($global['id'])));
91
                } else {
92 2
                    $definition->addMethodCall('addGlobal', array($key, $global['value']));
93
                }
94
            }
95
        }
96
97 6
        return $this;
98
    }
99
100
    /**
101
     * @param array $config
102
     * @param ContainerBuilder $container
103
     * @return Extension $this
104
     */
105 6
    protected function configureTwigEnvironment(array $config, ContainerBuilder $container)
106
    {
107 6
        $configurator = $container->getDefinition('run_open_code.query_resources_loader.twig.configurator.environment');
108 6
        $configurator->replaceArgument(0, $config['twig']['date']['format']);
109 6
        $configurator->replaceArgument(1, $config['twig']['date']['interval_format']);
110 6
        $configurator->replaceArgument(2, $config['twig']['date']['timezone']);
111 6
        $configurator->replaceArgument(3, $config['twig']['number_format']['decimals']);
112 6
        $configurator->replaceArgument(4, $config['twig']['number_format']['decimal_point']);
113 6
        $configurator->replaceArgument(5, $config['twig']['number_format']['thousands_separator']);
114
115 6
        return $this;
116
    }
117
118
    /**
119
     * @param array $config
120
     * @param ContainerBuilder $container
121
     * @return Extension $this
122
     */
123 6
    protected function configureTwigWarmUpCommand(array $config, ContainerBuilder $container)
124
    {
125 6
        $container->getDefinition('run_open_code.query_resources_loader.twig.query_sources_iterator')->replaceArgument(2, $config['twig']['paths']);
126
127 6
        return $this;
128
    }
129
130
    /**
131
     * @param array $config
132
     * @param ContainerBuilder $container
133
     * @return Extension $this
134
     */
135 6
    protected function configureTwigResourcePaths(array $config, ContainerBuilder $container)
136
    {
137 6
        $twigFilesystemLoaderDefinition = $container->getDefinition('run_open_code.query_resources_loader.twig.loader.filesystem');
138
139
        // register user-configured paths
140 6
        foreach ($config['twig']['paths'] as $path => $namespace) {
141 2
            if (!$namespace) {
142 2
                $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path));
143
            } else {
144 2
                $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
145
            }
146
        }
147
148 6
        return $this;
149
    }
150
151
    /**
152
     * @param array $config
153
     * @param ContainerBuilder $container
154
     * @return Extension $this
155
     */
156 6
    protected function configureTwigBundlePaths(array $config, ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
    {
158 6
        $twigFilesystemLoaderDefinition = $container->getDefinition('run_open_code.query_resources_loader.twig.loader.filesystem');
159
160 6
        $addTwigPath = function($dir, $bundle) use ($twigFilesystemLoaderDefinition) {
161
162
            $name = $bundle;
163
164 View Code Duplication
            if ('Bundle' === substr($name, -6)) {
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...
165
                $name = substr($name, 0, -6);
166
            }
167
168
            $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir, $name));
169 6
        };
170
171
        // register bundles as Twig namespaces
172 6
        foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
173
174
            $dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/query';
175
176
            if (is_dir($dir)) {
177
                $addTwigPath($dir, $bundle);
178
            }
179
180
            $container->addResource(new FileExistenceResource($dir));
181
182
            $reflection = new \ReflectionClass($class);
183
            $dir = dirname($reflection->getFileName()).'/Resources/query';
184
185
            if (is_dir($dir)) {
186
                $addTwigPath($dir, $bundle);
187
            }
188
189
            $container->addResource(new FileExistenceResource($dir));
190
        }
191
192 6
        return $this;
193
    }
194
}
195