Completed
Pull Request — master (#1622)
by Rimas
10:50
created

ResetTemplatesCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 5
dl 0
loc 67
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 20 1
A execute() 0 24 5
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
    public function __construct(
32
        TemplateResetter $resetter
33
    ) {
34
        parent::__construct();
35
36
        $this->resetter = $resetter;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function configure()
43
    {
44
        $this
45
            ->setName('fos:elastica:reset-templates')
46
            ->addOption(
47
                'index',
48
                null,
49
                InputOption::VALUE_REQUIRED,
50
                'The index template to reset. If no index template name specified than all templates will be reset'
51
            )
52
            ->addOption(
53
                'force-delete',
54
                null,
55
                InputOption::VALUE_NONE,
56
                'Delete all indexes that matches index templates patterns. ' .
57
                'Aware that pattern may match various indexes.'
58
            )
59
            ->setDescription('Reset search indexes templates')
60
        ;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68
        $indexTemplate = $input->getOption('index');
69
        $deleteByPattern = $input->getOption('force-delete');
70
71
        if ($input->isInteractive() && $deleteByPattern) {
72
            $helper = $this->getHelper('question');
73
            $question = new ConfirmationQuestion('You are going to remove all template indexes. Are you sure?', false);
74
75
            if (!$helper->ask($input, $output, $question)) {
76
                return 1;
77
            }
78
        }
79
80
        if (null !== $indexTemplate) {
81
            $output->writeln(sprintf('<info>Resetting template</info> <comment>%s</comment>', $indexTemplate));
82
            $this->resetter->resetIndex($indexTemplate, $deleteByPattern);
83
        } else {
84
            $output->writeln('<info>Resetting all templates</info>');
85
            $this->resetter->resetAllIndexes($deleteByPattern);
86
        }
87
88
        return 0;
89
    }
90
}
91