Configuration   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 234
Duplicated Lines 2.56 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 6
loc 234
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C getConfigTreeBuilder() 6 191 11
A addResolversSections() 0 4 1
A addLoadersSections() 0 4 1
A addConfigurationSections() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\DependencyInjection;
13
14
use Liip\ImagineBundle\Config\Controller\ControllerConfig;
15
use Liip\ImagineBundle\Controller\ImagineController;
16
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\LoaderFactoryInterface;
17
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface;
18
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
19
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
20
use Symfony\Component\Config\Definition\ConfigurationInterface;
21
22
class Configuration implements ConfigurationInterface
23
{
24
    /**
25
     * @var ResolverFactoryInterface[]
26
     */
27
    protected $resolversFactories;
28
29
    /**
30
     * @var LoaderFactoryInterface[]
31
     */
32
    protected $loadersFactories;
33
34
    /**
35
     * @param ResolverFactoryInterface[] $resolversFactories
36
     * @param LoaderFactoryInterface[]   $loadersFactories
37
     */
38
    public function __construct(array $resolversFactories, array $loadersFactories)
39
    {
40
        $this->resolversFactories = $resolversFactories;
41
        $this->loadersFactories = $loadersFactories;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getConfigTreeBuilder()
48
    {
49
        $treeBuilder = new TreeBuilder('liip_imagine');
50
        $rootNode = method_exists(TreeBuilder::class, 'getRootNode')
51
            ? $treeBuilder->getRootNode()
52
            : $treeBuilder->root('liip_imagine');
0 ignored issues
show
Bug introduced by
The method root() does not seem to exist on object<Symfony\Component...on\Builder\TreeBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
54
        $resolversPrototypeNode = $rootNode
55
            ->children()
56
                ->arrayNode('resolvers')
57
                    ->useAttributeAsKey('name')
58
                    ->prototype('array')
59
                        ->performNoDeepMerging();
60
        $this->addResolversSections($resolversPrototypeNode);
61
62
        $loadersPrototypeNode = $rootNode
63
            ->children()
64
                ->arrayNode('loaders')
65
                    ->useAttributeAsKey('name')
66
                    ->prototype('array');
67
        $this->addLoadersSections($loadersPrototypeNode);
68
69
        $rootNode
70
            ->beforeNormalization()
71
                ->ifTrue(function ($v) {
72
                    return
73
                        empty($v['loaders']) ||
74
                        empty($v['loaders']['default']) ||
75
                        empty($v['resolvers']) ||
76
                        empty($v['resolvers']['default']);
77
                })
78
                ->then(function ($v) {
79
                    if (empty($v['loaders'])) {
80
                        $v['loaders'] = [];
81
                    }
82
83
                    if (false === \is_array($v['loaders'])) {
84
                        throw new \LogicException('Loaders has to be array');
85
                    }
86
87 View Code Duplication
                    if (false === \array_key_exists('default', $v['loaders'])) {
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...
88
                        $v['loaders']['default'] = ['filesystem' => null];
89
                    }
90
91
                    if (empty($v['resolvers'])) {
92
                        $v['resolvers'] = [];
93
                    }
94
95
                    if (false === \is_array($v['resolvers'])) {
96
                        throw new \LogicException('Resolvers has to be array');
97
                    }
98
99 View Code Duplication
                    if (false === \array_key_exists('default', $v['resolvers'])) {
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...
100
                        $v['resolvers']['default'] = ['web_path' => null];
101
                    }
102
103
                    return $v;
104
                })
105
            ->end();
106
107
        $rootNode
108
            ->fixXmlConfig('filter_set', 'filter_sets')
109
            ->children()
110
                ->scalarNode('driver')->defaultValue('gd')
111
                    ->validate()
112
                        ->ifTrue(function ($v) {
113
                            return !\in_array($v, ['gd', 'imagick', 'gmagick'], true);
114
                        })
115
                        ->thenInvalid('Invalid imagine driver specified: %s')
116
                    ->end()
117
                ->end()
118
                ->scalarNode('cache')->defaultValue('default')->end()
119
                ->scalarNode('cache_base_path')->defaultValue('')->end()
120
                ->scalarNode('data_loader')->defaultValue('default')->end()
121
                ->scalarNode('default_image')->defaultNull()->end()
122
                ->arrayNode('default_filter_set_settings')
123
                    ->addDefaultsIfNotSet()
124
                    ->children()
125
                        ->scalarNode('quality')->defaultValue(100)->end()
126
                        ->scalarNode('jpeg_quality')->defaultNull()->end()
127
                        ->scalarNode('png_compression_level')->defaultNull()->end()
128
                        ->scalarNode('png_compression_filter')->defaultNull()->end()
129
                        ->scalarNode('format')->defaultNull()->end()
130
                        ->booleanNode('animated')->defaultFalse()->end()
131
                        ->scalarNode('cache')->defaultNull()->end()
132
                        ->scalarNode('data_loader')->defaultNull()->end()
133
                        ->scalarNode('default_image')->defaultNull()->end()
134
                        ->arrayNode('filters')
135
                            ->useAttributeAsKey('name')
136
                            ->prototype('array')
137
                                ->useAttributeAsKey('name')
138
                                ->prototype('variable')->end()
139
                            ->end()
140
                        ->end()
141
                        ->arrayNode('post_processors')
142
                            ->defaultValue([])
143
                            ->useAttributeAsKey('name')
144
                            ->prototype('array')
145
                                ->useAttributeAsKey('name')
146
                                ->prototype('variable')->end()
147
                            ->end()
148
                        ->end()
149
                    ->end()
150
                ->end()
151
                ->arrayNode('controller')
152
                    ->addDefaultsIfNotSet()
153
                    ->children()
154
                        ->scalarNode('filter_action')->defaultValue(sprintf('%s::filterAction', ImagineController::class))->end()
155
                        ->scalarNode('filter_runtime_action')->defaultValue(sprintf('%s::filterRuntimeAction', ImagineController::class))->end()
156
                        ->integerNode('redirect_response_code')->defaultValue(301)
157
                            ->validate()
158
                                ->ifTrue(function ($redirectResponseCode) {
159
                                    return !\in_array($redirectResponseCode, ControllerConfig::REDIRECT_RESPONSE_CODES, true);
160
                                })
161
                                ->thenInvalid('Invalid redirect response code "%s" (must be 201, 301, 302, 303, 307, or 308).')
162
                            ->end()
163
                        ->end()
164
                    ->end()
165
                ->end()
166
                ->arrayNode('filter_sets')
167
                    ->useAttributeAsKey('name')
168
                    ->prototype('array')
169
                        ->fixXmlConfig('filter', 'filters')
170
                        ->children()
171
                            ->scalarNode('quality')->end()
172
                            ->scalarNode('jpeg_quality')->end()
173
                            ->scalarNode('png_compression_level')->end()
174
                            ->scalarNode('png_compression_filter')->end()
175
                            ->scalarNode('format')->end()
176
                            ->booleanNode('animated')->end()
177
                            ->scalarNode('cache')->end()
178
                            ->scalarNode('data_loader')->end()
179
                            ->scalarNode('default_image')->end()
180
                            ->arrayNode('filters')
181
                                ->useAttributeAsKey('name')
182
                                ->prototype('array')
183
                                    ->useAttributeAsKey('name')
184
                                    ->prototype('variable')->end()
185
                                ->end()
186
                            ->end()
187
                            ->arrayNode('post_processors')
188
                                ->defaultValue([])
189
                                ->useAttributeAsKey('name')
190
                                ->prototype('array')
191
                                    ->useAttributeAsKey('name')
192
                                    ->prototype('variable')->end()
193
                                ->end()
194
                        ->end()
195
                    ->end()
196
                ->end()
197
            ->end()
198
            ->booleanNode('enqueue')
199
                ->defaultFalse()
200
                ->info('Enables integration with enqueue if set true. Allows resolve image caches in background by sending messages to MQ.')
201
            ->end()
202
            ->booleanNode('templating')
203
                ->defaultTrue()
204
                ->info('Enables integration with symfony/templating component')
205
                ->validate()
206
                    ->ifTrue()
207
                    ->then(function ($v) {
208
                        @trigger_error('Symfony templating integration has been deprecated since LiipImagineBundle 2.2 and will be removed in 3.0. Use Twig and use "false" as "liip_imagine.templating" value 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...
209
210
                        return $v;
211
                    })
212
                ->end()
213
            ->end()
214
        ->end();
215
216
        $rootNode
217
            ->children()
218
                ->arrayNode('webp')
219
                    ->addDefaultsIfNotSet()
220
                    ->children()
221
                        ->booleanNode('generate')->defaultFalse()->end()
222
                        ->integerNode('quality')->defaultValue(100)->end()
223
                        ->scalarNode('cache')->defaultNull()->end()
224
                        ->scalarNode('data_loader')->defaultNull()->end()
225
                        ->arrayNode('post_processors')
226
                            ->defaultValue([])
227
                            ->useAttributeAsKey('name')
228
                            ->prototype('array')
229
                                ->useAttributeAsKey('name')
230
                                ->prototype('variable')->end()
231
                            ->end()
232
                    ->end()
233
                ->end()
234
            ->end();
235
236
        return $treeBuilder;
237
    }
238
239
    private function addResolversSections(ArrayNodeDefinition $resolversPrototypeNode)
240
    {
241
        $this->addConfigurationSections($this->resolversFactories, $resolversPrototypeNode);
242
    }
243
244
    private function addLoadersSections(ArrayNodeDefinition $resolversPrototypeNode)
245
    {
246
        $this->addConfigurationSections($this->loadersFactories, $resolversPrototypeNode);
247
    }
248
249
    private function addConfigurationSections(array $factories, ArrayNodeDefinition $definition)
250
    {
251
        foreach ($factories as $f) {
252
            $f->addConfiguration($definition->children()->arrayNode($f->getName()));
253
        }
254
    }
255
}
256