Test Failed
Pull Request — develop (#19)
by Kevin
02:51
created

ConfigurationBuild::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
crap 2
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
    protected function configure()
21
    {
22
        $this
23
            ->setName(self::COMMAND)
24
            ->setDescription('Build configuration')
25
            ->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
                . 'individual context if you so like.')
29
        ;
30
31
        $this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context (ignore to build all contexts)');
32
    }
33
34
    public function setConfigurationFactory(MagiumConfigurationFactoryInterface $factory)
35
    {
36
        $this->configurationFactory = $factory;
37
    }
38
39
    protected function getConfigurationFactory()
40
    {
41
        if (!$this->configurationFactory instanceof MagiumConfigurationFactoryInterface) {
42
            $this->configurationFactory = new MagiumConfigurationFactory();
43
        }
44
        return $this->configurationFactory;
45
    }
46
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $factory = $this->getConfigurationFactory();
50
        $builder = $factory->getBuilder();
51
        $manager = $factory->getManager();
52
        $contexts = $factory->getContextFile()->getContexts();
53
        $context = $input->getArgument('context');
54
        if ($context) {
55
                    if (in_array($context, $contexts)) {
56
                $contexts = [$context];
57
        }
58
            } else {
59
            throw new InvalidContextException('Context does not exist: ' . $context);
60
        }
61
        foreach ($contexts as $context) {
62
            $output->writeln('Building context: ' . $context);
63
            $config = $builder->build($context);
64
            $manager->storeConfigurationObject($config, $context);
65
        }
66
    }
67
68
}
69