Completed
Push — master ( baff27...3a9c47 )
by Daniel
02:56
created

Command::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 10
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
crap 1
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 8
    protected function configure()
42
    {
43 8
        $this->addOption(
44 8
            'configuration',
45 8
            'c',
46 8
            InputOption::VALUE_OPTIONAL,
47 8
            'The commander configuration',
48 4
            'commander.json'
49 4
        );
50 8
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 4
    protected function initialize(InputInterface $input, OutputInterface $output)
56
    {
57 4
        $filename = $input->getOption('configuration');
58
59 4
        if (!file_exists($filename)) {
60 2
            $this->configuration = new Config();
61 2
            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 6
    public function getConfiguration()
77
    {
78 6
        if (null === $this->configuration) {
79 2
            throw new \LogicException('The commander configuration is not available before initialization');
80
        }
81
82 4
        return $this->configuration;
83
    }
84
85
    /**
86
     * Get operational commander.
87
     *
88
     * @return Commander
89
     *
90
     * @throws \LogicException
91
     */
92
    public function getCommander()
93
    {
94
        if (null === $this->commander) {
95
            $this->commander = new Commander($this->getConfiguration());
96
            if ($this->commander->getSchemaValidator()->schemaInSyncWithMetadata()) {
97
                return $this->commander;
98
            }
99
100
            $classes = $this->commander->getEntityManager()->getMetadataFactory()->getAllMetadata();
101
            $this->commander->getSchemaTool()->updateSchema($classes);
102
        }
103
104
        return $this->commander;
105
    }
106
}
107