Completed
Push — master ( c47ce7...f3a4f4 )
by GBProd
7s
created

DeleteIndexCommand::deleteIndex()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
ccs 19
cts 19
cp 1
rs 8.8571
cc 2
eloc 17
nc 2
nop 2
crap 2
1
<?php
2
3
namespace GBProd\ElasticaExtraBundle\Command;
4
5
use GBProd\ElasticaExtraBundle\Exception\IndexNotFoundException;
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 ElasticaAwareCommand
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 3
    protected function configure()
22
    {
23 3
        $this
24 3
            ->setName('elasticsearch:index:delete')
25 3
            ->setDescription('delete index from configuration')
26 3
            ->addArgument('index', InputArgument::REQUIRED, 'Which index ?')
27 3
            ->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action')
28 3
            ->addOption('client', null, InputOption::VALUE_REQUIRED, 'Client to use (if not default)', null)
29
        ;
30 3
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 3
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37 3
        if ($input->getOption('force')) {
38 2
            $this->deleteIndex($input, $output);
39 2
        } else {
40 1
            $this->displayWarningMassage($input, $output);
41
        }
42 3
    }
43
44 2
    private function deleteIndex(InputInterface $input, OutputInterface $output)
45
    {
46 2
        $client = $this->getClient($input->getOption('client'));
47 2
        $index  = $input->getArgument('index');
48
49 2
        $output->writeln(sprintf(
50 2
            '<info>Deleting index <comment>%s</comment> for client <comment>%s</comment>...</info>',
51 2
            $index,
52 2
            $input->getOption('client')
53 2
        ));
54
55 2
        $handler = $this
56 2
            ->getContainer()
57 2
            ->get('gbprod.elastica_extra.delete_index_handler')
58 2
        ;
59
60
        try {
61 2
            $handler->handle($client, $index);
62 2
        } catch (IndexNotFoundException $e) {
63 1
            $output->writeln(sprintf(
64 1
                '<info>Index "%s" not found</info>',
65
                $index
66 1
            ));
67
        }
68
69 2
        $output->writeln('done');
70 2
    }
71
72 1
    private function displayWarningMassage(InputInterface $input, OutputInterface $output)
73
    {
74 1
        $output->writeln('<error>ATTENTION</error>');
75 1
        $output->writeln(sprintf(
76 1
            '<info>Will delete the index <comment>%s</comment> on client <comment>%s</comment>.</info>',
77 1
            $input->getOption('client'),
78 1
            $input->getArgument('index')
79 1
        ));
80 1
        $output->writeln('Run the operation with --force to execute');
81 1
    }
82
}
83