GroupsController::down()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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 Cake\Datasource\Exception\InvalidPrimaryKeyException;
19
use Cake\Datasource\Exception\RecordNotFoundException;
20
use Community\Model\Entity\Group;
21
use Community\Model\Table\GroupsTable;
22
use Core\Controller\Component\MoveComponent;
23
use Core\Controller\Component\ProcessComponent;
24
use Cake\ORM\Exception\RolledbackTransactionException;
25
26
/**
27
 * Class GroupsController
28
 *
29
 * @package     Community\Controller\Admin
30
 * @property    GroupsTable $Groups
31
 * @property    MoveComponent $Move
32
 * @property    ProcessComponent $Process
33
 */
34
class GroupsController extends AppController
35
{
36
37
    /**
38
     * Edit action.
39
     *
40
     * @return  mixed
41
     *
42
     * @throws  RolledbackTransactionException
43
     * @throws  \Aura\Intl\Exception
44
     */
45
    public function add()
46
    {
47
        $group = $this->Groups->newEntity();
48
        if ($this->request->is('post')) {
49
            $group  = $this->Groups->patchEntity($group, $this->request->getData());
0 ignored issues
show
Bug introduced by
It seems like $this->request->getData() targeting Cake\Http\ServerRequest::getData() can also be of type null or string; however, Cake\ORM\Table::patchEntity() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
50
            $result = $this->Groups->save($group);
51
            if ($result) {
52
                $this->Flash->success(__d('community', 'The group has been saved.'));
53
                return $this->App->redirect([
54
                    'apply' => ['action' => 'edit', $result->id],
55
                ]);
56
            } else {
57
                $this->Flash->error(__d('community', 'The group could not be saved. Please, try again.'));
58
            }
59
        }
60
61
        $parents = $this->Groups->getTreeList();
62
        $this->set(compact('group', 'parents'));
63
        $this->set('page_title', __d('community', 'Add new group'));
64
    }
65
66
    /**
67
     * Move down group action.
68
     *
69
     * @param   int $id Group id.
70
     * @return  \Cake\Http\Response|null
71
     */
72
    public function down($id)
73
    {
74
        return $this->Move->down($this->Groups, $id);
75
    }
76
77
    /**
78
     * Edit action.
79
     *
80
     * @param   int $id Group id.
81
     * @return  \Cake\Http\Response|null
82
     *
83
     * @throws  RecordNotFoundException
84
     * @throws  InvalidPrimaryKeyException
85
     * @throws  RolledbackTransactionException
86
     * @throws  \Aura\Intl\Exception
87
     */
88
    public function edit($id)
89
    {
90
        /** @var Group $group */
91
        $group = $this->Groups->get($id, ['contain' => []]);
92 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...
93
            $group = $this->Groups->patchEntity($group, $this->request->getData());
0 ignored issues
show
Bug introduced by
It seems like $this->request->getData() targeting Cake\Http\ServerRequest::getData() can also be of type null or string; however, Cake\ORM\Table::patchEntity() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
94
            if ($result = $this->Groups->save($group)) {
95
                $this->Flash->success(__d('community', 'The group has been updated.'));
96
                return $this->App->redirect([
97
                    'apply' => ['action' => 'edit', $result->id]
98
                ]);
99
            } else {
100
                $this->Flash->error(__d('community', 'The group could not be updated. Please, try again.'));
101
            }
102
        }
103
104
        $parents = $this->Groups->getTreeList();
105
        $this->set(compact('group', 'parents'));
106
        $this->set('page_title', __d('community', 'Edit group: {0}', $group->name));
0 ignored issues
show
Bug introduced by
Accessing name 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...
107
    }
108
109
    /**
110
     * Index action.
111
     *
112
     * @return  void
113
     *
114
     * @throws  \Aura\Intl\Exception
115
     */
116
    public function index()
117
    {
118
        $this->set('groups', $this->Groups->getTreeList());
119
        $this->set('page_title', __d('community', 'User group list'));
120
    }
121
122
    /**
123
     * Initialization hook method.
124
     *
125
     * @return  void
126
     *
127
     * @throws  \Aura\Intl\Exception
128
     * @throws  \JBZoo\Utils\Exception
129
     * @throws  \Cake\Core\Exception\Exception When trying to set a key that is invalid.
130
     */
131
    public function initialize()
132
    {
133
        parent::initialize();
134
135
        $this->Move->setConfig('messages', [
136
            'success' => __d('community', 'Group has been moved'),
137
            'error'   => __d('community', 'Group could not been moved')
138
        ]);
139
    }
140
141
    /**
142
     * Process action.
143
     *
144
     * @return  \Cake\Http\Response|null
145
     *
146
     * @throws  \Aura\Intl\Exception
147
     */
148
    public function process()
149
    {
150
        list($action, $ids) = $this->Process->getRequestVars($this->name);
151
        return $this->Process->make($this->Groups, $action, $ids);
0 ignored issues
show
Bug introduced by
It seems like $action defined by $this->Process->getRequestVars($this->name) on line 150 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...
152
    }
153
154
    /**
155
     * Move up group action.
156
     *
157
     * @param   int $id Group id.
158
     * @return  \Cake\Http\Response|null
159
     *
160
     * @SuppressWarnings(PHPMD.ShortMethodName)
161
     */
162
    public function up($id)
163
    {
164
        return $this->Move->up($this->Groups, $id);
165
    }
166
}
167