Completed
Pull Request — v0.10 (#630)
by Tautrimas
93:19
created

TypeDropCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 21
Ratio 100 %
Metric Value
dl 21
loc 21
rs 9.3142
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
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 ONGR\ElasticsearchBundle\Command;
13
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Elasticsearch command used for dropping types.
20
 */
21
class TypeDropCommand extends AbstractManagerAwareCommand
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        parent::configure();
29
30
        $this
31
            ->setName('ongr:es:type:drop')
32
            ->setDescription('Updates elasticsearch index mappings.')
33
            ->addOption(
34
                'type',
35
                't',
36
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
37
                'Specific types to drop.',
38
                []
39
            )
40
            ->addOption(
41
                'force',
42
                'f',
43
                InputOption::VALUE_NONE,
44
                'Set this parameter to execute this command'
45
            );
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        if (!$input->getOption('force')) {
54
            $output->writeln(
55
                '<error>ATTENTION:</error> This action should not be used in production environment.'
56
                . "\n\nOption --force has to be used to drop type(s)."
57
            );
58
59
            return 1;
60
        }
61
62
        $manager = $input->getOption('manager');
63
        $type = $input->getOption('type');
64
65
        $connection = $this->getManager($manager)->getConnection();
66
        $status = $connection->dropTypes($type);
67
68
        if ($status) {
69
            $message = sprintf(
70
                '<info>Dropped `</info><comment>%s</comment><info>` type(s) for manager named '
71
                . '`</info><comment>%s</comment><info>`.</info>',
72
                empty($type) ? 'all' : implode('</comment><info>`, `</info><comment>', $type),
73
                $manager
74
            );
75
        } else {
76
            if (empty($type)) {
77
                $typeString = '';
78
            } else {
79
                $typeString = ' `<comment>' . implode('</comment><info>`, `</info><comment>', $type) . '</comment>`';
80
            }
81
            $message = sprintf(
82
                '<info>Manager `</info><comment>%s</comment><info>` does not contain%s type(s) information.</info>',
83
                $manager,
84
                $typeString
85
            );
86
        }
87
88
        $output->writeln($message);
89
90
        return 0;
91
    }
92
}
93