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

PopulateIndexCommand::execute()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 12
nc 8
nop 2
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
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);
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...
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
     * @throws \LogicException
57
     */
58
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60
        if ($input->getArgument('full')) {
61
            $deleteCommand = $this->getApplication()->find('kuma:search:delete');
62
            $deleteCommand->run(new ArrayInput([]), $output);
63
64
            $setupCommand = $this->getApplication()->find('kuma:search:setup');
65
            $setupCommand->setHelperSet($this->getHelperSet());
66
            $setupCommand->run(new ArrayInput([]), $output);
67
        }
68
69
        if (null === $this->configurationChain) {
70
            $this->configurationChain = $this->getContainer()->get('kunstmaan_search.search_configuration_chain');
71
        }
72
73
        /**
74
         * @var string                       $alias
75
         * @var SearchConfigurationInterface $searchConfiguration
76
         */
77
        foreach ($this->configurationChain->getConfigurations() as $alias => $searchConfiguration) {
78
            $searchConfiguration->populateIndex();
79
            $output->writeln('Index populated : ' . $alias);
80
        }
81
    }
82
}
83