Completed
Push — master ( 35e61c...496f6c )
by Iurii
01:33
created

Role::cmdDeleteRole()   D

Complexity

Conditions 10
Paths 16

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 19
nc 16
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
11
12
use gplcart\core\models\UserRole as UserRoleModel;
13
14
/**
15
 * Handles commands related to user roles
16
 */
17
class Role extends Base
18
{
19
20
    /**
21
     * Role model instance
22
     * @var \gplcart\core\models\UserRole $role
23
     */
24
    protected $role;
25
26
    /**
27
     * @param UserRoleModel $rule
28
     */
29
    public function __construct(UserRoleModel $rule)
30
    {
31
        parent::__construct();
32
33
        $this->role = $rule;
34
    }
35
36
    /**
37
     * Callback for "role-perm-add" command
38
     */
39
    public function cmdPermAddRole()
40
    {
41
        list($role_id, $existing, $submitted) = $this->getPermissionsRole();
42
43
        $data = array('permissions' => array_unique(array_merge($existing, $submitted)));
44
45
        $this->setSubmitted(null, $data);
46
        $this->setSubmitted('update', $role_id);
47
48
        $this->validateComponent('user_role');
49
50
        $this->updateRole($role_id);
51
        $this->output();
52
    }
53
54
    /**
55
     * Callback for "role-perm-delete" command
56
     */
57
    public function cmdPermDeleteRole()
58
    {
59
        list($role_id, $existing, $submitted) = $this->getPermissionsRole();
60
61
        $data = array(
62
            'permissions' => array_unique(array_diff($existing, $submitted))
63
        );
64
65
        $this->setSubmitted(null, $data);
66
        $this->setSubmitted('update', $role_id);
67
68
        $this->validateComponent('user_role');
69
70
        $this->updateRole($role_id);
71
        $this->output();
72
    }
73
74
    /**
75
     * Callback for "role-get" command
76
     */
77
    public function cmdGetRole()
78
    {
79
        $result = $this->getListRole();
80
        $this->outputFormat($result);
81
        $this->outputFormatTableRole($result);
82
        $this->output();
83
    }
84
85
    /**
86
     * Callback for "role-delete" command
87
     */
88
    public function cmdDeleteRole()
89
    {
90
        $id = $this->getParam(0);
91
        $all = $this->getParam('all');
92
93
        if (!isset($id) && empty($all)) {
94
            $this->errorAndExit($this->text('Invalid command'));
95
        }
96
97
        $result = false;
98
99
        if (isset($id)) {
100
101
            if (empty($id) || !is_numeric($id)) {
102
                $this->errorAndExit($this->text('Invalid ID'));
103
            }
104
105
            $result = $this->role->delete($id);
106
107
        } else if ($all) {
108
109
            $deleted = $count = 0;
110
            foreach ($this->role->getList() as $item) {
111
                $count++;
112
                $deleted += (int) $this->role->delete($item['role_id']);
113
            }
114
115
            $result = $count && $count == $deleted;
116
        }
117
118
        if (!$result) {
119
            $this->errorAndExit($this->text('An error occurred'));
120
        }
121
122
        $this->output();
123
    }
124
125
    /**
126
     * Callback for "role-add" command
127
     */
128
    public function cmdAddRole()
129
    {
130
        if ($this->getParam()) {
131
            $this->submitAddRole();
132
        } else {
133
            $this->wizardAddRole();
134
        }
135
136
        $this->output();
137
    }
138
139
    /**
140
     * Callback for "role-update" command
141
     */
142
    public function cmdUpdateRole()
143
    {
144
        $params = $this->getParam();
145
146
        if (empty($params[0]) || count($params) < 2) {
147
            $this->errorAndExit($this->text('Invalid command'));
148
        }
149
150
        if (!is_numeric($params[0])) {
151
            $this->errorAndExit($this->text('Invalid ID'));
152
        }
153
154
        $this->setSubmitted(null, $params);
155
        $this->setSubmittedList('permissions');
156
        $this->setSubmitted('update', $params[0]);
157
158
        $this->validateComponent('user_role');
159
160
        $this->updateRole($params[0]);
161
        $this->output();
162
    }
163
164
    /**
165
     * Returns an array of user roles
166
     * @return array
167
     */
168
    protected function getListRole()
169
    {
170
        $id = $this->getParam(0);
171
172
        if (!isset($id)) {
173
            return $this->role->getList(array('limit' => $this->getLimit()));
174
        }
175
176
        if (empty($id) || !is_numeric($id)) {
177
            $this->errorAndExit($this->text('Invalid ID'));
178
        }
179
180
        $result = $this->role->get($id);
181
182
        if (empty($result)) {
183
            $this->errorAndExit($this->text('Invalid ID'));
184
        }
185
186
        return array($result);
187
    }
188
189
    /**
190
     * Output table format
191
     * @param array $items
192
     */
193
    protected function outputFormatTableRole(array $items)
194
    {
195
        $header = array(
196
            $this->text('ID'),
197
            $this->text('Name'),
198
            $this->text('Redirect'),
199
            $this->text('Permissions'),
200
            $this->text('Enabled')
201
        );
202
203
        $rows = array();
204
205
        foreach ($items as $item) {
206
            $rows[] = array(
207
                $item['role_id'],
208
                $item['name'],
209
                $item['redirect'],
210
                count($item['permissions']),
211
                empty($item['status']) ? $this->text('No') : $this->text('Yes')
212
            );
213
        }
214
215
        $this->outputFormatTable($rows, $header);
216
    }
217
218
    /**
219
     * Returns an array containing parsed submitted data, such as:
220
     * role ID, the role permissions, submitted permissions
221
     * @return array
222
     */
223
    protected function getPermissionsRole()
224
    {
225
        $arguments = $this->getArguments();
226
227
        if (count($arguments) < 2) {
228
            $this->errorAndExit($this->text('Invalid command'));
229
        }
230
231
        $role_id = array_shift($arguments);
232
233
        if (!is_numeric($role_id)) {
234
            $this->errorAndExit($this->text('Invalid ID'));
235
        }
236
237
        $role = $this->role->get($role_id);
238
239
        if (!isset($role['permissions'])) {
240
            $this->errorAndExit($this->text('Invalid ID'));
241
        }
242
243
        return array($role_id, $role['permissions'], $arguments);
244
    }
245
246
    /**
247
     * Updates a user role
248
     * @param string $role_id
249
     */
250
    protected function updateRole($role_id)
251
    {
252
        if (!$this->isError() && !$this->role->update($role_id, $this->getSubmitted())) {
253
            $this->errorAndExit($this->text('An error occurred'));
254
        }
255
    }
256
257
    /**
258
     * Add a new user role at once
259
     */
260
    protected function submitAddRole()
261
    {
262
        $this->setSubmitted(null, $this->getParam());
263
        $this->setSubmittedList('permissions');
264
        $this->validateComponent('user_role');
265
        $this->addRole();
266
    }
267
268
    /**
269
     * Add a new user role
270
     */
271
    protected function addRole()
272
    {
273
        if (!$this->isError()) {
274
            $id = $this->role->add($this->getSubmitted());
275
            if (empty($id)) {
276
                $this->errorAndExit($this->text('An error occurred'));
277
            }
278
            $this->line($id);
279
        }
280
    }
281
282
    /**
283
     * Add a new user role step by step
284
     */
285
    protected function wizardAddRole()
286
    {
287
        $this->validatePrompt('name', $this->text('Name'), 'user_role');
288
        $this->validatePromptList('permissions', $this->text('Permissions'), 'user_role');
289
        $this->validatePrompt('redirect', $this->text('Redirect'), 'user_role', '');
290
        $this->validatePrompt('status', $this->text('Status'), 'user_role', 0);
291
        $this->setSubmittedList('permissions');
292
293
        $this->validateComponent('user_role');
294
        $this->addRole();
295
    }
296
297
}
298