Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

SearchBundle/Command/SetupIndexCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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);
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
64
         * @var SearchConfigurationInterface $searchConfiguration
65
         */
66
        foreach ($this->configurationChain->getConfigurations() as $alias => $searchConfiguration) {
67
            $languagesNotAnalyzed = $searchConfiguration->getLanguagesNotAnalyzed();
68
            if (count($languagesNotAnalyzed) > 0) {
69
                $question = new ChoiceQuestion(
70
                    sprintf('Languages analyzer is not available for: %s. Do you want continue?', implode(', ', $languagesNotAnalyzed)),
71
                    ['No', 'Yes']
72
                );
73
                $question->setErrorMessage('Answer %s is invalid.');
74
                if ($helper->ask($input, $output, $question) === 'No') {
75
                    return;
76
                }
77
            }
78
79
            $searchConfiguration->createIndex();
80
            $output->writeln('Index created : ' . $alias);
81
        }
82
    }
83
}
84