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     * @return void
15     */
16    public function cli(array $argv, \League\CLImate\CLImate $climate)
17    {
18        $config = (new \BO\Zmsbackend\Config\Service\Config())->readEntity();
19        if (count($argv) >= 3) {
20            $type = $argv[2];
21            $this->testType($config, $type);
22        }
23        if (count($argv) >= 4) {
24            $key = $argv[3];
25            $this->testKey($config, $type, $key);
26        }
27        if (count($argv) == 2) {
28            $climate->green("#CONFIGURATION:");
29            $climate->green("\tUsage: config [type] [key] [newvalue]");
30            $config->ksort();
31            array_walk($config, function ($subconfig, $type) use ($climate) {
32                $this->printType($climate, $type, $subconfig);
33            });
34        } elseif (count($argv) == 3) {
35            $subconfig = $config[$type];
36            $this->printType($climate, $type, $subconfig);
37        } elseif (count($argv) == 4) {
38            $climate->out($config[$type][$key]);
39        } elseif (count($argv) == 5) {
40            $value = $argv[4];
41            $climate->yellow("Old value:");
42            $this->printValue($climate, $key, $config[$type][$key]);
43            $config->setPreference($type, $key, $value);
44            $climate->green("New value:");
45            $this->printValue($climate, $key, $config[$type][$key]);
46            if ((new \BO\Zmsbackend\Config\Service\Config())->updateEntity($config)) {
47                $climate->green("Database entry changed");
48            }
49        } elseif (count($argv) > 5) {
50            throw new \Exception("Too much parameters");
51        }
52    }
53
54    /**
55     * @return void
56     */
57    protected function testType(\BO\Zmsentities\Config $config, $type)
58    {
59        if (!$config->hasType($type)) {
60            throw new \Exception("Could not find type of '$type'");
61        }
62    }
63
64    /**
65     * @return void
66     */
67    protected function testKey(\BO\Zmsentities\Config $config, $type, $key)
68    {
69        if (!$config->hasPreference($type, $key)) {
70            throw new \Exception("Could not find preference of '$type.$key'");
71        }
72    }
73
74    protected function printType(\League\CLImate\CLImate $climate, $type, $subconfig): void
75    {
76        $climate->blue("[$type]");
77        if (is_array($subconfig)) {
78            ksort($subconfig);
79            array_walk($subconfig, function ($value, $key) use ($climate) {
80                $this->printValue($climate, $key, $value);
81            });
82        }
83    }
84
85    protected function printValue(\League\CLImate\CLImate $climate, $key, $value): void
86    {
87        $padding = $climate->padding(25)->char(' ');
88        $padding->label("\"$key\"")->result("= \"" . (string)$value . '"');
89    }
90}