Completed
Pull Request — master (#1343)
by Dmitry
08:46
created

ResetTemplatesCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\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
    /**
29
     * Resetter
30
     *
31
     * @var TemplateResetter
32
     */
33
    private $resetter;
34
35
    public function __construct(
36
        TemplateResetter $resetter
37
    ) {
38
        parent::__construct();
39
40
        $this->resetter = $resetter;
41
    }
42
43
    protected function configure()
44
    {
45
        $this
46
            ->setName('fos:elastica:reset-templates')
47
            ->addOption(
48
                'index',
49
                null,
50
                InputOption::VALUE_OPTIONAL,
51
                'The index template to reset. If no index template name specified than all templates will be reset',
52
                true
53
            )
54
            ->addOption(
55
                'force-delete',
56
                null,
57
                InputOption::VALUE_NONE,
58
                'Delete all indexes that matches index templates patterns. ' .
59
                'Aware that pattern may match various indexes.'
60
            )
61
            ->setDescription('Reset search indexes templates')
62
        ;
63
    }
64
65
    protected function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        $indexTemplate = $input->hasParameterOption('--index') ? $input->getOption('index') : null;
68
        $deleteByPattern = (bool) $input->getOption('force-delete');
69
70
        if ($deleteByPattern) {
71
            $helper = $this->getHelper('question');
72
            $question = new ConfirmationQuestion('You are going to remove all template indexes. Are you sure?', false);
73
            if (!$helper->ask($input, $output, $question)) {
74
                return;
75
            }
76
        }
77
78
        if (is_string($indexTemplate)) {
79
            $output->writeln(sprintf('<info>Resetting template</info> <comment>%s</comment>', $indexTemplate));
80
            $this->resetter->resetIndex($indexTemplate, $deleteByPattern);
81
        } else {
82
            $output->writeln('<info>Resetting all templates</info>');
83
            $this->resetter->resetAllIndexes($deleteByPattern);
84
        }
85
    }
86
}
87