Passed
Push — master ( 02a4a5...734a7e )
by Mihail
05:36
created

User   B

Complexity

Total Complexity 23

Size/Duplication

Total Lines 215
Duplicated Lines 56.74 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 12
Bugs 5 Features 0
Metric Value
c 12
b 5
f 0
dl 122
loc 215
rs 8.4614
wmc 23
lcom 1
cbo 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
B actionUpdate() 24 24 3
A actionGrouplist() 0 9 1
A actionGroupUpdate() 20 20 3
B actionInvite() 22 22 4
B actionIndex() 26 26 1
C actionDelete() 10 34 8
A actionSettings() 20 20 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Apps\Controller\Admin;
4
5
use Apps\ActiveRecord\Role;
6
use Apps\Model\Admin\User\FormInviteSend;
7
use Apps\Model\Admin\User\FormUserDelete;
8
use Apps\Model\Admin\User\FormUserGroupUpdate;
9
use Apps\Model\Admin\User\FormUserSettings;
10
use Apps\Model\Admin\User\FormUserUpdate;
11
use Extend\Core\Arch\AdminController;
12
use Apps\ActiveRecord\User as UserRecords;
13
use Ffcms\Core\App;
14
use Ffcms\Core\Exception\NotFoundException;
15
use Ffcms\Core\Helper\HTML\SimplePagination;
16
use Ffcms\Core\Helper\Type\Arr;
17
use Ffcms\Core\Helper\Type\Obj;
18
19
20
/**
21
 * Class User. Admin controller of user application.
22
 * @package Apps\Controller\Admin
23
 */
24
class User extends AdminController
25
{
26
    const VERSION = 0.1;
27
    const ITEM_PER_PAGE = 10;
28
29
    public $type = 'app';
30
31
    /**
32
     * List all users as table
33
     * @return string
34
     * @throws \Ffcms\Core\Exception\NativeException
35
     * @throws \Ffcms\Core\Exception\SyntaxException
36
     */
37 View Code Duplication
    public function actionIndex()
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...
38
    {
39
        // init Active Record
40
        $query = new UserRecords();
41
42
        // set current page and offset
43
        $page = (int)$this->request->query->get('page', 0);
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Apps\Controller\Admin\User>. 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...
44
        $offset = $page * self::ITEM_PER_PAGE;
45
46
        // build pagination
47
        $pagination = new SimplePagination([
48
            'url' => ['user/index'],
49
            'page' => $page,
50
            'step' => self::ITEM_PER_PAGE,
51
            'total' => $query->count()
52
        ]);
53
54
        // build listing objects
55
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
56
57
        // display viewer
58
        return $this->view->render('index', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\User>. 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...
59
            'records' => $records,
60
            'pagination' => $pagination
61
        ]);
62
    }
63
64
    /**
65
     * Edit user profile by id
66
     * @param int $id
67
     * @return string
68
     * @throws \Ffcms\Core\Exception\NativeException
69
     * @throws \Ffcms\Core\Exception\SyntaxException
70
     */
71 View Code Duplication
    public function actionUpdate($id)
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...
72
    {
73
        $user = UserRecords::findOrNew($id);
74
75
        // find user identify object
76
        //$user = App::$User->identity($id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
77
        // generate model data based on user object
78
        $model = new FormUserUpdate($user);
79
80
        // check is form is sended
81
        if ($model->send()) {
82
            if ($model->validate()) { // check validation
83
                $model->save();
84
                App::$Session->getFlashBag()->add('success', __('Data was successful updated'));
85
            } else {
86
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
87
            }
88
        }
89
90
        // render viewer
91
        return $this->view->render('user_update', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\User>. 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...
92
            'model' => $model->filter()
93
        ]);
94
    }
95
96
    /**
97
     * Delete user row from database
98
     * @param int $id
99
     * @return string
100
     * @throws \Ffcms\Core\Exception\SyntaxException
101
     * @throws \Ffcms\Core\Exception\NativeException
102
     * @throws NotFoundException
103
     */
104
    public function actionDelete($id = null)
105
    {
106
        // check if id is passed or get data from GET as array ids
107 View Code Duplication
        if ($id === 0 || (int)$id < 1) {
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...
108
            $ids = $this->request->query->get('selected');
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Apps\Controller\Admin\User>. 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...
109
            if (Obj::isArray($ids) && Arr::onlyNumericValues($ids)) {
110
                $id = $ids;
111
            } else {
112
                throw new NotFoundException('Bad conditions');
113
            }
114
        } else {
115
            $id = [$id];
116
        }
117
118
        // initialize delete model
119
        $model = new FormUserDelete($id);
120
121
        // check if users is found
122
        if ($model->users === null) {
123
            throw new NotFoundException(__('Users are not found'));
124
        }
125
126
        // check if delete is submited
127
        if ($model->send() && $model->validate()) {
128
            $model->delete();
129
            App::$Session->getFlashBag()->add('success', __('Users and them data are successful removed'));
130
            $this->response->redirect('user/index');
0 ignored issues
show
Bug introduced by
The method redirect cannot be called on $this->response (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
131
        }
132
133
        // set view response
134
        return $this->view->render('user_delete', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\User>. 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...
135
            'model' => $model
136
        ]);
137
    }
138
139
    /**
140
     * Show all role groups
141
     * @return string
142
     * @throws \Ffcms\Core\Exception\NativeException
143
     * @throws \Ffcms\Core\Exception\SyntaxException
144
     */
145
    public function actionGrouplist()
146
    {
147
        // get all roles
148
        $roles = Role::getAll();
149
150
        return $this->view->render('group_list', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\User>. 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...
151
            'records' => $roles
152
        ]);
153
    }
154
155
    /**
156
     * Edit and add groups
157
     * @param int $id
158
     * @return string
159
     * @throws \Ffcms\Core\Exception\SyntaxException
160
     * @throws \Ffcms\Core\Exception\NativeException
161
     */
162 View Code Duplication
    public function actionGroupUpdate($id)
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...
163
    {
164
        // find role or create new object
165
        $role = Role::findOrNew($id);
166
167
        $model = new FormUserGroupUpdate($role);
168
        if ($model->send()) { // work with post request
169
            if ($model->validate()) {
170
                $model->save();
171
                App::$Session->getFlashBag()->add('success', __('Data was successful updated'));
172
            } else {
173
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
174
            }
175
        }
176
177
        // render view
178
        return $this->view->render('group_update', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\User>. 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...
179
            'model' => $model->filter()
180
        ]);
181
    }
182
183
    /**
184
     * User identity settings
185
     * @return string
186
     * @throws \Ffcms\Core\Exception\SyntaxException
187
     * @throws \Ffcms\Core\Exception\NativeException
188
     */
189 View Code Duplication
    public function actionSettings()
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...
190
    {
191
        // load model and pass property's as argument
192
        $model = new FormUserSettings($this->getConfigs());
193
194
        if ($model->send()) {
195
            if ($model->validate()) {
196
                $this->setConfigs($model->getAllProperties());
197
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
198
                $this->response->redirect('user/index');
0 ignored issues
show
Bug introduced by
The method redirect cannot be called on $this->response (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
199
            } else {
200
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
201
            }
202
        }
203
204
        // render view
205
        return $this->view->render('settings', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\User>. 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...
206
            'model' => $model->filter()
207
        ]);
208
    }
209
210
    /**
211
     * Send invite to user by email
212
     * @return string
213
     * @throws \Ffcms\Core\Exception\SyntaxException
214
     * @throws \Ffcms\Core\Exception\NativeException
215
     */
216 View Code Duplication
    public function actionInvite()
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...
217
    {
218
        // init model
219
        $model = new FormInviteSend();
220
221
        if ($model->send()) {
222
            if ($model->validate()) {
223
                if ($model->make()) {
224
                    App::$Session->getFlashBag()->add('success', __('Invite was successful send!'));
225
                } else {
226
                    App::$Session->getFlashBag()->add('error', __('Mail server connection is failed!'));
227
                }
228
            } else {
229
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
230
            }
231
        }
232
233
        // render view
234
        return $this->view->render('invite', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\User>. 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...
235
            'model' => $model->filter()
236
        ]);
237
    }
238
}