Completed
Push — master ( a06a95...a1d542 )
by dan
15s
created

Configuration::validateStyleKeys()   C

Complexity

Conditions 8
Paths 26

Size

Total Lines 25
Code Lines 13

Duplication

Lines 7
Ratio 28 %

Importance

Changes 0
Metric Value
dl 7
loc 25
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 13
nc 26
nop 1
1
<?php
2
/**
3
 * This file is part of the IrishDan\ResponsiveImageBundle package.
4
 *
5
 * (c) Daniel Byrne <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace IrishDan\ResponsiveImageBundle\DependencyInjection;
11
12
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
13
use Symfony\Component\Config\Definition\ConfigurationInterface;
14
15
/**
16
 * Class Configuration
17
 *
18
 * @package ResponsiveImageBundle\DependencyInjection
19
 */
20
class Configuration implements ConfigurationInterface
21
{
22
    /**
23
     * The root configuration for responsive image bundle.
24
     *
25
     * @return TreeBuilder
26
     */
27
    public function getConfigTreeBuilder()
28
    {
29
        $treeBuilder = new TreeBuilder();
30
        $rootNode = $treeBuilder->root('responsive_image');
31
32
        // Add basic configurations.
33
        $rootNode
34
            ->validate()
35
                ->ifTrue(function($config) {
36
                    return $this->validateBreakpointKeys($config);
37
                })
38
                ->thenInvalid('Undefined breakpoint key detected in picture_set or size_set definitions')
39
            ->end()
40
            ->validate()
41
                ->ifTrue(function($config) {
42
                    return $this->validateStyleKeys($config);
43
                })
44
                ->thenInvalid('Undefined style key detected in picture_set or size_set definitions')
45
            ->end()
46
            ->children()
47
                ->booleanNode('cache_bust')
48
                    ->defaultFalse()
49
                ->end()
50
                ->variableNode('image_entity_class')
51
                    ->defaultValue([])
52
                ->end()
53
                ->enumNode('image_driver')
54
                    ->defaultValue('gd')
55
                    ->values(['gd', 'imagick'])
56
                ->end()
57
                ->integerNode('image_compression')
58
                    ->defaultValue(90)
59
                    ->min(0)->max(100)
60
                ->end()
61
                ->scalarNode('image_directory')
62
                    ->defaultValue('images')
63
                ->end()
64
                ->scalarNode('image_styles_directory')
65
                    ->defaultValue('styles')
66
                ->end()
67
                ->arrayNode('breakpoints')
68
                    ->useAttributeAsKey('name')
69
                    ->prototype('array')
70
                        ->children()
71
                            ->scalarNode('media_query')->end()
72
                        ->end()
73
                    ->end()
74
                ->end()
75
                ->arrayNode('image_styles')
76
                    ->useAttributeAsKey('name')
77
                    ->prototype('array')
78
                        ->children()
79
                            ->integerNode('width')
80
                                ->min(1)
81
                            ->end()
82
                            ->integerNode('height')
83
                                ->min(1)
84
                            ->end()
85
                            ->enumNode('effect')
86
                                ->defaultValue('scale')
87
                                ->values(['scale', 'crop'])
88
                            ->end()
89
                            ->booleanNode('greyscale')
90
                                ->defaultFalse()
91
                            ->end()
92
                        ->end()
93
                    ->end()
94
                ->end()
95
                ->arrayNode('picture_sets')
96
                    ->useAttributeAsKey('name')
97
                    ->prototype('array')
98
                        ->children()
99
                            ->scalarNode('fallback')->end()
100
                            ->booleanNode('multipliers')->end()
101
                            ->arrayNode('sources')
102
                                ->prototype('scalar')->end()
103
                            ->end()
104
                        ->end()
105
                    ->end()
106
                ->end()
107
                ->arrayNode('size_sets')
108
                    ->useAttributeAsKey('name')
109
                    ->prototype('array')
110
                        ->children()
111
                            ->scalarNode('fallback')->end()
112
                            ->arrayNode('sizes')
113
                                ->useAttributeAsKey('name')
114
                                ->prototype('array')
115
                                    ->children()
116
                                        ->scalarNode('breakpoint')->end()
117
                                        ->scalarNode('calc')->end()
118
                                    ->end()
119
                                ->end()
120
                            ->end()
121
                            ->arrayNode('srcsets')
122
                                ->prototype('scalar')->end()
123
                            ->end()
124
                        ->end()
125
                    ->end()
126
                ->end()
127
                ->arrayNode('crop_focus_widget')
128
                    ->children()
129
                        ->booleanNode('include_js_css')
130
                            ->defaultTrue()
131
                        ->end()
132
                        ->booleanNode('display_coordinates')
133
                            ->defaultTrue()
134
                        ->end()
135
                    ->end()
136
                ->end()
137
            ->end()
138
        ;
139
140
        return $treeBuilder;
141
    }
142
143
    /**
144
     * @param $config
145
     *
146
     * @return bool
147
     */
148
    private function validateStyleKeys($config) {
149
        // Get a list of image styles and break points to validate against.
150
        $styles = [];
151
        if (!empty($config['image_styles'])) {
152
            $styles = array_keys($config['image_styles']);
153
        }
154
155 View Code Duplication
        foreach ($config['picture_sets'] as $pictureSet) {
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...
156
            foreach ($pictureSet['sources'] as $breakpoint => $style) {
157
                if (!in_array($style, $styles)) {
158
                    return true;
159
                }
160
            }
161
        }
162
163
        foreach ($config['size_sets'] as $sizeSet) {
164
            foreach ($sizeSet['srcsets'] as $style) {
165
                if (!in_array($style, $styles)) {
166
                    return true;
167
                }
168
            }
169
        }
170
171
        return false;
172
    }
173
174
    /**
175
     * @param $config
176
     *
177
     * @return bool
178
     */
179
    private function validateBreakpointKeys($config) {
180
        // Get a list of breakpoints to validate against.
181
        $breakpoints = [];
182
        if (!empty($config['breakpoints'])) {
183
            $breakpoints = array_keys($config['breakpoints']);
184
        }
185
186
        // Validate that the picture sets are defined using valid breakpoints
187 View Code Duplication
        foreach ($config['picture_sets'] as $pictureSet) {
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...
188
            foreach ($pictureSet['sources'] as $breakpoint => $style) {
189
                if (!in_array($breakpoint, $breakpoints)) {
190
                    return true;
191
                }
192
            }
193
        }
194
195
        // Validate that the picture sets are defined using valid breakpoints
196
        foreach ($config['size_sets'] as $sizeSet) {
197
            foreach ($sizeSet['sizes'] as $size) {
198
                $breakpoint = empty($size['breakpoint']) ? null : $size['breakpoint'];
199
                if ($breakpoint) {
200
                    if (!in_array($breakpoint, $breakpoints)) {
201
                        return true;
202
                    }
203
                }
204
            }
205
        }
206
207
        return false;
208
    }
209
}