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

CreateCommand::execute()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 8.9457
c 0
b 0
f 0
cc 6
nc 10
nop 2
crap 6
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