Users::editPost()   D
last analyzed

Complexity

Conditions 16
Paths 182

Size

Total Lines 130
Code Lines 88

Duplication

Lines 57
Ratio 43.85 %

Importance

Changes 0
Metric Value
cc 16
eloc 88
nc 182
nop 1
dl 57
loc 130
rs 4.5663
c 0
b 0
f 0

How to fix   Long Method    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
namespace FFCMS\Controllers\Admin;
4
5
use FFMVC\Helpers;
6
use FFCMS\{Traits, Controllers, Models, Mappers};
7
8
/**
9
 * Admin Users CMS Controller Class.
10
 *
11
 * @author Vijay Mahrra <[email protected]>
12
 * @copyright 2016 Vijay Mahrra
13
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
14
 */
15
class Users extends Admin
16
{
17
    /**
18
     * For admin listing and search results
19
     */
20
    use Traits\SearchController;
21
22
    protected $template_path = 'cms/admin/users/';
23
24
25
    /**
26
     *
27
     *
28
     * @param \Base $f3
29
     * @return void
30
     */
31 View Code Duplication
    public function listing(\Base $f3)
32
    {
33
        $view = strtolower(trim(strip_tags($f3->get('REQUEST.view'))));
34
        $view = empty($view) ? 'list.phtml' : $view . '.phtml';
35
        $f3->set('REQUEST.view', $view);
36
37
        $f3->set('results', $this->getListingResults($f3, new Mappers\Users));
38
39
        $f3->set('breadcrumbs', [
40
            _('Admin') => 'admin',
41
            _('Users') => 'admin_users_list',
42
        ]);
43
44
        $f3->set('form', $f3->get('REQUEST'));
45
        echo \View::instance()->render($this->template_path . $view);
46
    }
47
48
49
    /**
50
     *
51
     *
52
     * @param \Base $f3
53
     * @return void
54
     */
55 View Code Duplication
    public function search(\Base $f3)
56
    {
57
        $view = strtolower(trim(strip_tags($f3->get('REQUEST.view'))));
58
        $view = empty($view) ? 'list.phtml' : $view . '.phtml';
59
        $f3->set('REQUEST.view', $view);
60
61
        $f3->set('results', $this->getSearchResults($f3, new Mappers\Users));
62
63
        $f3->set('breadcrumbs', [
64
            _('Admin') => 'admin',
65
            _('Users') => 'admin_users_list',
66
            _('Search') => 'admin_users_search',
67
        ]);
68
69
        $f3->set('form', $f3->get('REQUEST'));
70
        echo \View::instance()->render($this->template_path . $view);
71
    }
72
73
74
    /**
75
     *
76
     *
77
     * @param \Base $f3
78
     * @return void
79
     */
80
    public function edit(\Base $f3)
81
    {
82
        $this->redirectLoggedOutUser();
83
        $this->csrf();
84
85
        if (false == $f3->get('isRoot')) {
86
            $this->notify(_('You do not have (root) permission!'), 'error');
87
            return $f3->reroute('@admin');
88
        }
89
90
        $usersModel = Models\Users::instance();
91
        $uuid = $f3->get('REQUEST.uuid');
92
        $usersMapper = $usersModel->getUserByUUID($uuid);
93
        if (null == $usersMapper->id) {
94
            $this->notify(_('The account no longer exists!'), 'error');
95
            $f3->reroute('@admin_users_lists');
96
        }
97
98
        $f3->set('breadcrumbs', [
99
            _('Admin') => 'admin',
100
            _('Users') => 'admin_users_list',
101
            _('Edit') => '',
102
        ]);
103
104
        $f3->set('form', $usersMapper->cast());
105
        echo \View::instance()->render($this->template_path . 'edit.phtml');
106
    }
107
108
109
    /**
110
     *
111
     *
112
     * @param \Base $f3
113
     * @return void
114
     */
115
    public function editPost(\Base $f3)
116
    {
117
        $this->csrf('@admin_users_list');
118
        $this->redirectLoggedOutUser();
119
120
        if (false == $f3->get('isRoot')) {
121
            $this->notify(_('You do not have (root) permission!'), 'error');
122
            return $f3->reroute('@admin');
123
        }
124
125
        $view = $this->template_path . 'edit.phtml';
126
127
        $f3->set('breadcrumbs', [
128
            _('Admin') => 'admin',
129
            _('Users') => 'admin_users_list',
130
            _('Edit') => '',
131
        ]);
132
133
        // get current user details
134
        $usersModel = Models\Users::instance();
135
        $uuid = $f3->get('REQUEST.uuid');
136
        $usersMapper = $usersModel->getUserByUUID($uuid);
137 View Code Duplication
        if (null == $usersMapper->id) {
138
            $this->notify(_('The account no longer exists!'), 'error');
139
            $f3->set('form', $f3->get('REQUEST'));
140
            echo \View::instance()->render('user/account.phtml');
141
            return;
142
        }
143
        $oldUserMapper = clone $usersMapper;
144
145
        // only allow updating of these fields
146
        $data = $f3->get('REQUEST');
147
        $fields = [
148
            'email',
149
            'password',
150
            'firstname',
151
            'lastname',
152
            'password_question',
153
            'password_answer',
154
            'scopes',
155
            'status',
156
        ];
157
158
        // check input data has values set for the above fields
159
        foreach ($fields as $k => $field) {
160
            if (!array_key_exists($field, $data)) {
161
                $data[$field] = null;
162
            }
163
        }
164
        // then remove any input data fields that aren't in the above fields
165
        foreach ($data as $field => $v) {
166
            if (!in_array($field, $fields)) {
167
                unset($data[$field]);
168
            }
169
        }
170
171
        // is this a password change?  if so, check they match
172
        $str = Helpers\Str::instance();
173
        $password = $f3->get('REQUEST.password');
174
        $confirm_password = $f3->get('REQUEST.confirm_password');
175 View Code Duplication
        if (!empty($password) || !empty($confirm_password)) {
176
            if ($password !== $confirm_password) {
177
                $this->notify(_('That password and confirm password must match!'), 'warning');
178
                $f3->set('form', $f3->get('REQUEST'));
179
                echo \View::instance()->render($view);
180
                return;
181
            } elseif ($str->passwordVerify($usersMapper->password, $password)) {
182
                $this->notify(_('The new password and old password are the same!'), 'warning');
183
                $f3->set('form', $f3->get('REQUEST'));
184
                echo \View::instance()->render($view);
185
                return;
186
            } else {
187
                // set new hashed password
188
                $data['password'] = $str->password($password);
189
            }
190
        } else {
191
            // same password
192
            $data['password'] = $usersMapper->password;
193
        }
194
195
        // check if email address change that email isn't taken
196
        $email = $f3->get('REQUEST.email');
197 View Code Duplication
        if ($usersMapper->email !== $email) {
198
            $usersMapper->load(['email = ?', $email]);
199
            if ($usersMapper->email == $email) {
200
                $this->notify(sprintf(_('The email address %s is already in use!'), $email), 'warning');
201
                $f3->set('form', $f3->get('REQUEST'));
202
                echo \View::instance()->render($view);
203
                return;
204
            } else {
205
                // new email
206
                $data['email'] = $email;
207
            }
208
        }
209
210
        // update required fields to check from ones which changed
211
        // validate the entered data
212
        $data['uuid'] = $uuid;
213
        $usersMapper->copyfrom($data);
214
        $usersMapper->validationRequired($fields);
215
        $errors = $usersMapper->validate(false);
216 View Code Duplication
        if (is_array($errors)) {
217
            $this->notify(['warning' => $usersMapper->validationErrors($errors)]);
218
            $f3->set('form', $f3->get('REQUEST'));
219
            echo \View::instance()->render($view);
220
            return;
221
        }
222
223
        // no change, do nothing
224 View Code Duplication
        if ($usersMapper->cast() === $oldUserMapper->cast()) {
225
            $this->notify(_('There was nothing to change!'), 'info');
226
            $f3->set('form', $f3->get('REQUEST'));
227
            echo \View::instance()->render($view);
228
            return;
229
        }
230
231
        // reset usermapper and copy in valid data
232
        $usersMapper->load(['uuid = ?', $data['uuid']]);
233
        $usersMapper->copyfrom($data);
234 View Code Duplication
        if ($usersMapper->save()) {
235
            $this->notify(_('The account was updated!'), 'success');
236
        } else {
237
            $this->notify(_('Unable to update your account!'), 'error');
238
            $f3->set('form', $f3->get('REQUEST'));
239
            echo \View::instance()->render($view);
240
            return;
241
        }
242
243
        $f3->reroute('@admin_users_search' . '?search=' . $usersMapper->uuid);
244
    }
245
246
247
    /**
248
     *
249
     *
250
     * @param \Base $f3
251
     * @return void
252
     */
253
    public function delete(\Base $f3)
254
    {
255
        $this->redirectLoggedOutUser();
256
        $this->csrf();
257
258
        if (false == $f3->get('isRoot')) {
259
            $this->notify(_('You do not have (root) permission!'), 'error');
260
            return $f3->reroute('@admin_users_list');
261
        }
262
263
        $uuid = $f3->get('REQUEST.uuid');
264
265
        $mapper = new Mappers\Users;
266
        $mapper->load(['uuid = ?', $uuid]);
267
268
        if (null == $mapper->id) {
269
            $this->notify(_('The user no longer exists!'), 'error');
270
            return $f3->reroute('@admin_users_list');
271
        }
272
273
        $mapper->erase();
274
        $this->notify('User deleted!', 'success');
275
276
        return $f3->reroute('@admin_users_list');
277
    }
278
279
}
280