Completed
Pull Request — 2.x (#161)
by Christian
01:20
created

SonataSeoExtension::configureHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\SeoBundle\DependencyInjection;
15
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Loader;
21
use Symfony\Component\DependencyInjection\Reference;
22
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
23
24
/**
25
 * This is the class that loads and manages your bundle configuration.
26
 */
27
class SonataSeoExtension extends Extension
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function load(array $configs, ContainerBuilder $container)
33
    {
34
        $configuration = new Configuration();
35
        $config = $this->processConfiguration($configuration, $configs);
36
        $config = $this->fixConfiguration($config);
37
38
        $bundles = $container->getParameter('kernel.bundles');
39
40
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
41
42
        if (isset($bundles['SonataBlockBundle'], $bundles['KnpMenuBundle'])) {
43
            $loader->load('blocks.xml');
44
        }
45
46
        $loader->load('event.xml');
47
        $loader->load('services.xml');
48
        $loader->load('commands.xml');
49
50
        $this->configureSeoPage($config['page'], $container);
51
        $this->configureSitemap($config['sitemap'], $container);
52
        $this->configureHttpClient($container, $config['http']);
53
54
        $container->getDefinition('sonata.seo.twig.extension')
55
            ->replaceArgument(1, $config['encoding']);
56
    }
57
58
    /**
59
     * Configure the default seo page.
60
     *
61
     * @param array            $config
62
     * @param ContainerBuilder $container
63
     */
64
    protected function configureSeoPage(array $config, ContainerBuilder $container)
65
    {
66
        $container->setParameter('sonata.seo.config', $config);
67
    }
68
69
    /**
70
     * Configure the sitemap source manager.
71
     *
72
     * @param array            $config
73
     * @param ContainerBuilder $container
74
     */
75
    protected function configureSitemap(array $config, ContainerBuilder $container)
76
    {
77
        $source = $container->getDefinition('sonata.seo.sitemap.manager');
78
79
        if (method_exists($source, 'setShared')) { // Symfony 2.8+
80
            $source->setShared(false);
81
        } else {
82
            // For Symfony <2.8 compatibility
83
            $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...
84
        }
85
86
        foreach ($config['doctrine_orm'] as $pos => $sitemap) {
87
            // define the connectionIterator
88
            $connectionIteratorId = 'sonata.seo.source.doctrine_connection_iterator_'.$pos;
89
90
            $connectionIterator = new Definition('%sonata.seo.exporter.database_source_iterator.class%', [
91
                new Reference($sitemap['connection']),
92
                $sitemap['query'],
93
            ]);
94
95
            $connectionIterator->setPublic(false);
96
            $container->setDefinition($connectionIteratorId, $connectionIterator);
97
98
            // define the sitemap proxy iterator
99
            $sitemapIteratorId = 'sonata.seo.source.doctrine_sitemap_iterator_'.$pos;
100
101
            $sitemapIterator = new Definition('%sonata.seo.exporter.sitemap_source_iterator.class%', [
102
                new Reference($connectionIteratorId),
103
                new Reference('router'),
104
                $sitemap['route'],
105
                $sitemap['parameters'],
106
            ]);
107
108
            $sitemapIterator->setPublic(false);
109
110
            $container->setDefinition($sitemapIteratorId, $sitemapIterator);
111
112
            $source->addMethodCall('addSource', [$sitemap['group'], new Reference($sitemapIteratorId), $sitemap['types']]);
113
        }
114
115
        foreach ($config['services'] as $service) {
116
            $source->addMethodCall('addSource', [$service['group'], new Reference($service['id']), $service['types']]);
117
        }
118
    }
119
120
    /**
121
     * Fix the sitemap configuration.
122
     *
123
     * @param array $config
124
     *
125
     * @return array
126
     */
127
    protected function fixConfiguration(array $config)
128
    {
129
        foreach ($config['sitemap']['doctrine_orm'] as $pos => $sitemap) {
130
            $sitemap['group'] = $sitemap['group'] ?? false;
131
            $sitemap['types'] = $sitemap['types'] ?? [];
132
            $sitemap['connection'] = $sitemap['connection'] ?? 'doctrine.dbal.default_connection';
133
            $sitemap['route'] = $sitemap['route'] ?? false;
134
            $sitemap['parameters'] = $sitemap['parameters'] ?? false;
135
            $sitemap['query'] = $sitemap['query'] ?? false;
136
137
            if (false === $sitemap['route']) {
138
                throw new \RuntimeException('Route cannot be empty, please review the sonata_seo.sitemap configuration');
139
            }
140
141
            if (false === $sitemap['query']) {
142
                throw new \RuntimeException('Query cannot be empty, please review the sonata_seo.sitemap configuration');
143
            }
144
145
            if (false === $sitemap['parameters']) {
146
                throw new \RuntimeException('Route\'s parameters cannot be empty, please review the sonata_seo.sitemap configuration');
147
            }
148
149
            $config['sitemap']['doctrine_orm'][$pos] = $sitemap;
150
        }
151
152
        foreach ($config['sitemap']['services'] as $pos => $sitemap) {
153
            if (!\is_array($sitemap)) {
154
                $sitemap = [
155
                    'group' => false,
156
                    'types' => [],
157
                    'id' => $sitemap,
158
                ];
159
            } else {
160
                $sitemap['group'] = $sitemap['group'] ?? false;
161
                $sitemap['types'] = $sitemap['types'] ?? [];
162
163
                if (!isset($sitemap['id'])) {
164
                    throw new \RuntimeException('Service id must to be defined, please review the sonata_seo.sitemap configuration');
165
                }
166
            }
167
168
            $config['sitemap']['services'][$pos] = $sitemap;
169
        }
170
171
        return $config;
172
    }
173
174
    private function configureHttpClient(ContainerBuilder $container, array $config): void
175
    {
176
        $container->setAlias('sonata.seo.http.client', $config['client']);
177
        $container->setAlias('sonata.seo.http.message_factory', $config['message_factory']);
178
    }
179
}
180