Completed
Pull Request — master (#1569)
by
unknown
24:35 queued 16s
created

ResetCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
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 6
    public function __construct(
32
        IndexManager $indexManager,
33
        Resetter $resetter
34
    ) {
35 6
        parent::__construct();
36
37 6
        $this->indexManager = $indexManager;
38 6
        $this->resetter = $resetter;
39 6
    }
40
41 6
    protected function configure()
42
    {
43
        $this
44 6
            ->setName('fos:elastica:reset')
45 6
            ->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index to reset')
46 6
            ->addOption('force', null, InputOption::VALUE_NONE, 'Force index deletion if same name as alias')
47 6
            ->setDescription('Reset search indexes')
48
        ;
49 6
    }
50
51 2
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53 2
        $indexes = (null !== $index = $input->getOption('index')) ? [$index] : array_keys($this->indexManager->getAllIndexes());
54 2
        $force = (bool) $input->getOption('force');
55
56 2
        foreach ($indexes as $index) {
57 2
            $output->writeln(sprintf('<info>Resetting</info> <comment>%s</comment>', $index));
58 2
            $this->resetter->resetIndex($index, false, $force);
59
        }
60
61 2
        return 0;
62
    }
63
}
64