Passed
Pull Request — develop (#31)
by Kevin
02:34
created

ConfigurationSet::execute()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 28
ccs 20
cts 20
cp 1
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 19
nc 4
nop 2
crap 3
1
<?php
2
3
namespace Magium\Configuration\Console\Command;
4
5
use Magium\Configuration\Config\Config;
6
use Magium\Configuration\Config\ConfigInterface;
7
use Magium\Configuration\MagiumConfigurationFactory;
8
use Magium\Configuration\MagiumConfigurationFactoryInterface;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class ConfigurationSet extends Command
16
{
17
    use ConfigurationFactoryTrait;
18
19
    const COMMAND = 'magium:configuration:set';
20
21
    protected $factory;
22
23 3 View Code Duplication
    protected function configure()
24
    {
25
        $this
26 3
            ->setName(self::COMMAND)
27 3
            ->setDescription('Set a configuration value')
28 3
            ->setHelp("This command sets a value for a specific configuration path")
29
        ;
30
31 3
        $this->addArgument('path', InputArgument::REQUIRED, 'Configuration Path');
32 3
        $this->addArgument('value', InputArgument::REQUIRED, 'Value');
33 3
        $this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context', Config::CONTEXT_DEFAULT);
34 3
    }
35
36
37 2
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39 2
        $factory = $this->getConfigurationFactory();
40 2
        $builderFactory = $factory->getBuilderFactory();
41 2
        $path = $input->getArgument('path');
42 2
        $value = $input->getArgument('value');
43 2
        $context = $input->getArgument('context');
44
45 2
        $structure = $factory->getBuilder()->getMergedStructure();
46 2
        $structure->registerXPathNamespace('s', 'http://www.magiumlib.com/Configuration');
47 2
        $paths = explode('/', $path);
48 2
        $xpath = '/';
49 2
        foreach ($paths as $pathName) {
50 2
            $xpath .= sprintf('/s:*[@id="%s"]', $pathName);
51
        }
52
53 2
        $results = $structure->xpath($xpath);
54 2
        if (!$results) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $results of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
55 1
            throw new UnconfiguredPathException(sprintf('Path (%s) is not configured.  Do you need to create a configuration file?', $path));
56
        }
57
58 1
        $builderFactory->getPersistence()->setValue($path, $value, $context);
59
60 1
        $out = sprintf("Set %s to %s (context: %s)", $path, $value, $context);
61
62 1
        $output->writeln($out);
63 1
        $output->writeln("Don't forget to rebuild your configuration cache with " . ConfigurationBuild::COMMAND);
64 1
    }
65
66
67
}
68