1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GBProd\ElasticsearchExtraBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Command to delete index |
13
|
|
|
* |
14
|
|
|
* @author gbprod <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class DeleteIndexCommand extends ContainerAwareCommand |
17
|
|
|
{ |
18
|
|
|
protected function configure() |
19
|
|
|
{ |
20
|
1 |
|
$this |
21
|
1 |
|
->setName('elasticsearch:index:delete') |
22
|
1 |
|
->setDescription('delete index from configuration') |
23
|
1 |
|
->addArgument( |
24
|
1 |
|
'client_id', |
25
|
1 |
|
InputArgument::REQUIRED, |
26
|
|
|
'Which client ?' |
27
|
1 |
|
) |
28
|
1 |
|
->addArgument( |
29
|
1 |
|
'index_id', |
30
|
1 |
|
InputArgument::REQUIRED, |
31
|
|
|
'Which index ?' |
32
|
1 |
|
) |
33
|
1 |
|
->addOption( |
34
|
1 |
|
'force', |
35
|
1 |
|
null, |
36
|
1 |
|
InputOption::VALUE_NONE, |
37
|
1 |
|
'Set this parameter to execute this action' |
38
|
1 |
|
) |
39
|
|
|
; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
43
|
|
|
{ |
44
|
1 |
|
if ($input->getOption('force')) { |
45
|
1 |
|
$this->deleteIndex($input, $output); |
46
|
1 |
|
} else { |
47
|
1 |
|
$this->displayWarningMassage($input, $output); |
48
|
|
|
} |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
private function deleteIndex(InputInterface $input, OutputInterface $output) |
|
|
|
|
52
|
|
|
{ |
53
|
1 |
|
$clientId = $input->getArgument('client_id'); |
54
|
1 |
|
$indexId = $input->getArgument('index_id'); |
55
|
|
|
|
56
|
1 |
|
$output->writeln(sprintf( |
57
|
1 |
|
'<info>Deleting index "%s" for client "%s"...</info>', |
58
|
1 |
|
$indexId, |
59
|
|
|
$clientId |
60
|
1 |
|
)); |
61
|
|
|
|
62
|
1 |
|
$handler = $this |
63
|
1 |
|
->getContainer() |
64
|
1 |
|
->get('gbprod.elasticsearch_extra.delete_index_handler') |
65
|
1 |
|
; |
66
|
|
|
|
67
|
1 |
|
$handler->handle($clientId, $indexId); |
68
|
|
|
|
69
|
1 |
|
$output->writeln('<info>done</info>'); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
private function displayWarningMassage(InputInterface $input, OutputInterface $output) |
73
|
|
|
{ |
74
|
1 |
|
$output->writeln('<error>ATTENTION</error>'); |
75
|
1 |
|
$output->writeln(sprintf( |
76
|
1 |
|
'<info>Would drop the index <comment>%s</comment> on client <comment>%s</comment>.</info>', |
77
|
1 |
|
$input->getArgument('client_id'), |
78
|
1 |
|
$input->getArgument('index_id') |
79
|
1 |
|
)); |
80
|
1 |
|
$output->writeln('Run the operation with --force to execute'); |
81
|
|
|
} |
82
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.