Passed
Pull Request — master (#138)
by Marko
15:23
created

FOSCKEditorExtension::resolveConfigs()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 5
nop 1
dl 0
loc 16
ccs 0
cts 12
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the FOSCKEditor Bundle.
5
 *
6
 * (c) 2018 - present  Friends of Symfony
7
 * (c) 2009 - 2017     Eric GELOEN <[email protected]>
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace FOS\CKEditorBundle\DependencyInjection;
14
15
use FOS\CKEditorBundle\Exception\DependencyInjectionException;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19
use Symfony\Component\Form\AbstractType;
20
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class FOSCKEditorExtension extends ConfigurableExtension
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function loadInternal(array $config, ContainerBuilder $container)
31
    {
32
        $this->loadResources($container);
33
34
        if (!isset($config['enable']) || $config['enable']) {
35
            $config = $this->resolveConfigs($config, $container);
0 ignored issues
show
Unused Code introduced by
The call to FOS\CKEditorBundle\Depen...nsion::resolveConfigs() has too many arguments starting with $container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
            /** @scrutinizer ignore-call */ 
36
            $config = $this->resolveConfigs($config, $container);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
36
            $config = $this->resolveStylesSet($config, $container);
0 ignored issues
show
Unused Code introduced by
The call to FOS\CKEditorBundle\Depen...ion::resolveStylesSet() has too many arguments starting with $container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
            /** @scrutinizer ignore-call */ 
37
            $config = $this->resolveStylesSet($config, $container);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
37
            $this->registerFilebrowsers($config, $container);
38
        }
39
40
        $container->getDefinition('fos_ck_editor.form.type')
41
            ->setArgument(0, $config);
42
43
        if (!method_exists(AbstractType::class, 'getBlockPrefix')) {
44
            $container->getDefinition('fos_ck_editor.form.type')
45
                ->clearTag('form.type')
46
                ->addTag('form.type', ['alias' => 'ckeditor']);
47
        }
48
49
        $bundles = $container->getParameter('kernel.bundles');
50
51
        if (isset($bundles['IvoryCKEditorBundle'])) {
52
            @trigger_error(
53
                "IvoryCKEditorBundle isn't maintained anymore and should be replaced with FOSCKEditorBundle.",
54
                E_USER_DEPRECATED
55
            );
56
        }
57
    }
58
59
    private function loadResources(ContainerBuilder $container)
60
    {
61
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
62
63
        $resources = [
64
            'builder',
65
            'command',
66
            'form',
67
            'installer',
68
            'renderer',
69
            'twig',
70
        ];
71
72
        foreach ($resources as $resource) {
73
            $loader->load($resource.'.xml');
74
        }
75
    }
76
77
    /**
78
     * @throws DependencyInjectionException
79
     */
80
    private function resolveConfigs(array $config): array
81
    {
82
        if (empty($config['configs'])) {
83
            return $config;
84
        }
85
86
        if (!isset($config['default_config']) && !empty($config['configs'])) {
87
            reset($config['configs']);
88
            $config['default_config'] = key($config['configs']);
89
        }
90
91
        if (isset($config['default_config']) && !isset($config['configs'][$config['default_config']])) {
92
            throw DependencyInjectionException::invalidDefaultConfig($config['default_config']);
93
        }
94
95
        return $config;
96
    }
97
98
    private function resolveStylesSet(array $config)
99
    {
100
        if (empty($config['styles'])) {
101
            return $config;
102
        }
103
104
        $stylesSets = $config['styles'];
105
106
        foreach ($stylesSets as &$stylesSet) {
107
            foreach ($stylesSet as &$value) {
108
                $value = array_filter($value);
109
            }
110
        }
111
112
        return $config;
113
    }
114
115
    /**
116
     * @param array            $config
117
     * @param ContainerBuilder $container
118
     */
119
    private function registerFilebrowsers(array $config, ContainerBuilder $container)
120
    {
121
        if (!empty($config['filebrowsers'])) {
122
            $container
123
                ->getDefinition('fos_ck_editor.form.type')
124
                ->addMethodCall('setFilebrowsers', [$config['filebrowsers']]);
125
        }
126
    }
127
128
    public function getAlias()
129
    {
130
        return 'fos_ck_editor';
131
    }
132
}
133