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