Passed
Pull Request — develop (#19)
by Kevin
02:49
created

ConfigurationBuild::getConfigurationFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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