Completed
Push — master ( 3cdfc2...4aa078 )
by Maksim
03:09
created

ResetCommand::execute()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0087

Importance

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