Passed
Push — develop ( 2869c7...c6971c )
by Kevin
05:28 queued 02:32
created

ConfigurationSet::execute()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4.0012

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 34
ccs 22
cts 23
cp 0.9565
rs 8.5806
cc 4
eloc 22
nc 6
nop 2
crap 4.0012
1
<?php
2
3
namespace Magium\Configuration\Console\Command;
4
5
use Magium\Configuration\Config\ConfigurationRepository;
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 3 View Code Duplication
    protected function configure()
22
    {
23
        $this
24 3
            ->setName(self::COMMAND)
25 3
            ->setDescription('Set a configuration value')
26 3
            ->setHelp("This command sets a value for a specific configuration path")
27
        ;
28
29 3
        $this->addArgument('path', InputArgument::REQUIRED, 'Configuration Path');
30 3
        $this->addArgument('value', InputArgument::OPTIONAL, 'Value');
31 3
        $this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context', ConfigurationRepository::CONTEXT_DEFAULT);
32 3
    }
33
34
35 2
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37 2
        $factory = $this->getConfigurationFactory();
38 2
        $builderFactory = $factory->getBuilderFactory();
0 ignored issues
show
Bug introduced by
The method getBuilderFactory() does not exist on Magium\Configuration\Mag...urationFactoryInterface. Did you maybe mean getBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
39 2
        $path = $input->getArgument('path');
40 2
        $value = $input->getArgument('value');
41 2
        $context = $input->getArgument('context');
42
43 2
        $structure = $factory->getBuilder()->getMergedStructure();
44 2
        $xml = $structure->asXML();
0 ignored issues
show
Unused Code introduced by
$xml is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
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:*[@identifier="%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->getBuilder()->setValue($path, $value, $context);
59
60 1
        if (!$value) {
61
            $value = '<empty>';
62
        }
63
64 1
        $out = sprintf("Set %s to %s (context: %s)", $path, $value, $context);
65
66 1
        $output->writeln($out);
67 1
        $output->writeln("Don't forget to rebuild your configuration cache with " . ConfigurationBuild::COMMAND);
68 1
    }
69
70
71
}
72