GroupsController::edit()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 20

Duplication

Lines 16
Ratio 44.44 %

Importance

Changes 0
Metric Value
dl 16
loc 36
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 4
nop 0
1
<?php
2
namespace App\Controller\Admin;
3
4
use App\Controller\AppController;
5
use App\Event\Statistics;
6
use Cake\Event\Event;
7
use Cake\I18n\I18n;
8
9
class GroupsController extends AppController
10
{
11
12
    /**
13
     * Helpers.
14
     *
15
     * @var array
16
     */
17
    public $helpers = ['I18n'];
18
19
    /**
20
     * Display all Groups.
21
     *
22
     * @return void
23
     */
24
    public function index()
25
    {
26
        $this->paginate = [
27
            'maxLimit' => 15
28
        ];
29
30
        $groups = $this->Groups
0 ignored issues
show
Documentation introduced by
The property Groups does not exist on object<App\Controller\Admin\GroupsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
31
            ->find()
32
            ->order([
33
                'Groups.created' => 'desc'
34
            ]);
35
36
        $groups = $this->paginate($groups);
37
        $this->set(compact('groups'));
38
    }
39
40
    /**
41
     * Add a Group.
42
     *
43
     * @return \Cake\Network\Response|void
44
     */
45
    public function add()
46
    {
47
        $this->Groups->locale(I18n::defaultLocale());
0 ignored issues
show
Documentation introduced by
The property Groups does not exist on object<App\Controller\Admin\GroupsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
48
        $group = $this->Groups->newEntity($this->request->getParsedBody());
0 ignored issues
show
Documentation introduced by
The property Groups does not exist on object<App\Controller\Admin\GroupsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
50 View Code Duplication
        if ($this->request->is('post')) {
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...
51
            $group->setTranslations($this->request->getParsedBody());
52
53
            if ($this->Groups->save($group)) {
0 ignored issues
show
Documentation introduced by
The property Groups does not exist on object<App\Controller\Admin\GroupsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
                //Event.
55
                $this->eventManager()->attach(new Statistics());
0 ignored issues
show
Documentation introduced by
new \App\Event\Statistics() is of type object<App\Event\Statistics>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
56
57
                $stats = new Event('Model.Groups.update', $this);
58
                $this->eventManager()->dispatch($stats);
59
60
                $this->Flash->success(__d('admin', 'Your group has been created successfully !'));
61
62
                return $this->redirect(['action' => 'index']);
63
            }
64
        }
65
66
        $this->set(compact('group'));
67
    }
68
69
    /**
70
     * Edit a Group.
71
     *
72
     * @return \Cake\Network\Response|void
73
     */
74
    public function edit()
75
    {
76
        $this->Groups->locale(I18n::defaultLocale());
0 ignored issues
show
Documentation introduced by
The property Groups does not exist on object<App\Controller\Admin\GroupsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
77
        $group = $this->Groups
0 ignored issues
show
Documentation introduced by
The property Groups does not exist on object<App\Controller\Admin\GroupsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
78
            ->find('translations')
79
            ->where([
80
                'Groups.id' => $this->request->id
81
            ])
82
            ->first();
83
84
        //Check if the group is found.
85
        if (empty($group)) {
86
            $this->Flash->error(__d('admin', 'This group doesn\'t exist or has been deleted.'));
87
88
            return $this->redirect(['action' => 'index']);
89
        }
90
91 View Code Duplication
        if ($this->request->is('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...
92
            $this->Groups->patchEntity($group, $this->request->getParsedBody());
0 ignored issues
show
Documentation introduced by
The property Groups does not exist on object<App\Controller\Admin\GroupsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
93
            $group->setTranslations($this->request->getParsedBody());
94
95
            if ($this->Groups->save($group)) {
0 ignored issues
show
Documentation introduced by
The property Groups does not exist on object<App\Controller\Admin\GroupsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96
                //Event.
97
                $this->eventManager()->attach(new Statistics());
0 ignored issues
show
Documentation introduced by
new \App\Event\Statistics() is of type object<App\Event\Statistics>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
98
99
                $stats = new Event('Model.Groups.update', $this);
100
                $this->eventManager()->dispatch($stats);
101
102
                $this->Flash->success(__d('admin', 'This group has been updated successfully !'));
103
104
                return $this->redirect(['action' => 'index']);
105
            }
106
        }
107
108
        $this->set(compact('group'));
109
    }
110
111
    /**
112
     * Delete a group.
113
     *
114
     * @return \Cake\Network\Response
115
     */
116
    public function delete()
117
    {
118
        $group = $this->Groups
0 ignored issues
show
Documentation introduced by
The property Groups does not exist on object<App\Controller\Admin\GroupsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
119
            ->find()
120
            ->where([
121
                'Groups.id' => $this->request->id
122
            ])
123
            ->contain([
124
                'Users' => function ($q) {
125
                    return $q->limit(1);
126
                }
127
            ])
128
            ->first();
129
130
        //Check if the group is found.
131
        if (empty($group)) {
132
            $this->Flash->error(__d('admin', 'This group doesn\'t exist or has been deleted.'));
133
134
            return $this->redirect(['action' => 'index']);
135
        }
136
137
        //Check if the group is assigned to one or more user(s).
138
        if (!empty($group->users)) {
139
            $this->Flash->error(__d('admin', 'This group is assigned to one or more user(s). You must change their group before to delete this group.'));
140
141
            return $this->redirect(['action' => 'index']);
142
        }
143
144
        if ($this->Groups->delete($group)) {
0 ignored issues
show
Documentation introduced by
The property Groups does not exist on object<App\Controller\Admin\GroupsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
145
            //Event.
146
            $this->eventManager()->attach(new Statistics());
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Documentation introduced by
new \App\Event\Statistics() is of type object<App\Event\Statistics>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
148
            $stats = new Event('Model.Groups.update', $this);
149
            $this->eventManager()->dispatch($stats);
150
151
            $this->Flash->success(__d('admin', 'This group has been deleted successfully !'));
152
153
            return $this->redirect(['action' => 'index']);
154
        }
155
156
        $this->Flash->error(__d('admin', 'Unable to delete this group.'));
157
158
        return $this->redirect(['action' => 'index']);
159
    }
160
}
161