Completed
Pull Request — master (#11)
by GBProd
02:38
created

DeleteIndexCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 92.86%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 5
dl 0
loc 67
ccs 39
cts 42
cp 0.9286
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
A execute() 0 8 2
B deleteIndex() 0 27 2
A displayWarningMassage() 0 10 1
1
<?php
2
3
namespace GBProd\ElasticaExtraBundle\Command;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Command to delete index
12
 *
13
 * @author gbprod <[email protected]>
14
 */
15
class DeleteIndexCommand extends ElasticaAwareCommand
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 2
    protected function configure()
21
    {
22 2
        $this
23 2
            ->setName('elasticsearch:index:delete')
24 2
            ->setDescription('delete index from configuration')
25 2
            ->addArgument('index', InputArgument::REQUIRED, 'Which index ?')
26 2
            ->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action')
27 2
            ->addOption('client', null, InputOption::VALUE_REQUIRED, 'Client to use (if not default)', null)
28
        ;
29 2
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 2
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36 2
        if ($input->getOption('force')) {
37 1
            $this->deleteIndex($input, $output);
38 1
        } else {
39 1
            $this->displayWarningMassage($input, $output);
40
        }
41 2
    }
42
43 1
    private function deleteIndex(InputInterface $input, OutputInterface $output)
44
    {
45 1
        $client = $this->getClient($input->getOption('client'));
46 1
        $index  = $input->getArgument('index');
47
48 1
        $output->writeln(sprintf(
49 1
            '<info>Deleting index <comment>%s</comment> for client <comment>%s</comment>...</info>',
50 1
            $index,
51 1
            $input->getOption('client')
52 1
        ));
53
54 1
        $handler = $this
55 1
            ->getContainer()
56 1
            ->get('gbprod.elastica_extra.delete_index_handler')
57 1
        ;
58
59
        try {
60 1
            $handler->handle($client, $index);
61 1
        } catch (IndexNotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class GBProd\ElasticaExtraBund...\IndexNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
62
            $output->writeln(sprintf(
63
                '<info>Index "%s" not found</info>',
64
                $index
65
            ));
66
        }
67
68 1
        $output->writeln('done');
69 1
    }
70
71 1
    private function displayWarningMassage(InputInterface $input, OutputInterface $output)
72
    {
73 1
        $output->writeln('<error>ATTENTION</error>');
74 1
        $output->writeln(sprintf(
75 1
            '<info>Will delete the index <comment>%s</comment> on client <comment>%s</comment>.</info>',
76 1
            $input->getOption('client'),
77 1
            $input->getArgument('index')
78 1
        ));
79 1
        $output->writeln('Run the operation with --force to execute');
80 1
    }
81
}
82