Passed
Push — develop ( 6eac47...79c28c )
by Kevin
05:14 queued 02:30
created

ConfigurationGet::getValueFlag()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 2
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
    const COMMAND = 'magium:configuration:get';
19
20
    protected $factory;
21
22 7
    protected function configure()
23
    {
24
        $this
25 7
            ->setName(self::COMMAND)
26 7
            ->setDescription('Get a configuration value')
27 7
            ->setHelp("This command retrieves a value for a specific configuration path")
28
        ;
29 7
        $this->addOption('use-flag', 'f', InputOption::VALUE_NONE, 'Get value as flag');
30 7
        $this->addArgument('path', InputArgument::REQUIRED, 'Configuration Path');
31 7
        $this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context', Config::CONTEXT_DEFAULT);
32 7
    }
33
34 6
    public function setConfigurationFactory(MagiumConfigurationFactoryInterface $factory)
35
    {
36 6
        $this->factory = $factory;
37 6
    }
38
39 6
    protected function getConfigurationFactory()
40
    {
41 6
        if (!$this->factory instanceof MagiumConfigurationFactoryInterface) {
42
            $this->factory = new MagiumConfigurationFactory();
43
        }
44 6
        return $this->factory;
45
    }
46
47 2
    protected function getValueFlag(ConfigInterface $config, $path)
48
    {
49 2
        $value = $config->getValueFlag($path);
50 2
        if ($value) {
51 1
            $value = 'flag:true';
52
        } else {
53 1
            $value = 'flag:false';
54
        }
55 2
        return $value;
56
    }
57
58 4
    protected function getValue(ConfigInterface $config, $path)
59
    {
60 4
        $value = $config->getValue($path);
61 4
        if (is_null($value)) {
62 1
            $value = '<null>';
63 3
        } else if (!is_string($value) && !is_numeric($value)) {
64 1
            $type = gettype($value);
65 1
            $value = json_encode($value);
66 1
            $value = sprintf('%s:%s', $type, $value);
67 2
        } else if ($value == '') {
68 1
            $value = '<empty>';
69
        }
70 4
        return $value;
71
    }
72
73 6
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75 6
        $factory = $this->getConfigurationFactory();
76 6
        $manager = $factory->getManager();
77 6
        $path = $input->getArgument('path');
78 6
        $context = $input->getArgument('context');
79 6
        $config = $manager->getConfiguration($context);
80 6
        $useFlag = $input->getOption('use-flag');
81 6
        if ($useFlag) {
82 2
            $value = $this->getValueFlag($config, $path);
83
        } else {
84 4
            $value = $this->getValue($config, $path);
85
        }
86 6
        $out = sprintf("Value for %s (context: %s): %s", $path, $context, $value);
87
88 6
        $output->writeln($out);
89 6
    }
90
91
92
}
93