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

DeleteIndexCommand::__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
11
/**
12
 * Command to delete all indexes.
13
 *
14
 * It will load the SearchConfigurationChain and call the deleteIndex() method on each SearchConfiguration
15
 *
16
 * @final since 5.1
17
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
18
 */
19
class DeleteIndexCommand extends ContainerAwareCommand
20
{
21
    /**
22
     * @var SearchConfigurationChain
23
     */
24
    private $configurationChain;
25
26
    public function __construct(/* SearchConfigurationChain */ $configurationChain = null)
27
    {
28
        parent::__construct();
29
30
        if (!$configurationChain instanceof SearchConfigurationChain) {
31
            @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...
32
33
            $this->setName(null === $configurationChain ? 'kuma:search:delete' : $configurationChain);
34
35
            return;
36
        }
37
38
        $this->configurationChain = $configurationChain;
39
    }
40
41
    protected function configure()
42
    {
43
        $this
44
            ->setName('kuma:search:delete')
45
            ->setDescription('Delete the index(es)');
46
    }
47
48
    /**
49
     * @param InputInterface  $input
50
     * @param OutputInterface $output
51
     *
52
     * @return null|int  null or 0 if everything went fine, or an error code
53
     */
54
    protected function execute(InputInterface $input, OutputInterface $output)
55
    {
56
        if (null === $this->configurationChain) {
57
            $this->configurationChain = $this->getContainer()->get('kunstmaan_search.search_configuration_chain');
58
        }
59
        /**
60
         * @var string                       $alias
61
         * @var SearchConfigurationInterface $searchConfiguration
62
         */
63
        foreach ($this->configurationChain->getConfigurations() as $alias => $searchConfiguration) {
64
            $searchConfiguration->deleteIndex();
65
            $output->writeln('Index deleted : ' . $alias);
66
        }
67
    }
68
}
69