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