Completed
Push — master ( 7b9575...49c7e6 )
by Maksim
17s
created

ResetCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 6
nc 1
nop 2
crap 1
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('type', null, InputOption::VALUE_OPTIONAL, 'The type to reset')
47 3
            ->addOption('force', null, InputOption::VALUE_NONE, 'Force index deletion if same name as alias')
48 3
            ->setDescription('Reset search indexes')
49
        ;
50 3
    }
51
52 3
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54 3
        $index = $input->getOption('index');
55 3
        $type = $input->getOption('type');
56 3
        $force = (bool) $input->getOption('force');
57
58 3
        if (null === $index && null !== $type) {
59
            throw new \InvalidArgumentException('Cannot specify type option without an index.');
60
        }
61
62 3
        if (null !== $type) {
63 1
            $output->writeln(sprintf('<info>Resetting</info> <comment>%s/%s</comment>', $index, $type));
64 1
            $this->resetter->resetIndexType($index, $type);
65
        } else {
66 2
            $indexes = null === $index
67 1
                ? array_keys($this->indexManager->getAllIndexes())
68 2
                : [$index]
69
            ;
70
71 2
            foreach ($indexes as $index) {
72 2
                $output->writeln(sprintf('<info>Resetting</info> <comment>%s</comment>', $index));
73 2
                $this->resetter->resetIndex($index, false, $force);
74
            }
75
        }
76 3
    }
77
}
78