Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
Config
n/a
0 / 0
n/a
0 / 0
16
n/a
0 / 0
 cli
n/a
0 / 0
n/a
0 / 0
9
 testType
n/a
0 / 0
n/a
0 / 0
2
 testKey
n/a
0 / 0
n/a
0 / 0
2
 printType
n/a
0 / 0
n/a
0 / 0
2
 printValue
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3namespace BO\Zmsbackend\Cli;
4
5/**
6 * @codeCoverageIgnore
7 *
8 */
9class Config extends \BO\Zmsbackend\Base
10{
11    /**
12     * @SuppressWarnings(Parameter)
13     *
14     */
15    public function cli(array $argv, \League\CLImate\CLImate $climate)
16    {
17        $config = (new \BO\Zmsbackend\Config\Service\Config())->readEntity();
18        if (count($argv) >= 3) {
19            $type = $argv[2];
20            $this->testType($config, $type);
21        }
22        if (count($argv) >= 4) {
23            $key = $argv[3];
24            $this->testKey($config, $type, $key);
25        }
26        if (count($argv) == 2) {
27            $climate->green("#CONFIGURATION:");
28            $climate->green("\tUsage: config [type] [key] [newvalue]");
29            $config->ksort();
30            array_walk($config, function ($subconfig, $type) use ($climate) {
31                $this->printType($climate, $type, $subconfig);
32            });
33        } elseif (count($argv) == 3) {
34            $subconfig = $config[$type];
35            $this->printType($climate, $type, $subconfig);
36        } elseif (count($argv) == 4) {
37            $climate->out($config[$type][$key]);
38        } elseif (count($argv) == 5) {
39            $value = $argv[4];
40            $climate->yellow("Old value:");
41            $this->printValue($climate, $key, $config[$type][$key]);
42            $config->setPreference($type, $key, $value);
43            $climate->green("New value:");
44            $this->printValue($climate, $key, $config[$type][$key]);
45            if ((new \BO\Zmsbackend\Config\Service\Config())->updateEntity($config)) {
46                $climate->green("Database entry changed");
47            }
48        } elseif (count($argv) > 5) {
49            throw new \Exception("Too much parameters");
50        }
51    }
52
53    protected function testType($config, $type)
54    {
55        if (!$config->hasType($type)) {
56            throw new \Exception("Could not find type of '$type'");
57        }
58    }
59
60    protected function testKey($config, $type, $key)
61    {
62        if (!$config->hasPreference($type, $key)) {
63            throw new \Exception("Could not find preference of '$type.$key'");
64        }
65    }
66
67    protected function printType(\League\CLImate\CLImate $climate, $type, $subconfig)
68    {
69        $climate->blue("[$type]");
70        if (is_array($subconfig)) {
71            ksort($subconfig);
72            array_walk($subconfig, function ($value, $key) use ($climate) {
73                $this->printValue($climate, $key, $value);
74            });
75        }
76    }
77
78    protected function printValue(\League\CLImate\CLImate $climate, $key, $value)
79    {
80        $padding = $climate->padding(25)->char(' ');
81        $padding->label("\"$key\"")->result("= \"" . (string)$value . '"');
82    }
83}