Completed
Push — 5.0 ( 918ecf...9b6a32 )
by Ruud
106:07 queued 94:36
created

KunstmaanNodeSearchExtension::prepend()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 124

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 124
rs 6.7555
c 0
b 0
f 0
cc 8
nc 6
nop 1

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 Kunstmaan\NodeSearchBundle\Helper\ElasticSearchUtil;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
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
    public function load(array $configs, ContainerBuilder $container)
28
    {
29
        $configuration = new Configuration($this->useElasticSearchVersion6);
30
        $config = $this->processConfiguration($configuration, $configs);
31
32
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
33
        $loader->load('services.yml');
34
35
        if (!empty($config['enable_update_listener']) && $config['enable_update_listener']) {
36
            $loader->load('update_listener.yml');
37
        }
38
39
        if (array_key_exists('use_match_query_for_title', $config)) {
40
            $container->getDefinition('kunstmaan_node_search.search.node')
41
                ->addMethodCall('setUseMatchQueryForTitle', [$config['use_match_query_for_title']]);
42
        }
43
44
        $container->getDefinition('kunstmaan_node_search.search_configuration.node')
45
            ->addMethodCall('setDefaultProperties', [$config['mapping']]);
46
47
        $container->setParameter('kunstmaan_node_search.contexts', $config['contexts']);
48
    }
49
50
    /**
51
     * Allow an extension to prepend the extension configurations.
52
     *
53
     * @param ContainerBuilder $container
54
     */
55
    public function prepend(ContainerBuilder $container)
56
    {
57
        $hosts = [];
58
        if ($container->hasParameter('kunstmaan_search.hostname') && $container->hasParameter('kunstmaan_search.port')) {
59
            $host = $container->getParameter('kunstmaan_search.hostname').':'.$container->getParameter('kunstmaan_search.port');
60
61
            if ($container->hasParameter('kunstmaan_search.username') && $container->hasParameter('kunstmaan_search.password') &&
62
                null !== $container->getParameter('kunstmaan_search.username') && null !== $container->getParameter('kunstmaan_search.password')) {
63
                $host = sprintf(
64
                    '%s:%s@%s',
65
                    $container->getParameter('kunstmaan_search.username'),
66
                    $container->getParameter('kunstmaan_search.password'),
67
                    $host
68
                );
69
            }
70
71
            $hosts[] = $host;
72
        }
73
74
        $this->useElasticSearchVersion6 = ElasticSearchUtil::useVersion6($hosts);
75
76
        if ($this->useElasticSearchVersion6) {
77
            $mapping = [
78
                'mapping' => [
79
                    'root_id' => [
80
                        'type' => 'integer',
81
                    ],
82
                    'node_id' => [
83
                        'type' => 'integer',
84
                    ],
85
                    'nodetranslation_id' => [
86
                        'type' => 'integer',
87
                    ],
88
                    'nodeversion_id' => [
89
                        'type' => 'integer',
90
                    ],
91
                    'title' => [
92
                        'type' => 'text',
93
                    ],
94
                    'slug' => [
95
                        'type' => 'text',
96
                    ],
97
                    'type' => [
98
                        'type' => 'keyword',
99
                    ],
100
                    'page_class' => [
101
                        'type' => 'keyword',
102
                    ],
103
                    'content' => [
104
                        'type' => 'text',
105
                    ],
106
                    'view_roles' => [
107
                        'type' => 'keyword',
108
                    ],
109
                ]
110
            ];
111
        } else {
112
            $mapping = [
113
                'mapping' => [
114
                    'root_id' => [
115
                        'type' => 'integer',
116
                        'include_in_all' => false,
117
                        'index' => 'not_analyzed'
118
                    ],
119
                    'node_id' => [
120
                        'type' => 'integer',
121
                        'include_in_all' => false,
122
                        'index' => 'not_analyzed'
123
                    ],
124
                    'nodetranslation_id' => [
125
                        'type' => 'integer',
126
                        'include_in_all' => false,
127
                        'index' => 'not_analyzed'
128
                    ],
129
                    'nodeversion_id' => [
130
                        'type' => 'integer',
131
                        'include_in_all' => false,
132
                        'index' => 'not_analyzed'
133
                    ],
134
                    'title' => [
135
                        'type' => 'string',
136
                        'boost' => 2,
137
                        'include_in_all' => true
138
                    ],
139
                    'slug' => [
140
                        'type' => 'string',
141
                        'include_in_all' => false,
142
                        'index' => 'not_analyzed'
143
                    ],
144
                    'type' => [
145
                        'type' => 'string',
146
                        'include_in_all' => false,
147
                        'index' => 'not_analyzed'
148
                    ],
149
                    'page_class' => [
150
                        'type' => 'string',
151
                        'include_in_all' => false,
152
                        'index' => 'not_analyzed'
153
                    ],
154
                    'content' => [
155
                        'type' => 'string',
156
                        'include_in_all' => true
157
                    ],
158
                    'created' => [
159
                        'type' => 'date',
160
                        'include_in_all' => false,
161
                        'index' => 'not_analyzed'
162
                    ],
163
                    'updated' => [
164
                        'type' => 'date',
165
                        'include_in_all' => false,
166
                        'index' => 'not_analyzed'
167
                    ],
168
                    'view_roles' => [
169
                        'type' => 'string',
170
                        'include_in_all' => true,
171
                        'index' => 'not_analyzed',
172
                    ],
173
                ]
174
            ];
175
        }
176
177
        $container->prependExtensionConfig('kunstmaan_node_search', $mapping);
178
    }
179
}
180