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

DeleteCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 20.63 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 74.19%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 13
loc 63
ccs 23
cts 31
cp 0.7419
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A configure() 0 7 1
A execute() 0 23 4
A deleteIndex() 13 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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