Completed
Pull Request — master (#1577)
by
unknown
04:16 queued 02:07
created

CreateCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 9
dl 0
loc 56
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A __construct() 0 13 1
A execute() 0 23 5
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 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
    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
            ->setDescription('Creating empty index with mapping')
55
        ;
56 7
    }
57
58 3
    protected function execute(InputInterface $input, OutputInterface $output)
59
    {
60 3
        $indexName = $input->getOption('index');
61 3
        $indexes = null === $indexName ? array_keys($this->indexManager->getAllIndexes()) : [$indexName];
62
63 3
        foreach ($indexes as $indexName) {
64 3
            $output->writeln(sprintf('<info>Creating</info> <comment>%s</comment>', $indexName));
65
66 3
            $indexConfig = $this->configManager->getIndexConfiguration($indexName);
67 3
            $index = $this->indexManager->getIndex($indexName);
68 3
            if ($indexConfig->isUseAlias()) {
69 1
                $this->aliasProcessor->setRootName($indexConfig, $index);
70
            }
71 3
            $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
72 3
            $index->create($mapping, false);
73
74 3
            if ($indexConfig->isUseAlias()) {
75
                $index->addAlias($indexName);
76
            }
77
        }
78
79
        return 0;
80
    }
81
}
82