Completed
Push — master ( 496f6c...fa7820 )
by Iurii
01:48
created

User::cmdDeleteUser()   D

Complexity

Conditions 9
Paths 24

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 4.909
c 0
b 0
f 0
cc 9
eloc 20
nc 24
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;
11
12
use gplcart\core\models\User as UserModel;
13
14
/**
15
 * Handles commands related to users
16
 */
17
class User extends Base
18
{
19
20
    /**
21
     * User model instance
22
     * @var \gplcart\core\models\User $user
23
     */
24
    protected $user;
25
26
    /**
27
     * @param UserModel $user
28
     */
29
    public function __construct(UserModel $user)
30
    {
31
        parent::__construct();
32
33
        $this->user = $user;
34
    }
35
36
    /**
37
     * Callback for "user-on" command
38
     */
39
    public function cmdOnUser()
40
    {
41
        $this->setStatusUser(true);
42
        $this->output();
43
    }
44
45
    /**
46
     * Callback for "user-off" command
47
     */
48
    public function cmdOffUser()
49
    {
50
        $this->setStatusUser(false);
51
        $this->output();
52
    }
53
54
    /**
55
     * Callback for "user-get" command
56
     */
57
    public function cmdGetUser()
58
    {
59
        $result = $this->getListUser();
60
        $this->outputFormat($result);
61
        $this->outputFormatTableUser($result);
62
        $this->output();
63
    }
64
65
    /**
66
     * Callback for "user-delete" command
67
     */
68
    public function cmdDeleteUser()
69
    {
70
        $id = $this->getParam(0);
71
72
        if (!isset($id) || !is_numeric($id)) {
73
            $this->errorAndExit($this->text('Invalid argument'));
74
        }
75
76
        $options = null;
77
78
        if ($this->getParam('role')) {
79
            $options = array('role_id' => $id);
80
        } else if ($this->getParam('store')) {
81
            $options = array('store_id' => $id);
82
        }
83
84
        if (isset($options)) {
85
86
            $deleted = $count = 0;
87
            foreach ($this->user->getList($options) as $item) {
88
                $count++;
89
                $deleted += (int) $this->user->delete($item['user_id']);
90
            }
91
92
            $result = $count && $count == $deleted;
93
94
        } else {
95
            $result = $this->user->delete($id);
96
        }
97
98
        if (empty($result)) {
99
            $this->errorAndExit($this->text('Unexpected result'));
100
        }
101
102
        $this->output();
103
    }
104
105
    /**
106
     * Callback for "user-add" command
107
     */
108
    public function cmdAddUser()
109
    {
110
        if ($this->getParam()) {
111
            $this->submitAddUser();
112
        } else {
113
            $this->wizardAddUser();
114
        }
115
116
        $this->output();
117
    }
118
119
    /**
120
     * Callback for "user-update" command
121
     */
122
    public function cmdUpdateUser()
123
    {
124
        $params = $this->getParam();
125
126
        if (empty($params[0]) || count($params) < 2) {
127
            $this->errorAndExit($this->text('Invalid command'));
128
        }
129
130
        if (!is_numeric($params[0])) {
131
            $this->errorAndExit($this->text('Invalid argument'));
132
        }
133
134
        $this->setSubmitted(null, $params);
135
        $this->setSubmitted('update', $params[0]);
136
        $this->setSubmittedJson('data');
137
138
        $this->validateComponent('user', array('admin' => true));
139
140
        $this->updateUser($params[0]);
141
        $this->output();
142
    }
143
144
    /**
145
     * Returns an array of users
146
     * @return array
147
     */
148
    protected function getListUser()
149
    {
150
        $id = $this->getParam(0);
151
152
        if (!isset($id)) {
153
            return $this->user->getList(array('limit' => $this->getLimit()));
154
        }
155
156
        if (!is_numeric($id)) {
157
            $this->errorAndExit($this->text('Invalid argument'));
158
        }
159
160
        $result = $this->user->get($id);
161
162
        if (empty($result)) {
163
            $this->errorAndExit($this->text('Unexpected result'));
164
        }
165
166
        return array($result);
167
    }
168
169
    /**
170
     * Output table format
171
     * @param array $items
172
     */
173
    protected function outputFormatTableUser(array $items)
174
    {
175
        $header = array(
176
            $this->text('ID'),
177
            $this->text('Name'),
178
            $this->text('E-mail'),
179
            $this->text('Role'),
180
            $this->text('Store'),
181
            $this->text('Enabled'),
182
            $this->text('Created'),
183
        );
184
185
        $rows = array();
186
187
        foreach ($items as $item) {
188
            $rows[] = array(
189
                $item['user_id'],
190
                $item['name'],
191
                $item['email'],
192
                $item['role_id'],
193
                $item['store_id'],
194
                empty($item['status']) ? $this->text('No') : $this->text('Yes'),
195
                $this->date($item['created'])
196
            );
197
        }
198
199
        $this->outputFormatTable($rows, $header);
200
    }
201
202
    /**
203
     * Updates a user
204
     * @param string $user_id
205
     */
206
    protected function updateUser($user_id)
207
    {
208
        if (!$this->isError() && !$this->user->update($user_id, $this->getSubmitted())) {
209
            $this->errorAndExit($this->text('Unexpected result'));
210
        }
211
    }
212
213
    /**
214
     * Add a new user at once
215
     */
216
    protected function submitAddUser()
217
    {
218
        $this->setSubmitted(null, $this->getParam());
219
        $this->setSubmittedJson('data');
220
221
        $this->validateComponent('user');
222
        $this->addUser();
223
    }
224
225
    /**
226
     * Add a new user
227
     */
228
    protected function addUser()
229
    {
230
        if (!$this->isError()) {
231
            $id = $this->user->add($this->getSubmitted());
232
            if (empty($id)) {
233
                $this->errorAndExit($this->text('Unexpected result'));
234
            }
235
            $this->line($id);
236
        }
237
    }
238
239
    /**
240
     * Add a new user step by step
241
     */
242
    protected function wizardAddUser()
243
    {
244
        // Required
245
        $this->validatePrompt('email', $this->text('E-mail'), 'user');
246
        $this->validatePrompt('password', $this->text('Password'), 'user');
247
        $this->validatePrompt('name', $this->text('Name'), 'user');
248
249
        // Optional
250
        $this->validatePrompt('role_id', $this->text('Role ID'), 'user', 0);
251
        $this->validatePrompt('store_id', $this->text('Store ID'), 'user', $this->config->get('store', 1));
252
        $this->validatePrompt('timezone', $this->text('Timezone'), 'user', $this->config->get('timezone', 'Europe/London'));
253
        $this->validatePrompt('status', $this->text('Status'), 'user', 0);
254
        $this->validatePrompt('data', $this->text('Data'), 'user');
255
        $this->setSubmittedJson('data');
256
257
        $this->validateComponent('user');
258
        $this->addUser();
259
    }
260
261
    /**
262
     * Sets a user status
263
     * @param $status
264
     */
265
    public function setStatusUser($status)
266
    {
267
        $options = $id = $result = null;
268
269
        if ($this->getParam('all')) {
270
            $options = array();
271
        } else {
272
273
            $id = $this->getParam(0);
274
275
            if (!is_numeric($id)) { // Allow 0 for roleless users
276
                $this->errorAndExit($this->text('Invalid argument'));
277
            }
278
279
            if ($this->getParam('role')) {
280
                $options = array('role_id' => $id);
281
            } else if ($this->getParam('store')) {
282
                $options = array('store_id' => $id);
283
            }
284
        }
285
286
        if (isset($options)) {
287
288
            $updated = $count = 0;
289
            foreach ($this->user->getList($options) as $item) {
290
                $count++;
291
                $updated += (int) $this->user->update($item['user_id'], array('status' => (bool) $status));
292
            }
293
294
            $result = $count && $count == $updated;
295
296
        } else if (isset($id)) {
297
            $result = $this->user->update($id, array('status' => (bool) $status));
298
        }
299
300
        if (empty($result)) {
301
            $this->errorAndExit($this->text('Unexpected result'));
302
        }
303
    }
304
305
}
306