Completed
Pull Request — master (#1343)
by Dmitry
04:26
created

ResetCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1.0002

Importance

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