Completed
Pull Request — 2.x (#361)
by Christian
01:12
created

SonataSeoExtension::configureHttpClient()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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