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

ConfigurationBuild   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 8
dl 0
loc 56
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 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
    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