Completed
Push — 6.0 ( eb641c...92cb73 )
by Ruud
148:12 queued 131:31
created

SetupIndexCommand::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace Kunstmaan\SearchBundle\Command;
4
5
use Kunstmaan\SearchBundle\Configuration\SearchConfigurationChain;
6
use Kunstmaan\SearchBundle\Configuration\SearchConfigurationInterface;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\ChoiceQuestion;
11
12
/**
13
 * Command to create the indexes
14
 *
15
 * It will load the SearchConfigurationChain and call the createIndex() method on each SearchConfiguration
16
 *
17
 * @final since 5.1
18
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
19
 */
20
class SetupIndexCommand extends ContainerAwareCommand
21
{
22
    /**
23
     * @var SearchConfigurationChain
24
     */
25
    private $configurationChain;
26
27
    public function __construct(/* SearchConfigurationChain */ $configurationChain = null)
28
    {
29
        parent::__construct();
30
31
        if (!$configurationChain instanceof SearchConfigurationChain) {
32
            @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
33
34
            $this->setName(null === $configurationChain ? 'kuma:search:setup' : $configurationChain);
35
36
            return;
37
        }
38
39
        $this->configurationChain = $configurationChain;
40
    }
41
42
    protected function configure()
43
    {
44
        $this
45
            ->setName('kuma:search:setup')
46
            ->setDescription('Set up the index(es)');
47
    }
48
49
    /**
50
     * @param InputInterface  $input
51
     * @param OutputInterface $output
52
     *
53
     * @return null|int  null or 0 if everything went fine, or an error code
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $helper = $this->getHelper('question');
58
        if (null === $this->configurationChain) {
59
            $this->configurationChain = $this->getContainer()->get('kunstmaan_search.search_configuration_chain');
60
        }
61
62
        /**
63
         * @var string                       $alias
64
         * @var SearchConfigurationInterface $searchConfiguration
65
         */
66
        foreach ($this->configurationChain->getConfigurations() as $alias => $searchConfiguration) {
67
68
            $languagesNotAnalyzed = $searchConfiguration->getLanguagesNotAnalyzed();
69
            if (count($languagesNotAnalyzed) > 0) {
70
                $question = new ChoiceQuestion(
71
                    sprintf('Languages analyzer is not available for: %s. Do you want continue?', implode(', ', $languagesNotAnalyzed)),
72
                    ['No', 'Yes']
73
                );
74
                $question->setErrorMessage('Answer %s is invalid.');
75
                if ( $helper->ask($input, $output, $question) === 'No' ) {
76
                    return;
77
                }
78
            }
79
80
            $searchConfiguration->createIndex();
81
            $output->writeln('Index created : ' . $alias);
82
        }
83
    }
84
}
85