IndexCreateCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 0
loc 133
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 38 1
C execute() 0 86 9
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\ElasticsearchBundle\Command;
13
14
use ONGR\ElasticsearchBundle\Service\IndexSuffixFinder;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Style\SymfonyStyle;
19
20
class IndexCreateCommand extends AbstractIndexServiceAwareCommand
21
{
22
    const NAME = 'ongr:es:index:create';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function configure()
28
    {
29
        parent::configure();
30
31
        $this
32
            ->setName(self::NAME)
33
            ->setDescription('Creates the ElasticSearch index.')
34
            ->addOption(
35
                'time',
36
                't',
37
                InputOption::VALUE_NONE,
38
                'Adds date suffix to the new index name.'
39
            )
40
            ->addOption(
41
                'alias',
42
                'a',
43
                InputOption::VALUE_NONE,
44
                'Adds alias as it is defined in the Index document annotation.'
45
            )
46
            ->addOption(
47
                'no-mapping',
48
                null,
49
                InputOption::VALUE_NONE,
50
                'Do not include mapping when the index is created.'
51
            )
52
            ->addOption(
53
                'if-not-exists',
54
                null,
55
                InputOption::VALUE_NONE,
56
                'Don\'t trigger an error, when the index already exists.'
57
            )
58
            ->addOption(
59
                'dump',
60
                null,
61
                InputOption::VALUE_NONE,
62
                'Prints a json output of the index mapping.'
63
            );
64
    }
65
66
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68
        $io = new SymfonyStyle($input, $output);
69
        $index = $this->getIndex($input->getOption(parent::INDEX_OPTION));
70
        $indexName = $aliasName = $index->getIndexName();
71
72
        if ($input->getOption('dump')) {
73
            $io->note("Index mappings:");
74
            $io->text(
75
                json_encode(
76
                    $index->getIndexSettings()->getIndexMetadata(),
0 ignored issues
show
Bug introduced by
The method getIndexMetadata cannot be called on $index->getIndexSettings() (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
77
                    JSON_PRETTY_PRINT
78
                )
79
            );
80
81
            return 0;
82
        }
83
84
        if ($input->getOption('time')) {
85
            /** @var IndexSuffixFinder $finder */
86
            $finder = $this->getContainer()->get(IndexSuffixFinder::class);
87
            $indexName = $finder->getNextFreeIndex($index);
88
        }
89
90
        if ($input->getOption('if-not-exists') && $index->indexExists()) {
91
            $io->note(
92
                sprintf(
93
                    'Index `%s` already exists.',
94
                    $index->getIndexName()
95
                )
96
            );
97
98
            return 0;
99
        }
100
101
        $indexesToRemoveAliases = null;
102
        if ($input->getOption('alias') && $index->getClient()->indices()->existsAlias(['name' => $aliasName])) {
103
            $indexesToRemoveAliases = $index->getClient()->indices()->getAlias(
104
                [
105
                    'name' => $aliasName,
106
                ]
107
            );
108
        }
109
110
        $index->createIndex($input->getOption('no-mapping'), array_filter([
111
            'index' => $indexName,
112
        ]));
113
114
        $io->text(
115
            sprintf(
116
                'Created `<comment>%s</comment>` index.',
117
                $index->getIndexName()
118
            )
119
        );
120
121
        if ($input->getOption('alias')) {
122
            $index->getClient()->indices()->putAlias([
123
                'index' => $indexName,
124
                'name' => $aliasName,
125
            ]);
126
            $io->text(
127
                sprintf(
128
                    'Created an alias `<comment>%s</comment>` for the `<comment>%s</comment>` index.',
129
                    $aliasName,
130
                    $indexName
131
                )
132
            );
133
        }
134
135
        if ($indexesToRemoveAliases) {
136
            $indexesToRemoveAliases = implode(',', array_keys($indexesToRemoveAliases));
137
            $index->getClient()->indices()->deleteAlias([
138
                'index' => $indexesToRemoveAliases,
139
                'name' => $aliasName,
140
            ]);
141
            $io->text(
142
                sprintf(
143
                    'Removed `<comment>%s</comment>` alias from `<comment>%s</comment>`.',
144
                    $aliasName,
145
                    $indexesToRemoveAliases
146
                )
147
            );
148
        }
149
150
        return 0;
151
    }
152
}
153