Completed
Pull Request — master (#1283)
by Oleg
07:19
created

CreateCommand::execute()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 11
nc 6
nop 2
1
<?php
2
/*
3
 * This file is part of the FOSElasticaBundle package.
4
 *
5
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FOS\ElasticaBundle\Command;
12
13
use FOS\ElasticaBundle\Index\IndexManager;
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Create command
21
 *
22
 * @author Oleg Andreyev <[email protected]>
23
 */
24
class CreateCommand extends ContainerAwareCommand
25
{
26
    /**
27
     * @var IndexManager
28
     */
29
    private $indexManager;
30
31
    /**
32
     * @var MappingBuilder
33
     */
34
    private $mappingBuilder;
35
36
    /**
37
     * @var ConfigManager
38
     */
39
    private $configManager;
40
41
    /**
42
     * Alias processor
43
     *
44
     * @var AliasProcessor
45
     */
46
    private $aliasProcessor;
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function configure()
52
    {
53
        $this
54
            ->setName('fos:elastica:create')
55
            ->addOption('index', null, InputOption::VALUE_OPTIONAL, 'Index that needs to be created')
56
            ->setDescription('Creating empty index with mapping')
57
        ;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function initialize(InputInterface $input, OutputInterface $output)
64
    {
65
        $this->indexManager = $this->getContainer()->get('fos_elastica.index_manager');
66
        $this->mappingBuilder = $this->getContainer()->get('fos_elastica.mapping_builder');
67
        $this->configManager = $this->getContainer()->get('fos_elastica.config_manager');
68
        $this->aliasProcessor = $this->getContainer()->get('fos_elastica.alias_processor');
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function execute(InputInterface $input, OutputInterface $output)
75
    {
76
        $indexName = $input->getOption('index');
77
        $indexes = null === $indexName ? array_keys($this->indexManager->getAllIndexes()) : array($indexName);
78
79
        foreach ($indexes as $indexName) {
80
            $output->writeln(sprintf('<info>Creating</info> <comment>%s</comment>', $indexName));
81
82
            $indexConfig = $this->configManager->getIndexConfiguration($indexName);
83
            $index = $this->indexManager->getIndex($indexName);
84
            if ($indexConfig->isUseAlias()) {
85
                $this->aliasProcessor->setRootName($indexConfig, $index);
86
            }
87
            $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
88
            $index->create($mapping, false);
89
        }
90
    }
91
}
92