Completed
Push — 1.0 ( f45751...770e00 )
by Simonas
12:41 queued 10:12
created

AbstractManagerAwareCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
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\Service\Manager;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputOption;
17
18
/**
19
 * AbstractElasticsearchCommand class.
20
 */
21
abstract class AbstractManagerAwareCommand extends ContainerAwareCommand
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function configure()
27
    {
28
        $this->addOption(
29
            'manager',
30
            'm',
31
            InputOption::VALUE_REQUIRED,
32
            'Manager name',
33
            'default'
34
        );
35
    }
36
37
    /**
38
     * Returns elasticsearch manager by name from service container.
39
     *
40
     * @param string $name Manager name defined in configuration.
41
     *
42
     * @return Manager
43
     *
44
     * @throws \RuntimeException If manager was not found.
45
     */
46
    protected function getManager($name)
47
    {
48
        $id = $this->getManagerId($name);
49
50
        if ($this->getContainer()->has($id)) {
51
            return $this->getContainer()->get($id);
52
        }
53
54
        throw new \RuntimeException(
55
            sprintf(
56
                'Manager named `%s` not found. Available: `%s`.',
57
                $name,
58
                implode('`, `', array_keys($this->getContainer()->getParameter('es.managers')))
59
            )
60
        );
61
    }
62
63
    /**
64
     * Formats manager service id from its name.
65
     *
66
     * @param string $name Manager name.
67
     *
68
     * @return string Service id.
69
     */
70
    private function getManagerId($name)
71
    {
72
        return sprintf('es.manager.%s', $name);
73
    }
74
}
75