UsersController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 32.14%

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 126
ccs 18
cts 56
cp 0.3214
rs 10
c 0
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeFilter() 0 4 1
A view() 0 7 1
A edit() 0 15 3
A logout() 0 3 1
A delete() 0 10 2
A index() 0 5 1
A add() 0 13 3
A login() 0 21 4
1
<?php
0 ignored issues
show
Coding Style introduced by
This file is missing a doc comment.
Loading history...
Coding Style introduced by
Filename "UsersController.php" doesn't match the expected filename "userscontroller.php"
Loading history...
Coding Style introduced by
The PHP open tag does not have a corresponding PHP close tag
Loading history...
Coding Style introduced by
Class found in ".php" file; use ".inc" extension instead
Loading history...
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace App\Controller\Admin;
4
5
use App\Controller\AppController;
6
use Cake\Event\EventInterface;
7
8
/**
9
 * Users Controller
10
 *
11
 * @property \App\Model\Table\UsersTable $Users
12
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
13
class UsersController extends AppController {
0 ignored issues
show
Coding Style introduced by
Opening brace of a class must be on the line after the definition
Loading history...
14
15 3
    public function beforeFilter(EventInterface $event) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Missing doc comment for function beforeFilter()
Loading history...
16 3
        parent::beforeFilter($event);
17
        //$this->Auth->allow('add','login','logout');
0 ignored issues
show
Unused Code Comprehensibility introduced by
85% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
No space found before comment text; expected "// $this->Auth->allow('add','login','logout');" but found "//$this->Auth->allow('add','login','logout');"
Loading history...
18 3
        $this->Authentication->addUnauthenticatedActions(['add','index','edit']);
0 ignored issues
show
Coding Style introduced by
Expected 1 space between comma and "'edit'"; 0 found
Loading history...
introduced by
Expected one space after the comma, 0 found
Loading history...
Coding Style introduced by
Expected 1 space between comma and "'index'"; 0 found
Loading history...
19 3
    }
20
21
    /**
22
     * Index method
23
     *
24
     * @return \Cake\Network\Response|null
0 ignored issues
show
introduced by
Function return type is not void, but function has no return statement
Loading history...
introduced by
@return doc comment specified, but function has no return statement
Loading history...
25
     */
26 1
    public function index() {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
27 1
        $users = $this->paginate($this->Users);
28
29 1
        $this->set(compact('users'));
30 1
        $this->set('_serialize', ['users']);
31 1
    }
32
33
    /**
34
     * View method
35
     *
36
     * @param string|null $id User id.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
37
     * @return \Cake\Network\Response|null
0 ignored issues
show
introduced by
@return doc comment specified, but function has no return statement
Loading history...
introduced by
Separate the @param and @return sections by a blank line.
Loading history...
introduced by
Function return type is not void, but function has no return statement
Loading history...
38
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
0 ignored issues
show
introduced by
Separate the @return and @throws sections by a blank line.
Loading history...
introduced by
@throws comment must be on the next line
Loading history...
39
     */
40
    public function view($id = null) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
41
        $user = $this->Users->get($id, [
42
            'contain' => []
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: ]
Loading history...
43
        ]);
44
45
        $this->set('user', $user);
46
        $this->set('_serialize', ['user']);
47
    }
48
49
    /**
50
     * Add method
51
     *
52
     * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
53
     */
54
    public function add() {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
55
        $user = $this->Users->newEmptyEntity();
56
        if ($this->request->is('post')) {
57
            $user = $this->Users->patchEntity($user, $this->request->getData());
58
            if ($this->Users->save($user)) {
59
                $this->Flash->success(__('The user has been saved.'));
60
61
                return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
62
            }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
63
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
64
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
65
        $this->set(compact('user'));
66
        $this->set('_serialize', ['user']);
67
    }
68
69
    /**
70
     * Edit method
71
     *
72
     * @param string|null $id User id.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
73
     * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
0 ignored issues
show
introduced by
Separate the @param and @return sections by a blank line.
Loading history...
74
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
0 ignored issues
show
introduced by
@throws comment must be on the next line
Loading history...
introduced by
Separate the @return and @throws sections by a blank line.
Loading history...
75
     */
76
    public function edit($id = null) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
77
        $user = $this->Users->get($id, [
78
            'contain' => []
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: ]
Loading history...
79
        ]);
80
        if ($this->request->is(['patch', 'post', 'put'])) {
81
            $user = $this->Users->patchEntity($user, $this->getRequest()->getData());
82
            if ($this->Users->save($user)) {
83
                $this->Flash->success(__('The user has been saved.'));
84
85
                return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
86
            }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
87
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
88
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
89
        $this->set(compact('user'));
90
        $this->set('_serialize', ['user']);
91
    }
92
93
    /**
94
     * Delete method
95
     *
96
     * @param string|null $id User id.
0 ignored issues
show
introduced by
Parameter comment must be on the next line
Loading history...
97
     * @return \Cake\Network\Response|null Redirects to index.
0 ignored issues
show
introduced by
Separate the @param and @return sections by a blank line.
Loading history...
98
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
0 ignored issues
show
introduced by
@throws comment must be on the next line
Loading history...
introduced by
Separate the @return and @throws sections by a blank line.
Loading history...
99
     */
100 1
    public function delete($id = null) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
101 1
        $this->request->allowMethod(['post', 'delete']);
102 1
        $user = $this->Users->get($id);
103 1
        if ($this->Users->delete($user)) {
104 1
            $this->Flash->success(__('The user has been deleted.'));
105
        } else {
0 ignored issues
show
introduced by
Expected newline after closing brace
Loading history...
106
            $this->Flash->error(__('The user could not be deleted. Please, try again.'));
107
        }
108
109 1
        return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
110
    }
111
112
    public function login() {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function login()
Loading history...
introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Opening brace should be on a new line
Loading history...
113
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
          if ($this->request->is('post')) {
0 ignored issues
show
Coding Style introduced by
First line of comment not aligned correctly; expected 12 spaces but found 10
Loading history...
Coding Style introduced by
Block comments must start with a capital letter
Loading history...
115
          //$user = $this->Auth->identify();
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 12 spaces but found 10
Loading history...
116
          $user = $this->Authentication->getResult();
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 12 spaces but found 10
Loading history...
117
          $this->set('lu', $user);
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 12 spaces but found 10
Loading history...
118
          if ($user) {
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 12 spaces but found 10
Loading history...
119
          //$this->Auth->setUser($user);
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 12 spaces but found 10
Loading history...
120
          return $this->redirect($this->Authentication->getLoginRedirect());
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 12 spaces but found 10
Loading history...
121
          }
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 12 spaces but found 10
Loading history...
122
          $this->Flash->error(__('Invalid username or password, try again'));
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 12 spaces but found 10
Loading history...
123
          }
0 ignored issues
show
Coding Style introduced by
Comment line indented incorrectly; expected at least 12 spaces but found 10
Loading history...
124
         */
0 ignored issues
show
Coding Style introduced by
Last line of comment aligned incorrectly; expected 8 spaces but found 9
Loading history...
Coding Style introduced by
Empty line required after block comment
Loading history...
125
        $result = $this->Authentication->getResult();
126
        // If the user is logged in send them away.
127
        if ($result->isValid()) {
128
            $target = $this->Authentication->getLoginRedirect() ?? '/home';
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
129
            return $this->redirect($target);
130
        }
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
131
        if ($this->request->is('post') && !$result->isValid()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
132
            $this->Flash->error('Invalid username or password');
133
        }
134
    }
135
136 1
    public function logout() {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function logout()
Loading history...
introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Opening brace should be on a new line
Loading history...
137 1
        $this->Authentication->logout();
138 1
        return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'login']);
139
    }
140
141
}
142