ConfigurationGet::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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