Command   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 7
c 5
b 0
f 2
lcom 1
cbo 6
dl 0
loc 79
ccs 26
cts 26
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
A initialize() 0 13 2
A getConfiguration() 0 8 2
A getCommander() 0 8 2
1
<?php
2
/**
3
 * This file is part of the Commander project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Commander\Console\Command;
9
10
use GravityMedia\Commander\Commander;
11
use GravityMedia\Commander\Config;
12
use GravityMedia\Commander\Console\Helper\ConfigLoaderHelper;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Command class.
19
 *
20
 * @package GravityMedia\Commander\Console\Command
21
 */
22
class Command extends \Symfony\Component\Console\Command\Command
23
{
24
    /**
25
     * The commander configuration.
26
     *
27
     * @var Config
28
     */
29
    private $configuration;
30
31
    /**
32
     * The commander.
33
     *
34
     * @var Commander
35
     */
36
    private $commander;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 10
    protected function configure()
42
    {
43 10
        $this->addOption(
44 10
            'configuration',
45 10
            'c',
46 10
            InputOption::VALUE_OPTIONAL,
47 10
            'The commander configuration',
48 5
            'commander.json'
49 5
        );
50 10
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 6
    protected function initialize(InputInterface $input, OutputInterface $output)
56
    {
57 6
        $filename = $input->getOption('configuration');
58
59 6
        if (!file_exists($filename)) {
60 4
            $this->configuration = new Config();
61 4
            return;
62
        }
63
64
        /** @var ConfigLoaderHelper $helper */
65 2
        $helper = $this->getHelper(ConfigLoaderHelper::class);
66 2
        $this->configuration = $helper->getLoader()->load($filename);
67 2
    }
68
69
    /**
70
     * Get commander configuration.
71
     *
72
     * @return Config
73
     *
74
     * @throws \LogicException
75
     */
76 8
    public function getConfiguration()
77
    {
78 8
        if (null === $this->configuration) {
79 2
            throw new \LogicException('The commander configuration is not available before initialization');
80
        }
81
82 6
        return $this->configuration;
83
    }
84
85
    /**
86
     * Get operational commander.
87
     *
88
     * @return Commander
89
     *
90
     * @throws \LogicException
91
     */
92 2
    public function getCommander()
93
    {
94 2
        if (null === $this->commander) {
95 2
            $this->commander = new Commander($this->getConfiguration());
96 1
        }
97
98 2
        return $this->commander;
99
    }
100
}
101