Completed
Push — master ( 257818...6d6774 )
by Jeroen
06:17 queued 13s
created

KunstmaanNodeSearchExtension::prepend()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 106

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 106
ccs 6
cts 7
cp 0.8571
rs 8
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0116

How to fix   Long Method   

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
namespace Kunstmaan\NodeSearchBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
10
use Kunstmaan\NodeSearchBundle\Helper\ElasticSearchUtil;
11
12
/**
13
 * This is the class that loads and manages your bundle configuration
14
 *
15
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
16
 */
17
class KunstmaanNodeSearchExtension extends Extension implements PrependExtensionInterface
18
{
19
    /**
20
     * @var bool
21
     */
22
    private $useElasticSearchVersion6;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public function load(array $configs, ContainerBuilder $container)
28
    {
29 1
        $configuration = new Configuration($this->useElasticSearchVersion6);
30 1
        $config = $this->processConfiguration($configuration, $configs);
31
32 1
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
33 1
        $loader->load('services.yml');
34
35 1
        if (!empty($config['enable_update_listener']) && $config['enable_update_listener']) {
36 1
            $loader->load('update_listener.yml');
37
        }
38
39 1
        if (\array_key_exists('use_match_query_for_title', $config)) {
40 1
            $container->getDefinition('kunstmaan_node_search.search.node')
41 1
                ->addMethodCall('setUseMatchQueryForTitle', [$config['use_match_query_for_title']]);
42
        }
43
44 1
        $container->getDefinition('kunstmaan_node_search.search_configuration.node')
45 1
            ->addMethodCall('setDefaultProperties', [$config['mapping']]);
46
47 1
        $container->setParameter('kunstmaan_node_search.contexts', $config['contexts']);
48
49 1
        if (class_exists(\Elastica\Type\Mapping::class)) {
50
            $nodeSearch = $container->getDefinition('kunstmaan_node_search.search.node');
51
            $nodeSearch->addMethodCall('setIndexType', ['%kunstmaan_node_search.indextype%']);
52
        }
53 1
    }
54
55
    /**
56
     * Allow an extension to prepend the extension configurations.
57
     *
58
     * @param ContainerBuilder $container
59
     */
60 1
    public function prepend(ContainerBuilder $container)
61
    {
62 1
        $this->useElasticSearchVersion6 = $this->useElasticSearchV6($container);
63
64 1
        if ($this->useElasticSearchVersion6) {
65
            $mapping = [
66
                'mapping' => [
67
                    'root_id' => [
68
                        'type' => 'integer',
69
                    ],
70
                    'node_id' => [
71
                        'type' => 'integer',
72
                    ],
73
                    'nodetranslation_id' => [
74
                        'type' => 'integer',
75
                    ],
76
                    'nodeversion_id' => [
77
                        'type' => 'integer',
78
                    ],
79
                    'title' => [
80
                        'type' => 'text',
81
                    ],
82
                    'slug' => [
83
                        'type' => 'text',
84
                    ],
85
                    'type' => [
86
                        'type' => 'keyword',
87
                    ],
88
                    'page_class' => [
89
                        'type' => 'keyword',
90
                    ],
91
                    'content' => [
92
                        'type' => 'text',
93
                    ],
94
                    'view_roles' => [
95
                        'type' => 'keyword',
96
                    ],
97
                ],
98
            ];
99
        } else {
100
            $mapping = [
101
                'mapping' => [
102
                    'root_id' => [
103 1
                        'type' => 'integer',
104
                        'include_in_all' => false,
105
                        'index' => 'not_analyzed',
106
                    ],
107
                    'node_id' => [
108
                        'type' => 'integer',
109
                        'include_in_all' => false,
110
                        'index' => 'not_analyzed',
111
                    ],
112
                    'nodetranslation_id' => [
113
                        'type' => 'integer',
114
                        'include_in_all' => false,
115
                        'index' => 'not_analyzed',
116
                    ],
117
                    'nodeversion_id' => [
118
                        'type' => 'integer',
119
                        'include_in_all' => false,
120
                        'index' => 'not_analyzed',
121
                    ],
122
                    'title' => [
123
                        'type' => 'string',
124
                        'include_in_all' => true,
125
                    ],
126
                    'slug' => [
127
                        'type' => 'string',
128
                        'include_in_all' => false,
129
                        'index' => 'not_analyzed',
130
                    ],
131
                    'type' => [
132
                        'type' => 'string',
133
                        'include_in_all' => false,
134
                        'index' => 'not_analyzed',
135
                    ],
136
                    'page_class' => [
137
                        'type' => 'string',
138
                        'include_in_all' => false,
139
                        'index' => 'not_analyzed',
140
                    ],
141
                    'content' => [
142
                        'type' => 'string',
143
                        'include_in_all' => true,
144
                    ],
145
                    'created' => [
146
                        'type' => 'date',
147
                        'include_in_all' => false,
148
                        'index' => 'not_analyzed',
149
                    ],
150
                    'updated' => [
151
                        'type' => 'date',
152
                        'include_in_all' => false,
153
                        'index' => 'not_analyzed',
154
                    ],
155
                    'view_roles' => [
156
                        'type' => 'string',
157
                        'include_in_all' => true,
158
                        'index' => 'not_analyzed',
159
                    ],
160
                ],
161
            ];
162
        }
163
164 1
        $container->prependExtensionConfig('kunstmaan_node_search', $mapping);
165 1
    }
166
167 1
    private function useElasticSearchV6(ContainerBuilder $container)
168
    {
169 1
        if ($container->hasParameter('kunstmaan_search.elasticsearch_version')) {
170
            $version = (string) $container->getParameter('kunstmaan_search.elasticsearch_version');
171
            if (!preg_match('/^(\d+\.)?(\d+\.)?(\*|\d+)$/', $version)) {
172
                throw new \InvalidArgumentException('Invalid value for "%kunstmaan_search.elasticsearch_version%" parameter, expected value must match a format like "6", "6.1" or "6.1.2".');
173
            }
174
175
            if (version_compare($version, '5.0.0', '>=')) {
176
                return true;
177
            }
178
179
            return false;
180
        }
181
182 1
        $hosts = [];
183 1
        if ($container->hasParameter('kunstmaan_search.hostname') && $container->hasParameter('kunstmaan_search.port')) {
184
            $host = $container->getParameter('kunstmaan_search.hostname').':'.$container->getParameter('kunstmaan_search.port');
185
186
            if ($container->hasParameter('kunstmaan_search.username') && $container->hasParameter('kunstmaan_search.password') &&
187
                null !== $container->getParameter('kunstmaan_search.username') && null !== $container->getParameter('kunstmaan_search.password')) {
188
                $host = sprintf(
189
                    '%s:%s@%s',
190
                    $container->getParameter('kunstmaan_search.username'),
191
                    $container->getParameter('kunstmaan_search.password'),
192
                    $host
193
                );
194
            }
195
196
            $hosts[] = $host;
197
        }
198
199 1
        return ElasticSearchUtil::useVersion6($hosts);
200
    }
201
202
    public function getConfiguration(array $config, ContainerBuilder $container)
203
    {
204
        return new Configuration($this->useElasticSearchV6($container));
205
    }
206
}
207