Completed
Push — master ( 5ab5c8...17b249 )
by Karel
20s queued 10s
created

DeleteCommand::execute()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.1967

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 10
cts 13
cp 0.7692
rs 9.552
c 0
b 0
f 0
cc 4
nc 6
nop 2
crap 4.1967
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Command;
13
14
use Elastica\Exception\ExceptionInterface;
15
use Elastica\Request;
16
use FOS\ElasticaBundle\Elastica\Client;
17
use FOS\ElasticaBundle\Index\IndexManager;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * @author Jan Schädlich <[email protected]>
25
 */
26
class DeleteCommand extends Command
27
{
28
    protected static $defaultName = 'fos:elastica:delete';
29
30
    private $client;
31
    private $indexManager;
32
33 6
    public function __construct(
34
        Client $client,
35
        IndexManager $indexManager
36
    ) {
37 6
        parent::__construct();
38
39 6
        $this->client = $client;
40 6
        $this->indexManager = $indexManager;
41 6
    }
42
43 6
    protected function configure()
44
    {
45
        $this
46 6
            ->addOption('index', null, InputOption::VALUE_OPTIONAL, 'Index that needs to be deleted')
47 6
            ->setDescription('Deleting an index')
48
        ;
49 6
    }
50
51 2
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53 2
        $indexName = $input->getOption('index');
54 2
        $indexes = null === $indexName ? array_keys($this->indexManager->getAllIndexes()) : [$indexName];
55
56 2
        foreach ($indexes as $indexName) {
57 2
            $output->writeln(
58 2
                sprintf('<info>Deleting</info> <comment>%s</comment> ', $indexName)
59
            );
60 2
            $index = $this->indexManager->getIndex($indexName);
61 2
            if (!$index->exists()) {
62
                $output->writeln(
63
                    sprintf('<error>%s does not exist and can\'t be deleted</error>', $indexName)
64
                );
65
66
                continue;
67
            }
68
69 2
            $this->deleteIndex($index->getName());
70
        }
71
72 2
        return 0;
73
    }
74
75 2 View Code Duplication
    private function deleteIndex(string $indexName): void
76
    {
77
        try {
78 2
            $path = sprintf('%s', $indexName);
79 2
            $this->client->request($path, Request::DELETE);
80
        } catch (ExceptionInterface $deleteOldIndexException) {
81
            throw new \RuntimeException(sprintf(
82
                'Failed to delete index %s with message: %s',
83
                $indexName,
84
                $deleteOldIndexException->getMessage()
85
            ), 0, $deleteOldIndexException);
86
        }
87 2
    }
88
}
89