Completed
Pull Request — 6.0-dev (#875)
by Simonas
08:47
created

AbstractIndexServiceAwareCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 32
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
A getIndex() 0 17 3
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
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 ONGR\ElasticsearchBundle\Command;
13
14
use ONGR\ElasticsearchBundle\DependencyInjection\Configuration;
15
use ONGR\ElasticsearchBundle\Service\IndexService;
16
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
17
use Symfony\Component\Console\Input\InputOption;
18
19
abstract class AbstractIndexServiceAwareCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
20
{
21
    const INDEX_OPTION = 'index';
22
23
    protected function configure()
24
    {
25
        $this->addOption(
26
            self::INDEX_OPTION,
27
            'i',
28
            InputOption::VALUE_REQUIRED,
29
            'ElasticSearch index alias name or index name if you don\'t use aliases.'
30
        );
31
    }
32
33
    protected function getIndex($name): IndexService
34
    {
35
        $name = $name ?? $this->getContainer()->getParameter(Configuration::ONGR_DEFAULT_INDEX);
36
        $indexes = $this->getContainer()->getParameter(Configuration::ONGR_INDEXES);
37
38
        if (isset($indexes[$name]) && $this->getContainer()->has($indexes[$name])) {
39
            return $this->getContainer()->get($indexes[$name]);
40
        }
41
42
        throw new \RuntimeException(
43
            sprintf(
44
                'There is no index under `%s` name found. Available options: `%s`.',
45
                $name,
46
                implode('`, `', array_keys($this->getContainer()->getParameter(Configuration::ONGR_INDEXES)))
47
            )
48
        );
49
    }
50
}
51