Command::getConfiguration()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 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