IvoryCKEditorExtension::registerConfig()   F
last analyzed

Complexity

Conditions 12
Paths 2048

Size

Total Lines 48
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 36
cts 36
cp 1
rs 2.5843
c 0
b 0
f 0
cc 12
eloc 24
nc 2048
nop 2
crap 12

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
/*
4
 * This file is part of the Ivory CKEditor package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\CKEditorBundle\DependencyInjection;
13
14
use Ivory\CKEditorBundle\Exception\DependencyInjectionException;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18
use Symfony\Component\Form\AbstractType;
19
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
20
21
/**
22
 * @author GeLo <[email protected]>
23
 */
24
class IvoryCKEditorExtension extends ConfigurableExtension
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 210
    protected function loadInternal(array $config, ContainerBuilder $container)
30
    {
31 210
        $this->loadResources($container);
32 210
        $this->registerConfig($config, $container);
33
34 210
        if (!isset($config['enable']) || $config['enable']) {
35 200
            $this->registerConfigs($config, $container);
36 190
            $this->registerPlugins($config, $container);
37 190
            $this->registerStylesSet($config, $container);
38 190
            $this->registerTemplates($config, $container);
39 190
            $this->registerToolbars($config, $container);
40 190
            $this->registerFilebrowsers($config, $container);
41 152
        }
42
43 200
        if (!method_exists(AbstractType::class, 'getBlockPrefix')) {
44 80
            $container->getDefinition('ivory_ck_editor.form.type')
45 80
                ->clearTag('form.type')
46 80
                ->addTag('form.type', ['alias' => 'ckeditor']);
47 40
        }
48 200
    }
49
50
    /**
51
     * @param ContainerBuilder $container
52
     */
53 210
    private function loadResources(ContainerBuilder $container)
54
    {
55 210
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
56
57
        $resources = [
58 210
            'command',
59 168
            'form',
60 168
            'installer',
61 168
            'renderer',
62 168
            'templating',
63 168
            'twig',
64 168
        ];
65
66 210
        foreach ($resources as $resource) {
67 210
            $loader->load($resource.'.xml');
68 168
        }
69 210
    }
70
71
    /**
72
     * @param array            $config
73
     * @param ContainerBuilder $container
74
     */
75 210
    private function registerConfig(array $config, ContainerBuilder $container)
76
    {
77 210
        $formType = $container->getDefinition('ivory_ck_editor.form.type');
78
79 210
        if (isset($config['enable'])) {
80 10
            $formType->addMethodCall('isEnable', [$config['enable']]);
81 8
        }
82
83 210
        if (isset($config['async'])) {
84 10
            $formType->addMethodCall('isAsync', [$config['async']]);
85 8
        }
86
87 210
        if (isset($config['auto_inline'])) {
88 10
            $formType->addMethodCall('isAutoInline', [$config['auto_inline']]);
89 8
        }
90
91 210
        if (isset($config['inline'])) {
92 10
            $formType->addMethodCall('isInline', [$config['inline']]);
93 8
        }
94
95 210
        if (isset($config['autoload'])) {
96 10
            $formType->addMethodCall('isAutoload', [$config['autoload']]);
97 8
        }
98
99 210
        if (isset($config['jquery'])) {
100 10
            $formType->addMethodCall('useJquery', [$config['jquery']]);
101 8
        }
102
103 210
        if (isset($config['require_js'])) {
104 10
            $formType->addMethodCall('useRequireJs', [$config['require_js']]);
105 8
        }
106
107 210
        if (isset($config['input_sync'])) {
108 10
            $formType->addMethodCall('isInputSync', [$config['input_sync']]);
109 8
        }
110
111 210
        if (isset($config['base_path'])) {
112 10
            $formType->addMethodCall('setBasePath', [$config['base_path']]);
113 8
        }
114
115 210
        if (isset($config['js_path'])) {
116 10
            $formType->addMethodCall('setJsPath', [$config['js_path']]);
117 8
        }
118
119 210
        if (isset($config['jquery_path'])) {
120 10
            $formType->addMethodCall('setJqueryPath', [$config['jquery_path']]);
121 8
        }
122 210
    }
123
124
    /**
125
     * @param array            $config
126
     * @param ContainerBuilder $container
127
     *
128
     * @throws DependencyInjectionException
129
     */
130 200
    private function registerConfigs(array $config, ContainerBuilder $container)
131
    {
132 200
        if (empty($config['configs'])) {
133 150
            return;
134
        }
135
136 50
        $definition = $container->getDefinition('ivory_ck_editor.config_manager');
137 50
        $definition->addMethodCall('setConfigs', [$config['configs']]);
138
139 50
        if (!isset($config['default_config']) && !empty($config['configs'])) {
140 10
            reset($config['configs']);
141 10
            $config['default_config'] = key($config['configs']);
142 8
        }
143
144 50
        if (isset($config['default_config'])) {
145 50
            if (!isset($config['configs'][$config['default_config']])) {
146 10
                throw DependencyInjectionException::invalidDefaultConfig($config['default_config']);
147
            }
148
149 40
            $definition->addMethodCall('setDefaultConfig', [$config['default_config']]);
150 32
        }
151 40
    }
152
153
    /**
154
     * @param array            $config
155
     * @param ContainerBuilder $container
156
     */
157 190
    private function registerPlugins(array $config, ContainerBuilder $container)
158
    {
159 190
        if (!empty($config['plugins'])) {
160
            $container
161 10
                ->getDefinition('ivory_ck_editor.plugin_manager')
162 10
                ->addMethodCall('setPlugins', [$config['plugins']]);
163 8
        }
164 190
    }
165
166
    /**
167
     * @param array            $config
168
     * @param ContainerBuilder $container
169
     */
170 190
    private function registerStylesSet(array $config, ContainerBuilder $container)
171
    {
172 190
        if (empty($config['styles'])) {
173 180
            return;
174
        }
175
176 10
        $stylesSets = $config['styles'];
177
178 10
        foreach ($stylesSets as &$stylesSet) {
179 10
            foreach ($stylesSet as &$value) {
180 10
                $value = array_filter($value);
181 8
            }
182 8
        }
183
184
        $container
185 10
            ->getDefinition('ivory_ck_editor.styles_set_manager')
186 10
            ->addMethodCall('setStylesSets', [$stylesSets]);
187 10
    }
188
189
    /**
190
     * @param array            $config
191
     * @param ContainerBuilder $container
192
     */
193 190
    private function registerTemplates(array $config, ContainerBuilder $container)
194
    {
195 190
        if (!empty($config['templates'])) {
196
            $container
197 10
                ->getDefinition('ivory_ck_editor.template_manager')
198 10
                ->addMethodCall('setTemplates', [$config['templates']]);
199 8
        }
200 190
    }
201
202
    /**
203
     * @param array            $config
204
     * @param ContainerBuilder $container
205
     */
206 190
    private function registerToolbars(array $config, ContainerBuilder $container)
207
    {
208 190
        $definition = $container->getDefinition('ivory_ck_editor.toolbar_manager');
209
210 190
        if (!empty($config['toolbars']['items'])) {
211 30
            $definition->addMethodCall('setItems', [$config['toolbars']['items']]);
212 24
        }
213
214 190
        if (!empty($config['toolbars']['configs'])) {
215 30
            $definition->addMethodCall('setToolbars', [$config['toolbars']['configs']]);
216 24
        }
217 190
    }
218
219
    /**
220
     * @param array            $config
221
     * @param ContainerBuilder $container
222
     */
223 190
    private function registerFilebrowsers(array $config, ContainerBuilder $container)
224
    {
225 190
        if (!empty($config['filebrowsers'])) {
226
            $container
227 10
                ->getDefinition('ivory_ck_editor.form.type')
228 10
                ->addMethodCall('setFilebrowsers', [$config['filebrowsers']]);
229 8
        }
230 190
    }
231
}
232