Completed
Push — 2.x-dev-kit ( 971030 )
by
unknown
07:52
created

SonataSeoExtension::fixConfiguration()   F

Complexity

Conditions 16
Paths 712

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
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
        $this->configureClassesToCompile();
50
51
        $container->getDefinition('sonata.seo.twig.extension')
52
            ->replaceArgument(1, $config['encoding']);
53
    }
54
55
    /**
56
     * Add class to compile.
57
     */
58
    public function configureClassesToCompile()
59
    {
60
        $this->addClassesToCompile(array(
61
            'Sonata\\SeoBundle\\Seo\\SeoPage',
62
            'Sonata\\SeoBundle\\Seo\\SeoPageInterface',
63
            'Sonata\\SeoBundle\\Sitemap\\SourceManager',
64
            'Sonata\\SeoBundle\\Twig\\Extension\\SeoExtension',
65
        ));
66
    }
67
68
    /**
69
     * Configure the default seo page.
70
     *
71
     * @param array            $config
72
     * @param ContainerBuilder $container
73
     */
74
    protected function configureSeoPage(array $config, ContainerBuilder $container)
75
    {
76
        $definition = $container->getDefinition($config['default']);
77
78
        $definition->addMethodCall('setTitle', array($config['title']));
79
        $definition->addMethodCall('setMetas', array($config['metas']));
80
        $definition->addMethodCall('setHtmlAttributes', array($config['head']));
81
        $definition->addMethodCall('setSeparator', array($config['separator']));
82
83
        $container->setAlias('sonata.seo.page', $config['default']);
84
    }
85
86
    /**
87
     * Configure the sitemap source manager.
88
     *
89
     * @param array            $config
90
     * @param ContainerBuilder $container
91
     */
92
    protected function configureSitemap(array $config, ContainerBuilder $container)
93
    {
94
        $source = $container->getDefinition('sonata.seo.sitemap.manager');
95
96
        if (method_exists($source, 'setShared')) { // Symfony 2.8+
97
            $source->setShared(false);
98
        } else {
99
            // For Symfony <2.8 compatibility
100
            $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...
101
        }
102
103
        foreach ($config['doctrine_orm'] as $pos => $sitemap) {
104
            // define the connectionIterator
105
            $connectionIteratorId = 'sonata.seo.source.doctrine_connection_iterator_'.$pos;
106
107
            $connectionIterator = new Definition('%sonata.seo.exporter.database_source_iterator.class%', array(
108
                new Reference($sitemap['connection']),
109
                $sitemap['query'],
110
            ));
111
112
            $connectionIterator->setPublic(false);
113
            $container->setDefinition($connectionIteratorId, $connectionIterator);
114
115
            // define the sitemap proxy iterator
116
            $sitemapIteratorId = 'sonata.seo.source.doctrine_sitemap_iterator_'.$pos;
117
118
            $sitemapIterator = new Definition('%sonata.seo.exporter.sitemap_source_iterator.class%', array(
119
                new Reference($connectionIteratorId),
120
                new Reference('router'),
121
                $sitemap['route'],
122
                $sitemap['parameters'],
123
            ));
124
125
            $sitemapIterator->setPublic(false);
126
127
            $container->setDefinition($sitemapIteratorId, $sitemapIterator);
128
129
            $source->addMethodCall('addSource', array($sitemap['group'], new Reference($sitemapIteratorId), $sitemap['types']));
130
        }
131
132
        foreach ($config['services'] as $service) {
133
            $source->addMethodCall('addSource', array($service['group'], new Reference($service['id']), $service['types']));
134
        }
135
    }
136
137
    /**
138
     * Fix the sitemap configuration.
139
     *
140
     * @param array $config
141
     *
142
     * @return array
143
     */
144
    protected function fixConfiguration(array $config)
145
    {
146
        foreach ($config['sitemap']['doctrine_orm'] as $pos => $sitemap) {
147
            $sitemap['group'] = isset($sitemap['group']) ? $sitemap['group'] : false;
148
            $sitemap['types'] = isset($sitemap['types']) ? $sitemap['types'] : array();
149
            $sitemap['connection'] = isset($sitemap['connection']) ? $sitemap['connection'] : 'doctrine.dbal.default_connection';
150
            $sitemap['route'] = isset($sitemap['route']) ? $sitemap['route'] : false;
151
            $sitemap['parameters'] = isset($sitemap['parameters']) ? $sitemap['parameters'] : false;
152
            $sitemap['query'] = isset($sitemap['query']) ? $sitemap['query'] : false;
153
154
            if ($sitemap['route'] === false) {
155
                throw new \RuntimeException('Route cannot be empty, please review the sonata_seo.sitemap configuration');
156
            }
157
158
            if ($sitemap['query'] === false) {
159
                throw new \RuntimeException('Query cannot be empty, please review the sonata_seo.sitemap configuration');
160
            }
161
162
            if ($sitemap['parameters'] === false) {
163
                throw new \RuntimeException('Route\'s parameters cannot be empty, please review the sonata_seo.sitemap configuration');
164
            }
165
166
            $config['sitemap']['doctrine_orm'][$pos] = $sitemap;
167
        }
168
169
        foreach ($config['sitemap']['services'] as $pos => $sitemap) {
170
            if (!is_array($sitemap)) {
171
                $sitemap = array(
172
                    'group' => false,
173
                    'types' => array(),
174
                    'id' => $sitemap,
175
                );
176
            } else {
177
                $sitemap['group'] = isset($sitemap['group']) ? $sitemap['group'] : false;
178
                $sitemap['types'] = isset($sitemap['types']) ? $sitemap['types'] : array();
179
180
                if (!isset($sitemap['id'])) {
181
                    throw new \RuntimeException('Service id must to be defined, please review the sonata_seo.sitemap configuration');
182
                }
183
            }
184
185
            $config['sitemap']['services'][$pos] = $sitemap;
186
        }
187
188
        return $config;
189
    }
190
}
191