Completed
Pull Request — 2.x (#225)
by Jeroen
01:18
created

SonataSeoExtension::fixConfiguration()   F

Complexity

Conditions 16
Paths 712

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 46
rs 2.8984
cc 16
eloc 28
nc 712
nop 1

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 Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\SeoBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Loader;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
22
/**
23
 * This is the class that loads and manages your bundle configuration.
24
 */
25
class SonataSeoExtension extends Extension
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function load(array $configs, ContainerBuilder $container)
31
    {
32
        $configuration = new Configuration();
33
        $config = $this->processConfiguration($configuration, $configs);
34
        $config = $this->fixConfiguration($config);
35
36
        $bundles = $container->getParameter('kernel.bundles');
37
38
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
39
40
        if (isset($bundles['SonataBlockBundle']) && isset($bundles['KnpMenuBundle'])) {
41
            $loader->load('blocks.xml');
42
        }
43
44
        $loader->load('event.xml');
45
        $loader->load('services.xml');
46
47
        $this->configureSeoPage($config['page'], $container);
48
        $this->configureSitemap($config['sitemap'], $container);
49
50
        $container->getDefinition('sonata.seo.twig.extension')
51
            ->replaceArgument(1, $config['encoding']);
52
    }
53
54
    /**
55
     * Configure the default seo page.
56
     *
57
     * @param array            $config
58
     * @param ContainerBuilder $container
59
     */
60
    protected function configureSeoPage(array $config, ContainerBuilder $container)
61
    {
62
        $definition = $container->getDefinition($config['default']);
63
64
        $definition->addMethodCall('setTitle', [$config['title']]);
65
        $definition->addMethodCall('setMetas', [$config['metas']]);
66
        $definition->addMethodCall('setHtmlAttributes', [$config['head']]);
67
        $definition->addMethodCall('setSeparator', [$config['separator']]);
68
69
        $container->setAlias('sonata.seo.page', $config['default']);
70
    }
71
72
    /**
73
     * Configure the sitemap source manager.
74
     *
75
     * @param array            $config
76
     * @param ContainerBuilder $container
77
     */
78
    protected function configureSitemap(array $config, ContainerBuilder $container)
79
    {
80
        $source = $container->getDefinition('sonata.seo.sitemap.manager');
81
82
        if (method_exists($source, 'setShared')) { // Symfony 2.8+
83
            $source->setShared(false);
84
        } else {
85
            // For Symfony <2.8 compatibility
86
            $source->setScope(ContainerInterface::SCOPE_PROTOTYPE);
0 ignored issues
show
Bug introduced by
The method setScope() does not seem to exist on object<Symfony\Component...cyInjection\Definition>.

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...
87
        }
88
89
        foreach ($config['doctrine_orm'] as $pos => $sitemap) {
90
            // define the connectionIterator
91
            $connectionIteratorId = 'sonata.seo.source.doctrine_connection_iterator_'.$pos;
92
93
            $connectionIterator = new Definition('%sonata.seo.exporter.database_source_iterator.class%', [
94
                new Reference($sitemap['connection']),
95
                $sitemap['query'],
96
            ]);
97
98
            $connectionIterator->setPublic(false);
99
            $container->setDefinition($connectionIteratorId, $connectionIterator);
100
101
            // define the sitemap proxy iterator
102
            $sitemapIteratorId = 'sonata.seo.source.doctrine_sitemap_iterator_'.$pos;
103
104
            $sitemapIterator = new Definition('%sonata.seo.exporter.sitemap_source_iterator.class%', [
105
                new Reference($connectionIteratorId),
106
                new Reference('router'),
107
                $sitemap['route'],
108
                $sitemap['parameters'],
109
            ]);
110
111
            $sitemapIterator->setPublic(false);
112
113
            $container->setDefinition($sitemapIteratorId, $sitemapIterator);
114
115
            $source->addMethodCall('addSource', [$sitemap['group'], new Reference($sitemapIteratorId), $sitemap['types']]);
116
        }
117
118
        foreach ($config['services'] as $service) {
119
            $source->addMethodCall('addSource', [$service['group'], new Reference($service['id']), $service['types']]);
120
        }
121
    }
122
123
    /**
124
     * Fix the sitemap configuration.
125
     *
126
     * @param array $config
127
     *
128
     * @return array
129
     */
130
    protected function fixConfiguration(array $config)
131
    {
132
        foreach ($config['sitemap']['doctrine_orm'] as $pos => $sitemap) {
133
            $sitemap['group'] = isset($sitemap['group']) ? $sitemap['group'] : false;
134
            $sitemap['types'] = isset($sitemap['types']) ? $sitemap['types'] : [];
135
            $sitemap['connection'] = isset($sitemap['connection']) ? $sitemap['connection'] : 'doctrine.dbal.default_connection';
136
            $sitemap['route'] = isset($sitemap['route']) ? $sitemap['route'] : false;
137
            $sitemap['parameters'] = isset($sitemap['parameters']) ? $sitemap['parameters'] : false;
138
            $sitemap['query'] = isset($sitemap['query']) ? $sitemap['query'] : false;
139
140
            if ($sitemap['route'] === false) {
141
                throw new \RuntimeException('Route cannot be empty, please review the sonata_seo.sitemap configuration');
142
            }
143
144
            if ($sitemap['query'] === false) {
145
                throw new \RuntimeException('Query cannot be empty, please review the sonata_seo.sitemap configuration');
146
            }
147
148
            if ($sitemap['parameters'] === false) {
149
                throw new \RuntimeException('Route\'s parameters cannot be empty, please review the sonata_seo.sitemap configuration');
150
            }
151
152
            $config['sitemap']['doctrine_orm'][$pos] = $sitemap;
153
        }
154
155
        foreach ($config['sitemap']['services'] as $pos => $sitemap) {
156
            if (!is_array($sitemap)) {
157
                $sitemap = [
158
                    'group' => false,
159
                    'types' => [],
160
                    'id' => $sitemap,
161
                ];
162
            } else {
163
                $sitemap['group'] = isset($sitemap['group']) ? $sitemap['group'] : false;
164
                $sitemap['types'] = isset($sitemap['types']) ? $sitemap['types'] : [];
165
166
                if (!isset($sitemap['id'])) {
167
                    throw new \RuntimeException('Service id must to be defined, please review the sonata_seo.sitemap configuration');
168
                }
169
            }
170
171
            $config['sitemap']['services'][$pos] = $sitemap;
172
        }
173
174
        return $config;
175
    }
176
}
177