Passed
Push — develop ( ee596d...b4560d )
by Kevin
05:19 queued 02:34
created

ConfigurationGet   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 16.42 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
lcom 2
cbo 7
dl 11
loc 67
ccs 38
cts 38
cp 1
rs 10
c 3
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 11 11 1
A getValueFlag() 0 10 2
B getValue() 0 14 5
A execute() 0 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 ConfigurationGet extends Command
16
{
17
18
    use ConfigurationFactoryTrait;
19
20
    const COMMAND = 'magium:configuration:get';
21
22
    protected $factory;
23
24 8 View Code Duplication
    protected function configure()
25
    {
26
        $this
27 8
            ->setName(self::COMMAND)
28 8
            ->setDescription('Get a configuration value')
29 8
            ->setHelp("This command retrieves a value for a specific configuration path")
30
        ;
31 8
        $this->addOption('use-flag', 'f', InputOption::VALUE_NONE, 'Get value as flag');
32 8
        $this->addArgument('path', InputArgument::REQUIRED, 'Configuration Path');
33 8
        $this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context', Config::CONTEXT_DEFAULT);
34 8
    }
35
36 2
    protected function getValueFlag(ConfigInterface $config, $path)
37
    {
38 2
        $value = $config->getValueFlag($path);
39 2
        if ($value) {
40 1
            $value = 'flag:true';
41
        } else {
42 1
            $value = 'flag:false';
43
        }
44 2
        return $value;
45
    }
46
47 4
    protected function getValue(ConfigInterface $config, $path)
48
    {
49 4
        $value = $config->getValue($path);
50 4
        if (is_null($value)) {
51 1
            $value = '<null>';
52 3
        } else if (!is_string($value) && !is_numeric($value)) {
53 1
            $type = gettype($value);
54 1
            $value = json_encode($value);
55 1
            $value = sprintf('%s:%s', $type, $value);
56 2
        } else if ($value == '') {
57 1
            $value = '<empty>';
58
        }
59 4
        return $value;
60
    }
61
62 6
    protected function execute(InputInterface $input, OutputInterface $output)
63
    {
64 6
        $factory = $this->getConfigurationFactory();
65 6
        $manager = $factory->getManager();
66 6
        $path = $input->getArgument('path');
67 6
        $context = $input->getArgument('context');
68 6
        $config = $manager->getConfiguration($context);
69 6
        $useFlag = $input->getOption('use-flag');
70 6
        if ($useFlag) {
71 2
            $value = $this->getValueFlag($config, $path);
72
        } else {
73 4
            $value = $this->getValue($config, $path);
74
        }
75 6
        $out = sprintf("Value for %s (context: %s): %s", $path, $context, $value);
76
77 6
        $output->writeln($out);
78 6
    }
79
80
81
}
82