Completed
Pull Request — master (#80)
by
unknown
123:09 queued 58:07
created

ONGRTranslationsExtension::load()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 10
Bugs 1 Features 6
Metric Value
c 10
b 1
f 6
dl 0
loc 24
ccs 18
cts 18
cp 1
rs 8.9713
cc 2
eloc 18
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[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 ONGR\TranslationsBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
19
use Symfony\Component\DependencyInjection\Reference;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
22
/**
23
 * This is the class that loads and manages bundle configuration.
24
 */
25
class ONGRTranslationsExtension extends Extension
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 2
    public function load(array $configs, ContainerBuilder $container)
31
    {
32 2
        $configuration = new Configuration();
33 2
        $config = $this->processConfiguration($configuration, $configs);
34
35 2
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
36 2
        $loader->load('services.yml');
37 2
        $loader->load('filters_container.yml');
38
39 2
        $container->setParameter('ongr_translations.managed_locales', $config['managed_locales']);
40 2
        $container->setParameter('ongr_translations.formats', $config['formats']);
41 2
        $container->setParameter('ongr_translations.domains', $config['domains']);
42 2
        $this->validateBundles($container, $config['bundles']);
43
44 1
        $this->setElasticsearchStorage($config['repository'], $container);
45 1
        $this->setFiltersManager($config['repository'], $container);
46 1
        $this->setTranslationManager($config['repository'], $container);
47 1
        $this->setControllerManager($config['repository'], 'list', $container);
48 1
        $this->setControllerManager($config['repository'], 'translation', $container);
49 1
        $this->setHistoryManager($this->editRepositoryName($config['repository']), $container);
50 1
        if ($config['history']) {
51
            $this->setEditMessageEvent($this->editRepositoryName($config['repository']), $container);
52 1
        }
53
    }
54
55
    /**
56
     * Adds translations manager.
57
     *
58
     * @param string           $repositoryId Elasticsearch repository id.
59
     * @param ContainerBuilder $container    Service container.
60 1
     */
61
    private function setTranslationManager($repositoryId, ContainerBuilder $container)
62 1
    {
63 1
        $definition = new Definition(
64
            'ONGR\TranslationsBundle\Translation\TranslationManager',
65 1
            [
66 1
                new Reference($repositoryId),
67
                new Reference('event_dispatcher'),
68
            ]
69
        );
70 1
71 1
        $container->setDefinition('ongr_translations.translation_manager', $definition);
72
    }
73
74
    /**
75
     * Adds history manager.
76
     *
77
     * @param string           $repositoryId
78
     * @param ContainerBuilder $container
79 1
     */
80
    private function setHistoryManager($repositoryId, ContainerBuilder $container)
81 1
    {
82 1
        $definition = new Definition(
83
            'ONGR\TranslationsBundle\Translation\HistoryManager',
84 1
            [
85
                new Reference($repositoryId),
86
            ]
87
        );
88 1
89 1
        $container->setDefinition('ongr_translations.history_manager', $definition);
90
    }
91
92
    /**
93
     * Sets elasticsearch storage for translations.
94
     *
95
     * @param string           $repositoryId Elasticsearch repository id.
96
     * @param ContainerBuilder $container    Service container.
97 1
     */
98
    private function setElasticsearchStorage($repositoryId, ContainerBuilder $container)
99 1
    {
100 1
        $definition = new Definition(
101
            'ONGR\TranslationsBundle\Storage\ElasticsearchStorage',
102 1
            [
103
                new Reference($repositoryId),
104
            ]
105
        );
106 1
107 1
        $container->setDefinition('ongr_translations.storage', $definition);
108
    }
109
110
    /**
111
     * Adds filter manager for displaying translations gui.
112
     *
113
     * @param string           $repositoryId Elasticsearch repository id.
114
     * @param ContainerBuilder $container    Service container.
115 1
     */
116
    private function setFiltersManager($repositoryId, ContainerBuilder $container)
117 1
    {
118 1
        $definition = new Definition(
119
            'ONGR\FilterManagerBundle\Search\FilterManager',
120 1
            [
121 1
                new Reference('ongr_translations.filters_container'),
122
                new Reference($repositoryId),
123
            ]
124
        );
125 1
126 1
        $container->setDefinition('ongr_translations.filter_manager', $definition);
127
    }
128
129
    /**
130
     * Injects elasticsearch repository to controller and sets it into service container.
131
     *
132
     * @param string           $repositoryId   Elasticsearch repository id.
133
     * @param string           $controllerName Controller name to which add repository.
134
     * @param ContainerBuilder $container      Service container.
135 1
     */
136
    private function setControllerManager($repositoryId, $controllerName, ContainerBuilder $container)
137 1
    {
138 1
        $definition = new Definition(
139
            sprintf('ONGR\TranslationsBundle\Controller\%sController', ucfirst($controllerName)),
140 1
            [
141
                new Reference($repositoryId),
142
            ]
143 1
        );
144
        $definition->addMethodCall('setContainer', [new Reference('service_container')]);
145 1
146 1
        $container->setDefinition(sprintf('ongr_translations.controller.%s', $controllerName), $definition);
147
    }
148
149
    /**
150
     * Validates configured bundles and sets into service container as parameter.
151
     *
152
     * @param ContainerBuilder $container Service container.
153
     * @param array            $bundles   Bundles array.
154
     *
155
     * @throws InvalidConfigurationException
156 2
     */
157
    private function validateBundles($container, $bundles)
158 2
    {
159 2
        foreach ($bundles as $bundle) {
160 1
            if (!class_exists($bundle)) {
161 2
                throw new InvalidConfigurationException(
162
                    "Invalid bundle namespace '{$bundle}'."
163
                );
164
            }
165 1
        }
166 1
        $container->setParameter('ongr_translations.bundles', $bundles);
167
    }
168
169
    /**
170
     * Validates edit message event.
171
     *
172
     * @param string           $repositoryId
173
     * @param ContainerBuilder $container
174 1
     */
175
    private function setEditMessageEvent($repositoryId, ContainerBuilder $container)
176 1
    {
177 1
        $definition = new Definition(
178
            'ONGR\TranslationsBundle\Event\HistoryListener',
179 1
            [
180
                new Reference($repositoryId),
181
            ]
182 1
        );
183 1
        $definition->addTag(
184 1
            'kernel.event_listener',
185
            ['event' => 'translation.history.add', 'method' => 'addToHistory']
186 1
        );
187 1
        $container->setDefinition('ongr_translations.es_manager', $definition);
188
    }
189
190
    /**
191
     * Edits repository name.
192
     *
193
     * @param string $repository
194
     *
195
     * @return string
196 1
     */
197
    private function editRepositoryName($repository)
198 1
    {
199
        return substr_replace($repository, 'history', strrpos($repository, '.') + 1);
200
    }
201
}
202