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

Module   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 1
dl 0
loc 162
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A cmdUninstallModule() 0 10 2
A cmdInstallModule() 0 10 2
A cmdOffModule() 0 16 3
A cmdOnModule() 0 16 3
A cmdGetModule() 0 7 1
A getListModule() 0 19 3
B outputFormatTableModule() 0 26 5
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-get" command
116
     */
117
    public function cmdGetModule()
118
    {
119
        $result = $this->getListModule();
120
        $this->outputFormat($result);
121
        $this->outputFormatTableModule($result);
122
        $this->output();
123
    }
124
125
    /**
126
     * Returns an array of modules
127
     * @return array
128
     */
129
    protected function getListModule()
130
    {
131
        $id = $this->getParam(0);
132
133
        if (!isset($id)) {
134
            $list = $this->module->getList();
135
            $this->limitArray($list);
136
            return $list;
137
        }
138
139
        $module = $this->module->get($id);
140
141
        if (empty($module)) {
142
            $this->errorAndExit($this->text('Unexpected result'));
143
        }
144
145
        return array($module);
146
147
    }
148
149
    /**
150
     * Output table format
151
     * @param array $items
152
     */
153
    protected function outputFormatTableModule(array $items)
154
    {
155
        $header = array(
156
            $this->text('ID'),
157
            $this->text('Name'),
158
            $this->text('Version'),
159
            $this->text('Type'),
160
            $this->text('Enabled'),
161
            $this->text('Installed')
162
        );
163
164
        $rows = array();
165
166
        foreach ($items as $item) {
167
            $rows[] = array(
168
                $item['module_id'],
169
                $this->text($item['name']),
170
                isset($item['version']) ? $item['version'] : $this->text('Unknown'),
171
                $this->text($item['type']),
172
                empty($item['status']) ? $this->text('No') : $this->text('Yes'),
173
                empty($item['installed']) ? $this->text('No') : $this->text('Yes')
174
            );
175
        }
176
177
        $this->outputFormatTable($rows, $header);
178
    }
179
180
}
181