Completed
Branch feature/currentUserRefactoring (c13c1d)
by Schlaefer
09:08
created

UsersController::delete()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 7
nop 1
dl 0
loc 52
rs 7.8028
c 0
b 0
f 0

How to fix   Long Method   

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
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Admin\Controller;
14
15
use App\Model\Table\UsersTable;
16
17
/**
18
 * @property UsersTable $Users
19
 */
20
class UsersController extends AdminAppController
21
{
22
    /**
23
     * {@inheritDoc}
24
     */
25
    public function initialize()
26
    {
27
        parent::initialize();
28
        $this->loadModel('Users');
29
    }
30
31
    /**
32
     * List all users.
33
     *
34
     * @return void
35
     */
36
    public function index()
37
    {
38
        $data = $this->Users->find()
39
            ->select(
40
                [
41
                    'id',
42
                    'username',
43
                    'user_type',
44
                    'user_email',
45
                    'registered',
46
                    'user_lock'
47
                ]
48
            )
49
            ->order(['username' => 'asc'])
50
            ->all();
51
        $this->set('users', $data);
52
    }
53
54
    /**
55
     * add user
56
     *
57
     * @return \Cake\Network\Response|void
58
     */
59
    public function add()
60
    {
61
        if (!$this->request->is('post') && empty($this->request->getData())) {
62
            $user = $this->Users->newEntity();
63
        } else {
64
            $user = $this->Users->register($this->request->getData(), true);
65
            if (!empty($user) && !$user->hasErrors()) {
66
                $this->Flash->set(__('user.admin.add.success'), ['element' => 'success']);
67
68
                return $this->redirect(['plugin' => false, 'action' => 'view', $user->get('id')]);
69
            } else {
70
                $this->Flash->set(__('user.admin.add.error'), ['element' => 'error']);
71
            }
72
        }
73
        $this->set('user', $user);
74
    }
75
76
    /**
77
     * List all blocked users.
78
     *
79
     * @return void
80
     */
81
    public function block()
82
    {
83
        $this->set('UserBlock', $this->Users->UserBlocks->getAll());
84
    }
85
}
86