Completed
Push — master ( 0255ae...827dea )
by Alessandro
05:44
created

AbstractCommand::initialize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 9
nc 4
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Command;
6
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
use MongoDB\Database as Connection;
13
14
/**
15
 * Class AbstractCommand.
16
 */
17
abstract class AbstractCommand extends ContainerAwareCommand
18
{
19
    /** @var SymfonyStyle */
20
    protected $io;
21
    /** @var Connection */
22
    protected $connection;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 5
    protected function configure()
28
    {
29 5
        parent::configure();
30
        $this
31 5
            ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
32
        ;
33 5
    }
34
    /**
35
     * {@inheritdoc}
36
     */
37 5
    protected function initialize(InputInterface $input, OutputInterface $output)
38
    {
39 5
        parent::initialize($input, $output);
40 5
        $this->io = new SymfonyStyle($input, $output);
41
42 5
        $connectionName = 'mongo.connection';
43
44 5
        if ($input->getOption('connection')) {
45 2
            $connectionName .= '.'.$input->getOption('connection');
46
        }
47
48 5
        if (!$this->getContainer()->has($connectionName)) {
49 1
            throw new \LogicException(sprintf('No connection named \'%s\' found', $input->getOption('connection')));
50
        }
51
52 4
        $this->connection = $this->getContainer()->get($connectionName);
53 4
    }
54
}
55