Completed
Push — master ( 67fdac...838896 )
by giu
38s
created

UsersController::logout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace App\Controller;
3
4
use App\Controller\AppController;
5
use Cake\Event\Event;
6
7
/**
8
 * Users Controller
9
 *
10
 * @property \App\Model\Table\UsersTable $Users
11
 */
12
class UsersController extends AppController
13
{
14
15 5
    public function beforeFilter(Event $event)
16
    {
17 5
        parent::beforeFilter($event);
18 5
        $this->Auth->allow('add');
19 5
    }
20
	
21
	
22
	/**
23
     * Index method
24
     *
25
     * @return \Cake\Network\Response|null
26
     */
27 1
    public function index()
28
    {
29 1
        $users = $this->paginate($this->Users);
30
31 1
        $this->set(compact('users'));
32 1
        $this->set('_serialize', ['users']);
33 1
    }
34
35
    /**
36
     * View method
37
     *
38
     * @param string|null $id User id.
39
     * @return \Cake\Network\Response|null
40
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
41
     */
42 1
    public function view($id = null)
43
    {
44 1
        $user = $this->Users->get($id, [
45
            'contain' => []
46 1
        ]);
47
48 1
        $this->set('user', $user);
49 1
        $this->set('_serialize', ['user']);
50 1
    }
51
52
    /**
53
     * Add method
54
     *
55
     * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
56
     */
57 1 View Code Duplication
    public function add()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59 1
        $user = $this->Users->newEntity();
60 1
        if ($this->request->is('post')) {
61 1
            $user = $this->Users->patchEntity($user, $this->request->data);
62 1
            if ($this->Users->save($user)) {
63 1
                $this->Flash->success(__('The user has been saved.'));
64
65 1
                return $this->redirect(['action' => 'index']);
66
            }
67
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
68
        }
69
        $this->set(compact('user'));
70
        $this->set('_serialize', ['user']);
71
    }
72
73
    /**
74
     * Edit method
75
     *
76
     * @param string|null $id User id.
77
     * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
78
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
79
     */
80 1 View Code Duplication
    public function edit($id = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82 1
        $user = $this->Users->get($id, [
83
            'contain' => []
84 1
        ]);
85 1
        if ($this->request->is(['patch', 'post', 'put'])) {
86 1
            $user = $this->Users->patchEntity($user, $this->request->data);
87 1
            if ($this->Users->save($user)) {
88 1
                $this->Flash->success(__('The user has been saved.'));
89
90 1
                return $this->redirect(['action' => 'index']);
91
            }
92
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
93
        }
94 1
        $this->set(compact('user'));
95 1
        $this->set('_serialize', ['user']);
96 1
    }
97
98
    /**
99
     * Delete method
100
     *
101
     * @param string|null $id User id.
102
     * @return \Cake\Network\Response|null Redirects to index.
103
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
104
     */
105 View Code Duplication
    public function delete($id = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $this->request->allowMethod(['post', 'delete']);
108
        $user = $this->Users->get($id);
109
        if ($this->Users->delete($user)) {
110
            $this->Flash->success(__('The user has been deleted.'));
111
        } else {
112
            $this->Flash->error(__('The user could not be deleted. Please, try again.'));
113
        }
114
115
        return $this->redirect(['action' => 'index']);
116
    }
117
	
118
	public function login() {
119
		if ($this->request->is('post')) {
120
            $user = $this->Auth->identify();
121
			$this->set('lu', $user);
122
            if ($user) {
123
                $this->Auth->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->Auth->identify() on line 120 can also be of type boolean; however, Cake\Controller\Component\AuthComponent::setUser() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
124
                return $this->redirect($this->Auth->redirectUrl());
125
            }
126
            $this->Flash->error(__('Invalid username or password, try again'));
127
        }
128
	}
129 1
	public function logout() 
130
	{
131 1
		return $this->redirect($this->Auth->logout());
132
	}
133
}
134