Completed
Push — master ( 8b30ba...c06433 )
by Karel
04:39
created

ResetTemplatesCommand::execute()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 14
cp 0.9286
rs 9.536
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 4.0058
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\TemplateResetter;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Question\ConfirmationQuestion;
20
21
/**
22
 * Reset search indexes templates.
23
 */
24
final class ResetTemplatesCommand extends Command
25
{
26
    protected static $defaultName = 'fos:elastica:reset-templates';
27
28
    /** @var TemplateResetter */
29
    private $resetter;
30
31 4
    public function __construct(
32
        TemplateResetter $resetter
33
    ) {
34 4
        parent::__construct();
35
36 4
        $this->resetter = $resetter;
37 4
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 4
    protected function configure()
43
    {
44
        $this
45 4
            ->setName('fos:elastica:reset-templates')
46 4
            ->addOption(
47 4
                'index',
48 4
                null,
49 4
                InputOption::VALUE_REQUIRED,
50 4
                'The index template to reset. If no index template name specified than all templates will be reset'
51
            )
52 4
            ->addOption(
53 4
                'force-delete',
54 4
                null,
55 4
                InputOption::VALUE_NONE,
56
                'Delete all indexes that matches index templates patterns. ' .
57 4
                'Aware that pattern may match various indexes.'
58
            )
59 4
            ->setDescription('Reset search indexes templates')
60
        ;
61 4
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 4
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68 4
        $indexTemplate = $input->getOption('index');
69 4
        $deleteByPattern = $input->getOption('force-delete');
70
71 4
        if ($deleteByPattern) {
72 2
            $helper = $this->getHelper('question');
73 2
            $question = new ConfirmationQuestion('You are going to remove all template indexes. Are you sure?', false);
74
75 2
            if (!$helper->ask($input, $output, $question)) {
76
                return 1;
77
            }
78
        }
79
80 4
        if (null !== $indexTemplate) {
81 2
            $output->writeln(sprintf('<info>Resetting template</info> <comment>%s</comment>', $indexTemplate));
82 2
            $this->resetter->resetIndex($indexTemplate, $deleteByPattern);
83
        } else {
84 2
            $output->writeln('<info>Resetting all templates</info>');
85 2
            $this->resetter->resetAllIndexes($deleteByPattern);
86
        }
87
88 4
        return 0;
89
    }
90
}
91