Passed
Push — master ( b7618a...8c3cc9 )
by Mihail
15:19
created

Profile   B

Complexity

Total Complexity 25

Size/Duplication

Total Lines 166
Duplicated Lines 39.76 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 25
c 3
b 0
f 0
lcom 1
cbo 18
dl 66
loc 166
rs 7.3333

7 Methods

Rating   Name   Duplication   Size   Complexity  
B actionIndex() 26 26 1
A actionDelete() 0 4 1
C actionUpdate() 0 32 8
A actionFieldlist() 0 8 1
B actionFieldupdate() 23 23 5
B actionFielddelete() 0 24 6
A actionSettings() 17 17 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\ProfileField;
6
use Apps\Model\Admin\Profile\FormFieldUpdate;
7
use Apps\Model\Admin\Profile\FormSettings;
8
use Apps\Model\Front\Profile\FormSettings as FrontFormSettings;
9
use Extend\Core\Arch\AdminAppController;
10
use Apps\ActiveRecord\Profile as ProfileRecords;
11
use Ffcms\Core\App;
12
use Ffcms\Core\Exception\ForbiddenException;
13
use Ffcms\Core\Exception\NotFoundException;
14
use Ffcms\Core\Helper\HTML\SimplePagination;
15
use Ffcms\Core\Helper\Type\Obj;
16
17
class Profile extends AdminAppController
18
{
19
    const ITEM_PER_PAGE = 10;
20
21
    // profile list
22 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...
23
    {
24
        // init Active Record
25
        $query = new ProfileRecords();
26
27
        // set current page and offset
28
        $page = (int)App::$Request->query->get('page');
29
        $offset = $page * self::ITEM_PER_PAGE;
30
31
        // build pagination
32
        $pagination = new SimplePagination([
33
            'url' => ['profile/index'],
34
            'page' => $page,
35
            'step' => self::ITEM_PER_PAGE,
36
            'total' => $query->count()
0 ignored issues
show
Documentation Bug introduced by
The method count does not exist on object<Apps\ActiveRecord\Profile>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
37
        ]);
38
39
        // build listing objects
40
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
0 ignored issues
show
Documentation Bug introduced by
The method orderBy does not exist on object<Apps\ActiveRecord\Profile>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
41
42
        // display viewer
43
        $this->response = App::$View->render('index', [
44
            'records' => $records,
45
            'pagination' => $pagination
46
        ]);
47
    }
48
49
    // redirect delete action to user controller
50
    public function actionDelete($id)
51
    {
52
        App::$Response->redirect('user/delete/' . $id);
53
    }
54
55
    /**
56
     * Profile edit action
57
     * @param int $id
58
     * @throws NotFoundException
59
     */
60
    public function actionUpdate($id)
61
    {
62
        if (!Obj::isLikeInt($id) || $id < 1) {
63
            throw new NotFoundException();
64
        }
65
66
        // get user profile via id
67
        $profile = ProfileRecords::find($id);
68
        if (false === $profile || null === $profile) {
69
            throw new NotFoundException();
70
        }
71
72
        // check if user id for this profile_id is exist
73
        if (!App::$User->isExist($profile->user_id)) {
74
            throw new NotFoundException();
75
        }
76
77
        // get user object from profile
78
        $user = $profile->User();
79
        $model = new FrontFormSettings($user);
80
81
        if ($model->send() && $model->validate()) {
82
            $model->save();
83
            App::$Session->getFlashBag()->add('success', __('Profile is updated'));
84
        }
85
86
        $this->response = App::$View->render('update', [
87
            'model' => $model->export(),
88
            'user' => $user,
89
            'profile' => $profile
90
        ]);
91
    }
92
93
    /**
94
     * List additional fields
95
     */
96
    public function actionFieldlist()
97
    {
98
        $records = ProfileField::all();
99
100
        $this->response = App::$View->render('field_list', [
101
            'records' => $records
102
        ]);
103
    }
104
105
    /**
106
     * Add and edit additional fields data
107
     * @param $id
108
     */
109 View Code Duplication
    public function actionFieldupdate($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...
110
    {
111
        // get current record or new and init form DI
112
        $record = ProfileField::findOrNew($id);
113
        $model = new FormFieldUpdate($record);
0 ignored issues
show
Documentation introduced by
$record is of type object<Illuminate\Suppor...atabase\Eloquent\Model>, but the function expects a object<Apps\ActiveRecord\ProfileField>.

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...
114
115
        $isNew = false;
116
        if ($record->id === null) {
117
            $isNew = true;
118
        }
119
        // check if form is submited
120
        if ($model->send() && $model->validate()) {
121
            $model->save();
122
            if (true === $isNew) {
123
                App::$Response->redirect('profile/fieldlist');
124
            }
125
            App::$Session->getFlashBag()->add('success', __('Profile field was successful updated'));
126
        }
127
128
        $this->response = App::$View->render('field_update', [
129
            'model' => $model->export()
130
        ]);
131
    }
132
133
    /**
134
     * Delete custom field action
135
     * @param int $id
136
     * @throws ForbiddenException
137
     */
138
    public function actionFielddelete($id)
139
    {
140
        if (!Obj::isLikeInt($id) || $id < 1) {
141
            throw new ForbiddenException();
142
        }
143
144
        // check if record with $id is exist
145
        $record = ProfileField::find($id);
146
        if ($record === null || $record === false) {
147
            throw new ForbiddenException();
148
        }
149
150
        $model = new FormFieldUpdate($record);
151
152
        // if delete is submited - lets remove this record
153
        if ($model->send()) {
154
            $model->delete();
155
            App::$Response->redirect('profile/fieldlist');
156
        }
157
158
        $this->response = App::$View->render('field_delete', [
159
            'model' => $model->export()
160
        ]);
161
    }
162
163 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...
164
    {
165
        $model = new FormSettings($this->getConfigs());
166
167
        if ($model->send()) {
168
            if ($model->validate()) {
169
                $this->setConfigs($model->getAllProperties());
170
                App::$Response->redirect('profile/index');
171
            } else {
172
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
173
            }
174
        }
175
176
        $this->response = App::$View->render('settings', [
177
            'model' => $model
178
        ]);
179
    }
180
181
182
}