Completed
Push — master ( b0f4f0...bce789 )
by Iurii
01:44
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package CLI
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\cli\controllers\commands;
11
12
use gplcart\modules\cli\controllers\Command;
13
14
/**
15
 * Handles commands related to system configuration
16
 */
17
class Config extends Command
18
{
19
    /**
20
     * Callback for "config-get" command
21
     */
22
    public function cmdGetConfig()
23
    {
24
        $result = $this->getListConfig();
25
        $this->outputFormat($result);
26
        $this->outputFormatTableConfig($result);
27
        $this->output();
28
    }
29
30
    /**
31
     * Returns an array of configurations
32
     * @return array
33
     */
34
    protected function getListConfig()
35
    {
36
        $name = $this->getParam(0);
37
38
        if (isset($name)) {
39
            $result = $this->config->get($name);
40
            if (!isset($result)) {
41
                $this->errorAndExit($this->text('Invalid argument'));
42
            }
43
            $list = array($name => $result);
44
        } else {
45
            $list = $this->config->get();
46
            $this->limitArray($list);
47
        }
48
49
        $saved = $this->config->select();
50
51
        $reindexed = array();
52
        foreach ($list as $key => $value) {
53
            $reindexed[] = array(
54
                'key' => $key,
55
                'value' => $value,
56
                'in_database' => isset($saved[$key])
57
            );
58
        }
59
60
        return $reindexed;
61
    }
62
63
    /**
64
     * Callback for "config-delete" command
65
     */
66
    public function cmdDeleteConfig()
67
    {
68
        $id = $this->getParam(0);
69
70
        if (!isset($id)) {
71
            $this->errorAndExit($this->text('Invalid argument'));
72
        }
73
74
        if (!$this->config->reset($id)) {
75
            $this->errorAndExit($this->text('Unexpected result'));
76
        }
77
78
        $this->output();
79
    }
80
81
    /**
82
     * Callback for "config-set" command
83
     */
84
    public function cmdSetConfig()
85
    {
86
        $key = $this->getParam(0);
87
        $value = $this->getParam(1);
88
89
        if (!isset($key) || !isset($value)) {
90
            $this->errorAndExit($this->text('Invalid command'));
91
        }
92
93
        if (!$this->config->set($key, $value)) {
94
            $this->errorAndExit($this->text('Unexpected result'));
95
        }
96
97
        $this->output();
98
    }
99
100
    /**
101
     * Output table format
102
     * @param mixed $items
103
     */
104
    protected function outputFormatTableConfig($items)
105
    {
106
        $header = array(
107
            $this->text('Name'),
108
            $this->text('Value'),
109
            $this->text('In database'),
110
        );
111
112
        $rows = array();
113
114
        foreach ($items as $item) {
115
            $rows[] = array(
116
                $item['key'],
117
                $item['value'],
118
                empty($item['in_database']) ? $this->text('No') : $this->text('Yes')
119
            );
120
        }
121
122
        $this->outputFormatTable($rows, $header);
123
    }
124
125
}
126