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

AbstractCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 38
ccs 14
cts 14
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A initialize() 0 17 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