|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
namespace T3G\Elasticorn\Commands\Index; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
10
|
|
|
use T3G\Elasticorn\Commands\BaseCommand; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CornifyCommand |
|
14
|
|
|
* |
|
15
|
|
|
* Converts an existing index with mapping configuration and data to an elasticorn index |
|
16
|
|
|
* |
|
17
|
|
|
* @package T3G\Elasticorn\Commands\Index |
|
18
|
|
|
*/ |
|
19
|
|
|
class CornifyCommand extends BaseCommand |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Configure the cornify command |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function configure() |
|
28
|
|
|
{ |
|
29
|
|
|
parent::configure(); |
|
30
|
|
|
$this |
|
31
|
|
|
->setName('index:cornify') |
|
32
|
|
|
->setDescription('Convert existing index to elasticorn managed index.'); |
|
33
|
|
|
|
|
34
|
|
|
$this->addArgument('indexName', InputArgument::REQUIRED, 'The index name.'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param InputInterface $input |
|
39
|
|
|
* @param OutputInterface $output |
|
40
|
|
|
* @return int|null|void |
|
41
|
|
|
*/ |
|
42
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
43
|
|
|
{ |
|
44
|
|
|
parent::execute($input, $output); |
|
45
|
|
|
$indexName = $input->getArgument('indexName'); |
|
46
|
|
|
$helper = $this->getHelper('question'); |
|
47
|
|
|
$continue = false; |
|
48
|
|
|
try { |
|
49
|
|
|
$continue = $this->compareConfiguration($input, $output, $helper, $indexName); |
|
50
|
|
|
} catch (\InvalidArgumentException $e) { |
|
51
|
|
|
if ($e->getCode() === 666) { |
|
52
|
|
|
$continue = $this->createConfiguration($input, $output, $helper, $indexName); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (true === $continue) { |
|
57
|
|
|
$suffix = uniqid('_', true); |
|
58
|
|
|
$this->indexService->renameIndex($indexName . $suffix); |
|
59
|
|
|
$this->indexService->initIndex($indexName); |
|
|
|
|
|
|
60
|
|
|
$this->indexService->copyData($indexName . $suffix, $indexName); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
66
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
67
|
|
|
* @param $helper |
|
68
|
|
|
* @param $indexName |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
private function createConfiguration(InputInterface $input, OutputInterface $output, $helper, $indexName) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$question = new ConfirmationQuestion('Configuration does not exist. Shall I create it? [Y/n]', true); |
|
74
|
|
|
if (!$helper->ask($input, $output, $question)) { |
|
75
|
|
|
$output->writeln('Cannot continue without configuration.'); |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
$this->configurationService->createConfigurationFromExistingIndex($indexName, $this->indexService->getIndex()); |
|
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
84
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
85
|
|
|
* @param $helper |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
private function compareConfiguration(InputInterface $input, OutputInterface $output, $helper, $indexName) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$this->configurationService->compareMappingConfiguration($indexName, $this->indexService->getIndex()); |
|
91
|
|
|
$question = new ConfirmationQuestion('Continue? [Y/n]', true); |
|
92
|
|
|
if (!$helper->ask($input, $output, $question)) { |
|
93
|
|
|
$output->writeln('User aborted.'); |
|
94
|
|
|
return false; |
|
95
|
|
|
} else { |
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.