Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

SearchBundle/Command/DeleteIndexCommand.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
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 View Code Duplication
class DeleteIndexCommand 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...
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);
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
     * @return int|null null or 0 if everything went fine, or an error code
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        if (null === $this->configurationChain) {
54
            $this->configurationChain = $this->getContainer()->get('kunstmaan_search.search_configuration_chain');
55
        }
56
        /**
57
         * @var string
58
         * @var SearchConfigurationInterface $searchConfiguration
59
         */
60
        foreach ($this->configurationChain->getConfigurations() as $alias => $searchConfiguration) {
61
            $searchConfiguration->deleteIndex();
62
            $output->writeln('Index deleted : ' . $alias);
63
        }
64
65
        return 0;
66
    }
67
}
68