Completed
Push — master ( 82aca0...096f3e )
by Iurii
01:41
created

Module::cmdClearModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\core\models\Module as ModuleModel;
13
use gplcart\core\Module as CoreModule;
14
use gplcart\modules\cli\controllers\Command;
15
16
/**
17
 * Handles commands related to modules
18
 */
19
class Module extends Command
20
{
21
22
    /**
23
     * Module class instance
24
     * @var \gplcart\core\Module $module
25
     */
26
    protected $module;
27
28
    /**
29
     * Module model instance
30
     * @var \gplcart\core\models\Module $module_model
31
     */
32
    protected $module_model;
33
34
    /**
35
     * @param CoreModule $module
36
     * @param ModuleModel $module_model
37
     */
38
    public function __construct(CoreModule $module, ModuleModel $module_model)
39
    {
40
        parent::__construct();
41
42
        $this->module = $module;
43
        $this->module_model = $module_model;
44
    }
45
46
    /**
47
     * Callback for "module-uninstall" command
48
     */
49
    public function cmdUninstallModule()
50
    {
51
        $result = $this->module_model->uninstall($this->getParam(0));
52
53
        if ($result !== true) {
54
            $this->errorAndExit($result);
55
        }
56
57
        $this->output();
58
    }
59
60
    /**
61
     * Callback for "module-install" command
62
     */
63
    public function cmdInstallModule()
64
    {
65
        $result = $this->module_model->install($this->getParam(0));
66
67
        if ($result !== true) {
68
            $this->errorAndExit($result);
69
        }
70
71
        $this->output();
72
    }
73
74
    /**
75
     * Callback for "module-off" command
76
     */
77
    public function cmdOffModule()
78
    {
79
        $id = $this->getParam(0);
80
81
        if (empty($id)) {
82
            $this->errorAndExit($this->text('Invalid argument'));
83
        }
84
85
        $result = $this->module_model->disable($id);
86
87
        if ($result !== true) {
88
            $this->errorAndExit($result);
89
        }
90
91
        $this->output();
92
    }
93
94
    /**
95
     * Callback for "module-on" command
96
     */
97
    public function cmdOnModule()
98
    {
99
        $id = $this->getParam(0);
100
101
        if (empty($id)) {
102
            $this->errorAndExit($this->text('Invalid argument'));
103
        }
104
105
        $result = $this->module_model->enable($id);
106
107
        if ($result !== true) {
108
            $this->errorAndExit($result);
109
        }
110
111
        $this->output();
112
    }
113
114
    /**
115
     * Callback for "module-clear" command
116
     */
117
    public function cmdClearModule()
118
    {
119
        $this->module->clearCache();
120
        $this->output();
121
    }
122
123
    /**
124
     * Callback for "module-get" command
125
     */
126
    public function cmdGetModule()
127
    {
128
        $result = $this->getListModule();
129
        $this->outputFormat($result);
130
        $this->outputFormatTableModule($result);
131
        $this->output();
132
    }
133
134
    /**
135
     * Returns an array of modules
136
     * @return array
137
     */
138
    protected function getListModule()
139
    {
140
        $id = $this->getParam(0);
141
142
        if (!isset($id)) {
143
            $list = $this->module->getList();
144
            $this->limitArray($list);
145
            return $list;
146
        }
147
148
        $module = $this->module->get($id);
149
150
        if (empty($module)) {
151
            $this->errorAndExit($this->text('Unexpected result'));
152
        }
153
154
        return array($module);
155
156
    }
157
158
    /**
159
     * Output table format
160
     * @param array $items
161
     */
162
    protected function outputFormatTableModule(array $items)
163
    {
164
        $header = array(
165
            $this->text('ID'),
166
            $this->text('Name'),
167
            $this->text('Version'),
168
            $this->text('Type'),
169
            $this->text('Enabled'),
170
            $this->text('Installed')
171
        );
172
173
        $rows = array();
174
175
        foreach ($items as $item) {
176
            $rows[] = array(
177
                $item['module_id'],
178
                $this->text($item['name']),
179
                isset($item['version']) ? $item['version'] : $this->text('Unknown'),
180
                $this->text($item['type']),
181
                empty($item['status']) ? $this->text('No') : $this->text('Yes'),
182
                empty($item['installed']) ? $this->text('No') : $this->text('Yes')
183
            );
184
        }
185
186
        $this->outputFormatTable($rows, $header);
187
    }
188
189
}
190