Completed
Push — master ( ad8122...c7e11a )
by Cheren
10:30
created

UsersController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 * CakeCMS Community
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Community
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Community".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Community\Controller\Admin;
17
18
use Community\Token;
19
use Core\Event\EventManager;
20
use Community\Model\Entity\User;
21
use Community\Model\Table\UsersTable;
22
use Cake\ORM\Exception\RolledbackTransactionException;
23
use Cake\Datasource\Exception\RecordNotFoundException;
24
use Cake\Datasource\Exception\InvalidPrimaryKeyException;
25
26
/**
27
 * Class UsersController
28
 *
29
 * @package Community\Controller\Admin
30
 * @property UsersTable $Users
31
 */
32
class UsersController extends AppController
33
{
34
35
    /**
36
     * Add action.
37
     *
38
     * @return \Cake\Http\Response|null
39
     *
40
     * @throws \Cake\ORM\Exception\RolledbackTransactionException
41
     */
42
    public function add()
43
    {
44
        $user = $this->Users->newEntity();
45
        if ($this->request->is('post')) {
46
            $user = $this->Users->patchEntity($user, $this->request->getData());
47
48
            if ($user->get('notify') === '1') {
49
                $user->set('token', Token::generate());
50
            }
51
52
            EventManager::trigger('Admin.Controller.User.Add.BeforeSave', $this, [
53
                'user' => $user
54
            ]);
55
56
            if ($result = $this->Users->save($user)) {
57
                EventManager::trigger('Admin.Controller.User.Add.AfterSave', $this, [
58
                    'user' => $result
59
                ]);
60
61
                $this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf(
62
                    '<strong>«%s»</strong>',
63
                    $result->get('login')
64
                )));
65
66
                return $this->App->redirect(['apply' => ['action' => 'edit', $result->id]]);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
67
            } else {
68
                $this->Flash->error(__d('community', 'User could not be updated. Please, try again.'));
69
            }
70
        }
71
72
        $groups = $this->Users->Groups->getTreeList();
73
74
        $this
75
            ->set(compact('user', 'groups'))
76
            ->set('page_title', __d('community', 'Profiles: Add new user'));
77
    }
78
79
    /**
80
     * Edit action.
81
     *
82
     * @param null|int $id
83
     * @return \Cake\Http\Response|null
84
     *
85
     * @throws RecordNotFoundException
86
     * @throws InvalidPrimaryKeyException
87
     * @throws RolledbackTransactionException
88
     */
89
    public function edit($id = null)
90
    {
91
        /** @var User $user */
92
        $user = $this->Users->get($id);
93
        $groups = $this->Users->Groups->getTreeList();
94
95 View Code Duplication
        if ($this->request->is(['patch', 'post', 'put'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96
            $group = $this->Users->patchEntity($user, $this->request->getData());
97
            if ($result = $this->Users->save($group)) {
98
                $this->Flash->success(__d('community', 'The user {0} has been saved.', sprintf(
99
                    '<strong>«%s»</strong>',
100
                    $result->get('login')
101
                )));
102
                return $this->App->redirect([
103
                    'apply' => ['action' => 'edit', $result->id]
0 ignored issues
show
Bug introduced by
Accessing id on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
104
                ]);
105
            } else {
106
                $this->Flash->error(__d('community', 'User could not be updated. Please, try again.'));
107
            }
108
        }
109
110
        $this
111
            ->set(compact('user', 'groups'))
112
            ->set('page_title', __d('community', 'Profiles: Edit user - {0}', $user->name));
113
    }
114
115
    /**
116
     * Index action.
117
     *
118
     * @return void
119
     *
120
     * @throws \RuntimeException
121
     */
122
    public function index()
123
    {
124
        $query = $this->Users
125
            ->find('search', $this->Users->filterParams($this->request->getQueryParams()))
126
            ->contain('Groups');
127
128
        $this
129
            ->set('users', $this->paginate($query))
0 ignored issues
show
Bug introduced by
It seems like $query defined by $this->Users->find('sear...()))->contain('Groups') on line 124 can also be of type array; however, Cake\Controller\Controller::paginate() does only seem to accept object<Cake\ORM\Table>|s...ct<Cake\ORM\Query>|null, 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...
130
            ->set('page_title', __d('community', 'Profiles: User list'));
131
    }
132
133
    /**
134
     * Initialization hook method.
135
     *
136
     * Implement this method to avoid having to overwrite
137
     * the constructor and call parent.
138
     *
139
     * @return void
140
     *
141
     * @throws \Cake\Core\Exception\Exception
142
     */
143
    public function initialize()
144
    {
145
        parent::initialize();
146
        $this->Security->setConfig('unlockedFields', ['password', 'password_confirm']);
147
    }
148
149
    /**
150
     * Process action.
151
     *
152
     * @return \Cake\Http\Response|null
153
     */
154
    public function process()
155
    {
156
        list($action, $ids) = $this->Process->getRequestVars($this->name);
157
        return $this->Process->make($this->Users, $action, $ids);
0 ignored issues
show
Bug introduced by
It seems like $action defined by $this->Process->getRequestVars($this->name) on line 156 can also be of type array or null; however, Core\Controller\Component\ProcessComponent::make() does only seem to accept string, 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...
158
    }
159
}
160