Completed
Pull Request — master (#917)
by Dmitry
08:52
created

ResetCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 24
ccs 18
cts 18
cp 1
rs 8.9713
cc 1
eloc 19
nc 1
nop 0
crap 1
1
<?php
2
3
namespace FOS\ElasticaBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use FOS\ElasticaBundle\IndexManager;
10
use FOS\ElasticaBundle\Resetter;
11
12
/**
13
 * Reset search indexes.
14
 */
15
class ResetCommand extends ContainerAwareCommand
16
{
17
    /**
18
     * @var IndexManager
19
     */
20
    private $indexManager;
21
22
    /**
23
     * @var Resetter
24
     */
25
    private $resetter;
26
27
    /**
28
     * @see Symfony\Component\Console\Command\Command::configure()
29
     */
30 8
    protected function configure()
31
    {
32
        $this
33 8
            ->setName('fos:elastica:reset')
34 8
            ->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index to reset')
35 8
            ->addOption(
36 8
                'index-template',
37 8
                null,
38 8
                InputOption::VALUE_OPTIONAL,
39 8
                'The index template to reset. If no index template name specified than all templates will be reset',
40 8
                true
41
            )
42 8
            ->addOption('type', null, InputOption::VALUE_OPTIONAL, 'The type to reset')
43 8
            ->addOption('force', null, InputOption::VALUE_NONE, 'Force index deletion if same name as alias or index matches index template pattern')
44 8
            ->addOption(
45 8
                'delete-template-indexes',
46 8
                null,
47 8
                InputOption::VALUE_NONE,
48
                'Delete all indexes that matches index templates patterns. ' .
49 8
                'Aware that pattern may match various indexes.'
50
            )
51 8
            ->setDescription('Reset search indexes')
52
        ;
53 8
    }
54
55
    /**
56
     * @see Symfony\Component\Console\Command\Command::initialize()
57
     */
58 8
    protected function initialize(InputInterface $input, OutputInterface $output)
59
    {
60 8
        $this->indexManager = $this->getContainer()->get('fos_elastica.index_manager');
61 8
        $this->resetter = $this->getContainer()->get('fos_elastica.resetter');
62 8
    }
63
64
    /**
65
     * @see Symfony\Component\Console\Command\Command::execute()
66
     */
67 8
    protected function execute(InputInterface $input, OutputInterface $output)
68
    {
69 8
        $index = $input->getOption('index');
70 8
        $indexTemplate = $input->hasParameterOption('--index-template') ? $input->getOption('index-template') : null;
71 8
        $type = $input->getOption('type');
72 8
        $force = (bool) $input->getOption('force');
73 8
        $deleteByPattern = (bool) $input->getOption('delete-template-indexes');
74
75 8
        if (null === $index && null !== $type) {
76
            throw new \InvalidArgumentException('Cannot specify type option without an index.');
77
        }
78
79 8
        if ($indexTemplate !== null && $index !== null) {
80 1
            throw new \InvalidArgumentException('Only index or index template name can by specify at the same time.');
81
        }
82
83 7
        if (is_string($indexTemplate)) {
84 2
            $output->writeln(sprintf('<info>Resetting template</info> <comment>%s</comment>', $indexTemplate));
85 2
            $this->resetter->resetTemplate($indexTemplate, $deleteByPattern);
86
        } else {
87 5
            $output->writeln('<info>Resetting all templates</info>');
88 5
            $this->resetter->resetAllTemplates($deleteByPattern);
89
        }
90
91 7
        if (null !== $type) {
92 1
            $output->writeln(sprintf('<info>Resetting</info> <comment>%s/%s</comment>', $index, $type));
93 1
            $this->resetter->resetIndexType($index, $type);
94 6
        } elseif (!$indexTemplate) {
95 2
            $indexes = null === $index
96 1
                ? array_keys($this->indexManager->getAllIndexes())
97 2
                : array($index)
98
            ;
99
100 2
            foreach ($indexes as $index) {
101 2
                $output->writeln(sprintf('<info>Resetting</info> <comment>%s</comment>', $index));
102 2
                $this->resetter->resetIndex($index, false, $force);
103
            }
104
        }
105 7
    }
106
}
107