Completed
Pull Request — master (#89)
by
unknown
62:49
created

ONGRTranslationsExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 114
ccs 47
cts 47
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 19 1
A setTranslationManager() 0 13 1
A setHistoryManager() 0 11 1
A setFiltersManager() 0 12 1
A validateBundles() 0 11 3
A editRepositoryName() 0 4 1
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
        $container->setAlias('ongr_translations.repository', $config['repository']);
43
        $this->validateBundles($container, $config['bundles']);
44 1
45 1
        $this->setFiltersManager($config['repository'], $container);
46 1
        $this->setTranslationManager($config['repository'], $container);
47 1
        $this->setHistoryManager($this->editRepositoryName($config['repository']), $container);
48 1
    }
49 1
50 1
    /**
51
     * Adds translations manager.
52 1
     *
53
     * @param string           $repositoryId Elasticsearch repository id.
54
     * @param ContainerBuilder $container    Service container.
55
     */
56
    private function setTranslationManager($repositoryId, ContainerBuilder $container)
57
    {
58
        $definition = new Definition(
59
            'ONGR\TranslationsBundle\Translation\TranslationManager',
60 1
            [
61
                new Reference($repositoryId),
62 1
                new Reference('ongr_translations.history_manager'),
63 1
                new Reference('event_dispatcher'),
64
            ]
65 1
        );
66 1
67
        $container->setDefinition('ongr_translations.translation_manager', $definition);
68
    }
69
70 1
    /**
71 1
     * Adds history manager.
72
     *
73
     * @param string           $repositoryId
74
     * @param ContainerBuilder $container
75
     */
76
    private function setHistoryManager($repositoryId, ContainerBuilder $container)
77
    {
78
        $definition = new Definition(
79 1
            'ONGR\TranslationsBundle\Translation\HistoryManager',
80
            [
81 1
                new Reference($repositoryId),
82 1
            ]
83
        );
84 1
85
        $container->setDefinition('ongr_translations.history_manager', $definition);
86
    }
87
88 1
    /**
89 1
     * Adds filter manager for displaying translations gui.
90
     *
91
     * @param string           $repositoryId Elasticsearch repository id.
92
     * @param ContainerBuilder $container    Service container.
93
     */
94
    private function setFiltersManager($repositoryId, ContainerBuilder $container)
95
    {
96
        $definition = new Definition(
97 1
            'ONGR\FilterManagerBundle\Search\FilterManager',
98
            [
99 1
                new Reference('ongr_translations.filters_container'),
100 1
                new Reference($repositoryId),
101
            ]
102 1
        );
103
104
        $container->setDefinition('ongr_translations.filter_manager', $definition);
105
    }
106 1
107 1
    /**
108
     * Validates configured bundles and sets into service container as parameter.
109
     *
110
     * @param ContainerBuilder $container Service container.
111
     * @param array            $bundles   Bundles array.
112
     *
113
     * @throws InvalidConfigurationException
114
     */
115 1
    private function validateBundles($container, $bundles)
116
    {
117 1
        foreach ($bundles as $bundle) {
118 1
            if (!class_exists($bundle)) {
119
                throw new InvalidConfigurationException(
120 1
                    "Invalid bundle namespace '{$bundle}'."
121 1
                );
122
            }
123
        }
124
        $container->setParameter('ongr_translations.bundles', $bundles);
125 1
    }
126 1
127
    /**
128
     * Edits repository name.
129
     *
130
     * @param string $repository
131
     *
132
     * @return string
133
     */
134
    private function editRepositoryName($repository)
135 1
    {
136
        return substr_replace($repository, 'history', strrpos($repository, '.') + 1);
137 1
    }
138
}
139