CornifyCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types = 1);
3
namespace T3G\Elasticorn\Commands\Index;
4
5
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
use T3G\Elasticorn\Commands\BaseCommand;
11
12
/**
13
 * Class CornifyCommand
14
 *
15
 * Converts an existing index with mapping configuration and data to an elasticorn index
16
 *
17
 * @package T3G\Elasticorn\Commands\Index
18
 */
19
class CornifyCommand extends BaseCommand
20
{
21
22
    /**
23
     * Configure the cornify command
24
     *
25
     * @return void
26
     */
27
    protected function configure()
28
    {
29
        parent::configure();
30
        $this
31
            ->setName('index:cornify')
32
            ->setDescription('Convert existing index to elasticorn managed index.');
33
34
        $this->addArgument('indexName', InputArgument::REQUIRED, 'The index name.');
35
    }
36
37
    /**
38
     * @param InputInterface $input
39
     * @param OutputInterface $output
40
     * @return int|null|void
41
     */
42
    public function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        parent::execute($input, $output);
45
        $indexName = $input->getArgument('indexName');
46
        $helper = $this->getHelper('question');
47
        $continue = false;
48
        try {
49
            $continue = $this->compareConfiguration($input, $output, $helper, $indexName);
50
        } catch (\InvalidArgumentException $e) {
51
            if ($e->getCode() === 666) {
52
                $continue = $this->createConfiguration($input, $output, $helper, $indexName);
53
            }
54
        }
55
56
        if (true === $continue) {
57
            $suffix = uniqid('_', true);
58
            $this->indexService->renameIndex($indexName . $suffix);
59
            $this->indexService->initIndex($indexName);
0 ignored issues
show
Bug introduced by
It seems like $indexName defined by $input->getArgument('indexName') on line 45 can also be of type array<integer,string> or null; however, T3G\Elasticorn\Service\IndexService::initIndex() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
60
            $this->indexService->copyData($indexName . $suffix, $indexName);
0 ignored issues
show
Bug introduced by
It seems like $indexName defined by $input->getArgument('indexName') on line 45 can also be of type array<integer,string> or null; however, T3G\Elasticorn\Service\IndexService::copyData() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
        }
62
    }
63
64
    /**
65
     * @param \Symfony\Component\Console\Input\InputInterface $input
66
     * @param \Symfony\Component\Console\Output\OutputInterface $output
67
     * @param $helper
68
     * @param $indexName
69
     * @return bool
70
     */
71 View Code Duplication
    private function createConfiguration(InputInterface $input, OutputInterface $output, $helper, $indexName)
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...
72
    {
73
        $question = new ConfirmationQuestion('Configuration does not exist. Shall I create it? [Y/n]', true);
74
        if (!$helper->ask($input, $output, $question)) {
75
            $output->writeln('Cannot continue without configuration.');
76
            return false;
77
        }
78
        $this->configurationService->createConfigurationFromExistingIndex($indexName, $this->indexService->getIndex());
0 ignored issues
show
Bug introduced by
It seems like $this->indexService->getIndex() can be null; however, createConfigurationFromExistingIndex() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
79
        return true;
80
    }
81
82
    /**
83
     * @param \Symfony\Component\Console\Input\InputInterface $input
84
     * @param \Symfony\Component\Console\Output\OutputInterface $output
85
     * @param $helper
86
     * @return bool
87
     */
88 View Code Duplication
    private function compareConfiguration(InputInterface $input, OutputInterface $output, $helper, $indexName)
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...
89
    {
90
        $this->configurationService->compareMappingConfiguration($indexName, $this->indexService->getIndex());
91
        $question = new ConfirmationQuestion('Continue? [Y/n]', true);
92
        if (!$helper->ask($input, $output, $question)) {
93
            $output->writeln('User aborted.');
94
            return false;
95
        } else {
96
            return true;
97
        }
98
    }
99
}
100