Completed
Push — 3.1.x ( b8b6d2...fd2df9 )
by Karel
14:12 queued 11:28
created

ResetCommand::execute()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0052
Metric Value
dl 0
loc 25
ccs 18
cts 19
cp 0.9474
rs 8.439
cc 6
eloc 16
nc 6
nop 2
crap 6.0052
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 3
    protected function configure()
31
    {
32 3
        $this
33 3
            ->setName('fos:elastica:reset')
34 3
            ->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index to reset')
35 3
            ->addOption('type', null, InputOption::VALUE_OPTIONAL, 'The type to reset')
36 3
            ->addOption('force', null, InputOption::VALUE_NONE, 'Force index deletion if same name as alias')
37 3
            ->setDescription('Reset search indexes')
38
        ;
39 3
    }
40
41
    /**
42
     * @see Symfony\Component\Console\Command\Command::initialize()
43
     */
44 3
    protected function initialize(InputInterface $input, OutputInterface $output)
45
    {
46 3
        $this->indexManager = $this->getContainer()->get('fos_elastica.index_manager');
47 3
        $this->resetter = $this->getContainer()->get('fos_elastica.resetter');
48 3
    }
49
50
    /**
51
     * @see Symfony\Component\Console\Command\Command::execute()
52
     */
53 3
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55 3
        $index = $input->getOption('index');
56 3
        $type = $input->getOption('type');
57 3
        $force = (bool) $input->getOption('force');
58
59 3
        if (null === $index && null !== $type) {
60
            throw new \InvalidArgumentException('Cannot specify type option without an index.');
61
        }
62
63 3
        if (null !== $type) {
64 1
            $output->writeln(sprintf('<info>Resetting</info> <comment>%s/%s</comment>', $index, $type));
65 1
            $this->resetter->resetIndexType($index, $type);
66 1
        } else {
67 2
            $indexes = null === $index
68 2
                ? array_keys($this->indexManager->getAllIndexes())
69 2
                : array($index)
70 2
            ;
71
72 2
            foreach ($indexes as $index) {
73 2
                $output->writeln(sprintf('<info>Resetting</info> <comment>%s</comment>', $index));
74 2
                $this->resetter->resetIndex($index, false, $force);
75 2
            }
76
        }
77 3
    }
78
}
79