Passed
Push — master ( 939347...72ceee )
by Mihail
07:40
created

User   C

Complexity

Total Complexity 19

Size/Duplication

Total Lines 192
Duplicated Lines 58.33 %

Coupling/Cohesion

Components 0
Dependencies 19

Importance

Changes 8
Bugs 3 Features 0
Metric Value
wmc 19
c 8
b 3
f 0
lcom 0
cbo 19
dl 112
loc 192
rs 6.875

7 Methods

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

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
17
18
class User extends AdminController
19
{
20
    const VERSION = 0.1;
21
    const ITEM_PER_PAGE = 10;
22
23
    public $type = 'app';
24
25
    /**
26
     * List all users as table
27
     * @return string
28
     * @throws \Ffcms\Core\Exception\SyntaxException
29
     */
30 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...
31
    {
32
        // init Active Record
33
        $query = new UserRecords();
34
35
        // set current page and offset
36
        $page = (int)App::$Request->query->get('page');
37
        $offset = $page * self::ITEM_PER_PAGE;
38
39
        // build pagination
40
        $pagination = new SimplePagination([
41
            'url' => ['user/index'],
42
            'page' => $page,
43
            'step' => self::ITEM_PER_PAGE,
44
            'total' => $query->count()
45
        ]);
46
47
        // build listing objects
48
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
49
50
        // display viewer
51
        return App::$View->render('index', [
52
            'records' => $records,
53
            'pagination' => $pagination
54
        ]);
55
    }
56
57
    /**
58
     * Edit user profile by id
59
     * @param int $id
60
     * @return string
61
     * @throws \Ffcms\Core\Exception\SyntaxException
62
     */
63 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...
64
    {
65
        $user = UserRecords::findOrNew($id);
66
67
        // find user identify object
68
        //$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...
69
        // generate model data based on user object
70
        $model = new FormUserUpdate($user);
71
72
        // check is form is sended
73
        if ($model->send()) {
74
            if ($model->validate()) { // check validation
75
                $model->save();
76
                App::$Session->getFlashBag()->add('success', __('Data was successful updated'));
77
            } else {
78
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
79
            }
80
        }
81
82
        // render viewer
83
        return App::$View->render('user_update', [
84
            'model' => $model->export()
85
        ]);
86
    }
87
88
    /**
89
     * Delete user row from database
90
     * @param int $id
91
     * @return string
92
     * @throws NotFoundException
93
     */
94
    public function actionDelete($id)
95
    {
96
        if ($id < !1 || !App::$User->isExist($id)) {
97
            throw new NotFoundException('User is not founded');
98
        }
99
100
        // get user object and load model
101
        $user = App::$User->identity($id);
102
        $model = new FormUserDelete($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by \Ffcms\Core\App::$User->identity($id) on line 101 can be null; however, Apps\Model\Admin\User\Fo...erDelete::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
103
104
        if ($model->send()) {
105
            $model->delete();
106
            App::$Response->redirect('user/index');
107
        } else {
108
            App::$Session->getFlashBag()->add('error', __('There is no way to revert this action! Be careful!'));
109
        }
110
111
        // set view response
112
        return App::$View->render('user_delete', [
113
            'model' => $model
114
        ]);
115
    }
116
117
    /**
118
     * Show all role groups
119
     * @return string
120
     * @throws \Ffcms\Core\Exception\SyntaxException
121
     */
122
    public function actionGrouplist()
123
    {
124
        // get all roles
125
        $roles = Role::getAll();
126
127
        return App::$View->render('group_list', [
128
            'records' => $roles
129
        ]);
130
    }
131
132
    /**
133
     * Edit and add groups
134
     * @param int $id
135
     * @return string
136
     */
137 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...
138
    {
139
        // find role or create new object
140
        $role = Role::findOrNew($id);
141
142
        $model = new FormUserGroupUpdate($role);
143
        if ($model->send()) { // work with post request
144
            if ($model->validate()) {
145
                $model->save();
146
                App::$Session->getFlashBag()->add('success', __('Data was successful updated'));
147
            } else {
148
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
149
            }
150
        }
151
152
        // render view
153
        return App::$View->render('group_update', [
154
            'model' => $model
155
        ]);
156
    }
157
158
    /**
159
     * User identity settings
160
     * @return string
161
     */
162 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...
163
    {
164
        // load model and pass property's as argument
165
        $model = new FormUserSettings($this->getConfigs());
166
167
        if ($model->send()) {
168
            if ($model->validate()) {
169
                $this->setConfigs($model->getAllProperties());
170
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
171
                App::$Response->redirect('user/index');
172
            } else {
173
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
174
            }
175
        }
176
177
        // render view
178
        return App::$View->render('settings', [
179
            'model' => $model->export()
180
        ]);
181
    }
182
183
    /**
184
     * Send invite to user by email
185
     * @return string
186
     */
187 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...
188
    {
189
        // init model
190
        $model = new FormInviteSend();
191
192
        if ($model->send()) {
193
            if ($model->validate()) {
194
                if ($model->make()) {
195
                    App::$Session->getFlashBag()->add('success', __('Invite was successful send!'));
196
                } else {
197
                    App::$Session->getFlashBag()->add('error', __('Mail server connection is failed!'));
198
                }
199
            } else {
200
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
201
            }
202
        }
203
204
        // render view
205
        return App::$View->render('invite', [
206
            'model' => $model
207
        ]);
208
    }
209
}