Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

SearchBundle/Command/PopulateIndexCommand.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\ArrayInput;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * Command to populate all indexes. Use the 'full' argument when you want to delete and add all indexes again
15
 *
16
 * It will load the SearchConfigurationChain and call the populateIndex() method on each SearchConfiguration
17
 *
18
 * @final since 5.1
19
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
20
 */
21
class PopulateIndexCommand 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...
22
{
23
    /**
24
     * @var SearchConfigurationChain
25
     */
26
    private $configurationChain;
27
28
    public function __construct(/* SearchConfigurationChain */ $configurationChain = null)
29
    {
30
        parent::__construct();
31
32
        if (!$configurationChain instanceof SearchConfigurationChain) {
33
            @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);
34
35
            $this->setName(null === $configurationChain ? 'kuma:search:populate' : $configurationChain);
36
37
            return;
38
        }
39
40
        $this->configurationChain = $configurationChain;
41
    }
42
43
    protected function configure()
44
    {
45
        $this
46
            ->setName('kuma:search:populate')
47
            ->addArgument('full', InputArgument::OPTIONAL, 'Delete and create new index(es) before populating')
48
            ->setDescription('Populate the index(es)');
49
    }
50
51
    /**
52
     * @param InputInterface  $input
53
     * @param OutputInterface $output
54
     *
55
     * @return null|int null or 0 if everything went fine, or an error code
56
     *
57
     * @throws \LogicException
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        if ($input->getArgument('full')) {
62
            $deleteCommand = $this->getApplication()->find('kuma:search:delete');
63
            $deleteCommand->run(new ArrayInput([]), $output);
64
65
            $setupCommand = $this->getApplication()->find('kuma:search:setup');
66
            $setupCommand->setHelperSet($this->getHelperSet());
67
            $setupCommand->run(new ArrayInput([]), $output);
68
        }
69
70
        if (null === $this->configurationChain) {
71
            $this->configurationChain = $this->getContainer()->get('kunstmaan_search.search_configuration_chain');
72
        }
73
74
        /**
75
         * @var string
76
         * @var SearchConfigurationInterface $searchConfiguration
77
         */
78
        foreach ($this->configurationChain->getConfigurations() as $alias => $searchConfiguration) {
79
            $searchConfiguration->populateIndex();
80
            $output->writeln('Index populated : ' . $alias);
81
        }
82
    }
83
}
84