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

ConfigurationGet::setConfigurationFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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