ConfigurationSet::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 8
cts 8
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Magium\Configuration\Console\Command;
4
5
use Magium\Configuration\Config\Repository\ConfigurationRepository;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class ConfigurationSet extends AbstractCommand
11
{
12
    use ConfigurationFactoryTrait;
13
14
    const COMMAND = 'magium:configuration:set';
15
16 3 View Code Duplication
    protected function configure()
17
    {
18
        $this
19 3
            ->setName(self::COMMAND)
20 3
            ->setDescription('Set a configuration value')
21 3
            ->setHelp("This command sets a value for a specific configuration path")
22
        ;
23
24 3
        $this->addArgument('path', InputArgument::REQUIRED, 'Configuration Path');
25 3
        $this->addArgument('value', InputArgument::OPTIONAL, 'Value');
26 3
        $this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context', ConfigurationRepository::CONTEXT_DEFAULT);
27 3
    }
28
29
30 2
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32 2
        $factory = $this->getConfigurationFactory();
33 2
        $builderFactory = $factory->getBuilderFactory();
34 2
        $path = $input->getArgument('path');
35 2
        $value = $input->getArgument('value');
36 2
        $context = $input->getArgument('context');
37
38 2
        $structure = $factory->getBuilder()->getMergedStructure();
39
40 2
        $structure->registerXPathNamespace('s', 'http://www.magiumlib.com/Configuration');
41 2
        $paths = explode('/', $path);
42 2
        $xpath = '/';
43 2
        foreach ($paths as $pathName) {
44 2
            $xpath .= sprintf('/s:*[@identifier="%s"]', $pathName);
45
        }
46
47 2
        $results = $structure->xpath($xpath);
48 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...
49 1
            throw new UnconfiguredPathException(sprintf('Path (%s) is not configured.  Do you need to create a configuration file?', $path));
50
        }
51
52 1
        $builderFactory->getBuilder()->setValue($path, $value, $context);
53
54 1
        if (!$value) {
55
            $value = '<empty>';
56
        }
57
58 1
        $out = sprintf("Set %s to %s (context: %s)", $path, $value, $context);
59
60 1
        $output->writeln($out);
61 1
        $output->writeln("Don't forget to rebuild your configuration cache with " . ConfigurationBuild::COMMAND);
62 1
    }
63
64
65
}
66