Completed
Push — master ( cda96c...f408fe )
by
unknown
15s queued 12s
created

CreateCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 23.21 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 8
dl 13
loc 56
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A configure() 0 9 1
B execute() 0 22 6

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 <https://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 FOS\ElasticaBundle\Configuration\ConfigManager;
15
use FOS\ElasticaBundle\Index\AliasProcessor;
16
use FOS\ElasticaBundle\Index\IndexManager;
17
use FOS\ElasticaBundle\Index\MappingBuilder;
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 Oleg Andreyev <[email protected]>
25
 */
26
class CreateCommand extends Command
27
{
28
    protected static $defaultName = 'fos:elastica:create';
29
30
    private $indexManager;
31
    private $mappingBuilder;
32
    private $configManager;
33
    private $aliasProcessor;
34
35 7 View Code Duplication
    public function __construct(
36
        IndexManager $indexManager,
37
        MappingBuilder $mappingBuilder,
38
        ConfigManager $configManager,
39
        AliasProcessor $aliasProcessor
40
    ) {
41 7
        parent::__construct();
42
43 7
        $this->indexManager = $indexManager;
44 7
        $this->mappingBuilder = $mappingBuilder;
45 7
        $this->configManager = $configManager;
46 7
        $this->aliasProcessor = $aliasProcessor;
47 7
    }
48
49 7
    protected function configure()
50
    {
51
        $this
52 7
            ->setName('fos:elastica:create')
53 7
            ->addOption('index', null, InputOption::VALUE_OPTIONAL, 'Index that needs to be created')
54 7
            ->addOption('no-alias', null, InputOption::VALUE_NONE, 'Do not alias index')
55
            ->setDescription('Creating empty index with mapping')
56 7
        ;
57
    }
58 3
59
    protected function execute(InputInterface $input, OutputInterface $output)
60 3
    {
61
        $indexes = (null !== $index = $input->getOption('index')) ? [$index] : \array_keys($this->indexManager->getAllIndexes());
62 3
63 3
        foreach ($indexes as $indexName) {
64
            $output->writeln(\sprintf('<info>Creating</info> <comment>%s</comment>', $indexName));
65 3
66 3
            $indexConfig = $this->configManager->getIndexConfiguration($indexName);
67 3
            $index = $this->indexManager->getIndex($indexName);
68 1
            if ($indexConfig->isUseAlias()) {
69
                $this->aliasProcessor->setRootName($indexConfig, $index);
0 ignored issues
show
Compatibility introduced by
$indexConfig of type object<FOS\ElasticaBundl...n\IndexConfigInterface> is not a sub-type of object<FOS\ElasticaBundl...figuration\IndexConfig>. It seems like you assume a concrete implementation of the interface FOS\ElasticaBundle\Confi...on\IndexConfigInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
70 3
            }
71 3
            $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
72
            $index->create($mapping);
73 3
74 1
            if ($indexConfig->isUseAlias() && !$input->getOption('no-alias')) {
75
                $index->addAlias($indexName);
76
            }
77
        }
78 3
79
        return 0;
80
    }
81
}
82