Completed
Pull Request — master (#732)
by 12345
03:41
created

Configuration   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 168
Duplicated Lines 3.57 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 1
cbo 8
dl 6
loc 168
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addResolversSections() 0 8 2
A addLoadersSections() 0 8 2
D getConfigTreeBuilder() 6 118 10

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
namespace Liip\ImagineBundle\DependencyInjection;
4
5
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\LoaderFactoryInterface;
6
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface;
7
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
class Configuration implements ConfigurationInterface
12
{
13
    /**
14
     * @var ResolverFactoryInterface[]
15
     */
16
    protected $resolversFactories;
17
18
    /**
19
     * @var LoaderFactoryInterface[]
20
     */
21
    protected $loadersFactories;
22
23
    /**
24
     * @param ResolverFactoryInterface[] $resolversFactories
25
     * @param LoaderFactoryInterface[]   $loadersFactories
26
     */
27
    public function __construct(array $resolversFactories, array $loadersFactories)
28
    {
29
        $this->resolversFactories = $resolversFactories;
30
        $this->loadersFactories = $loadersFactories;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getConfigTreeBuilder()
37
    {
38
        $treeBuilder = new TreeBuilder();
39
        $rootNode = $treeBuilder->root('liip_imagine', 'array');
40
41
        $resolversPrototypeNode = $rootNode
42
            ->children()
43
                ->arrayNode('resolvers')
44
                    ->useAttributeAsKey('name')
45
                    ->prototype('array')
46
                        ->performNoDeepMerging()
47
        ;
48
        $this->addResolversSections($resolversPrototypeNode);
49
50
        $loadersPrototypeNode = $rootNode
51
            ->children()
52
                ->arrayNode('loaders')
53
                    ->useAttributeAsKey('name')
54
                    ->prototype('array')
55
        ;
56
        $this->addLoadersSections($loadersPrototypeNode);
57
58
        $rootNode
59
            ->beforeNormalization()
60
                ->ifTrue(function ($v) {
61
                    return
62
                        empty($v['loaders']) ||
63
                        empty($v['loaders']['default']) ||
64
                        empty($v['resolvers']) ||
65
                        empty($v['resolvers']['default'])
66
                    ;
67
                })
68
                ->then(function ($v) {
69
                    if (empty($v['loaders'])) {
70
                        $v['loaders'] = array();
71
                    }
72
73
                    if (false == is_array($v['loaders'])) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
74
                        throw new \LogicException('Loaders has to be array');
75
                    }
76
77 View Code Duplication
                    if (false == array_key_exists('default', $v['loaders'])) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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...
78
                        $v['loaders']['default'] = array('filesystem' => null);
79
                    }
80
81
                    if (empty($v['resolvers'])) {
82
                        $v['resolvers'] = array();
83
                    }
84
85
                    if (false == is_array($v['resolvers'])) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
86
                        throw new \LogicException('Resolvers has to be array');
87
                    }
88
89 View Code Duplication
                    if (false == array_key_exists('default', $v['resolvers'])) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
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...
90
                        $v['resolvers']['default'] = array('web_path' => null);
91
                    }
92
93
                    return $v;
94
                })
95
            ->end()
96
        ;
97
98
        $rootNode
99
            ->fixXmlConfig('filter_set', 'filter_sets')
100
            ->children()
101
                ->scalarNode('driver')->defaultValue('gd')
102
                    ->validate()
103
                        ->ifTrue(function ($v) { return !in_array($v, array('gd', 'imagick', 'gmagick')); })
104
                        ->thenInvalid('Invalid imagine driver specified: %s')
105
                    ->end()
106
                ->end()
107
                ->scalarNode('cache')->defaultValue('default')->end()
108
                ->scalarNode('cache_base_path')->defaultValue('')->end()
109
                ->scalarNode('data_loader')->defaultValue('default')->end()
110
                ->scalarNode('default_image')->defaultNull()->end()
111
                ->arrayNode('controller')
112
                    ->addDefaultsIfNotSet()
113
                    ->children()
114
                        ->scalarNode('filter_action')->defaultValue('liip_imagine.controller:filterAction')->end()
115
                        ->scalarNode('filter_runtime_action')->defaultValue('liip_imagine.controller:filterRuntimeAction')->end()
116
                    ->end()
117
                ->end()
118
                ->arrayNode('filter_sets')
119
                    ->useAttributeAsKey('name')
120
                    ->prototype('array')
121
                        ->fixXmlConfig('filter', 'filters')
122
                        ->children()
123
                            ->scalarNode('quality')->defaultValue(100)->end()
124
                            ->scalarNode('jpeg_quality')->defaultNull()->end()
125
                            ->scalarNode('png_compression_level')->defaultNull()->end()
126
                            ->scalarNode('png_compression_filter')->defaultNull()->end()
127
                            ->scalarNode('format')->defaultNull()->end()
128
                            ->booleanNode('animated')->defaultFalse()->end()
129
                            ->scalarNode('cache')->defaultNull()->end()
130
                            ->scalarNode('data_loader')->defaultNull()->end()
131
                            ->scalarNode('default_image')->defaultNull()->end()
132
                            ->arrayNode('filters')
133
                                ->useAttributeAsKey('name')
134
                                ->prototype('array')
135
                                    ->useAttributeAsKey('name')
136
                                    ->prototype('variable')->end()
137
                                ->end()
138
                            ->end()
139
                            ->arrayNode('post_processors')
140
                                ->defaultValue(array())
141
                                ->useAttributeAsKey('name')
142
                                ->prototype('array')
143
                                    ->useAttributeAsKey('name')
144
                                    ->prototype('variable')->end()
145
                                ->end()
146
                        ->end()
147
                    ->end()
148
                ->end()
149
            ->end()
150
        ->end();
151
152
        return $treeBuilder;
153
    }
154
155
    /**
156
     * @param ArrayNodeDefinition $resolversPrototypeNode
157
     */
158
    protected function addResolversSections(ArrayNodeDefinition $resolversPrototypeNode)
159
    {
160
        foreach ($this->resolversFactories as $factory) {
161
            $factory->addConfiguration(
162
                $resolversPrototypeNode->children()->arrayNode($factory->getName())
163
            );
164
        }
165
    }
166
167
    /**
168
     * @param ArrayNodeDefinition $resolversPrototypeNode
169
     */
170
    protected function addLoadersSections(ArrayNodeDefinition $resolversPrototypeNode)
171
    {
172
        foreach ($this->loadersFactories as $factory) {
173
            $factory->addConfiguration(
174
                $resolversPrototypeNode->children()->arrayNode($factory->getName())
175
            );
176
        }
177
    }
178
}
179