Passed
Pull Request — develop (#20)
by Kevin
02:50
created

ConfigurationBuild   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 8
dl 0
loc 55
ccs 28
cts 29
cp 0.9655
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
A setConfigurationFactory() 0 4 1
A getConfigurationFactory() 0 7 2
A execute() 0 20 4
1
<?php
2
3
namespace Magium\Configuration\Console\Command;
4
5
use Magium\Configuration\Config\InvalidContextException;
6
use Magium\Configuration\MagiumConfigurationFactory;
7
use Magium\Configuration\MagiumConfigurationFactoryInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class ConfigurationBuild extends Command
14
{
15
16
    const COMMAND = 'magium:configuration:build';
17
18
    protected $configurationFactory;
19
20 5
    protected function configure()
21
    {
22
        $this
23 5
            ->setName(self::COMMAND)
24 5
            ->setDescription('Build configuration')
25 5
            ->setHelp(
26
                'This command will build the configuration object based off of configuration files and '
27
                . 'persistent storage data.  By default, it will rebuild all contexts, but you can specify an '
28 5
                . 'individual context if you so like.');
29
30 5
        $this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context (ignore to build all contexts)');
31 5
    }
32
33 4
    public function setConfigurationFactory(MagiumConfigurationFactoryInterface $factory)
34
    {
35 4
        $this->configurationFactory = $factory;
36 4
    }
37
38 4
    protected function getConfigurationFactory()
39
    {
40 4
        if (!$this->configurationFactory instanceof MagiumConfigurationFactoryInterface) {
41
            $this->configurationFactory = new MagiumConfigurationFactory();
42
        }
43 4
        return $this->configurationFactory;
44
    }
45
46 4
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48 4
        $factory = $this->getConfigurationFactory();
49 4
        $builder = $factory->getBuilder();
50 4
        $manager = $factory->getManager();
51 4
        $contexts = $factory->getContextFile()->getContexts();
52 4
        $context = $input->getArgument('context');
53 4
        if ($context) {
54 2
            if (in_array($context, $contexts)) {
55 1
                $contexts = [$context];
56
            } else {
57 1
                throw new InvalidContextException('Context does not exist: ' . $context);
58
            }
59
        }
60 3
        foreach ($contexts as $context) {
61 3
            $output->writeln('Building context: ' . $context);
62 3
            $config = $builder->build($context);
63 3
            $manager->storeConfigurationObject($config, $context);
64
        }
65 3
    }
66
67
}
68