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

Profile   C

Complexity

Total Complexity 25

Size/Duplication

Total Lines 187
Duplicated Lines 35.83 %

Coupling/Cohesion

Components 0
Dependencies 19

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 25
c 7
b 2
f 0
lcom 0
cbo 19
dl 67
loc 187
rs 6.875

7 Methods

Rating   Name   Duplication   Size   Complexity  
A actionDelete() 0 4 1
B actionIndex() 26 26 1
C actionUpdate() 0 32 8
A actionFieldlist() 0 8 1
B actionFieldupdate() 23 23 5
B actionFielddelete() 0 24 6
A actionSettings() 18 18 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\AdminController;
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 AdminController
18
{
19
    const VERSION = 0.1;
20
    const ITEM_PER_PAGE = 10;
21
22
    public $type = 'app';
23
24
    /**
25
     * List all profiles in website with pagination
26
     * @return string
27
     * @throws \Ffcms\Core\Exception\SyntaxException
28
     */
29 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...
30
    {
31
        // init Active Record
32
        $query = new ProfileRecords();
33
34
        // set current page and offset
35
        $page = (int)App::$Request->query->get('page');
36
        $offset = $page * self::ITEM_PER_PAGE;
37
38
        // build pagination
39
        $pagination = new SimplePagination([
40
            'url' => ['profile/index'],
41
            'page' => $page,
42
            'step' => self::ITEM_PER_PAGE,
43
            'total' => $query->count()
44
        ]);
45
46
        // build listing objects
47
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
48
49
        // display viewer
50
        return App::$View->render('index', [
51
            'records' => $records,
52
            'pagination' => $pagination
53
        ]);
54
    }
55
56
    /**
57
     * Redirect to user controller
58
     * @param $id
59
     */
60
    public function actionDelete($id)
61
    {
62
        App::$Response->redirect('user/delete/' . $id);
63
    }
64
65
    /**
66
     * Profile edit action
67
     * @param int $id
68
     * @return string
69
     * @throws NotFoundException
70
     */
71
    public function actionUpdate($id)
72
    {
73
        if (!Obj::isLikeInt($id) || $id < 1) {
74
            throw new NotFoundException();
75
        }
76
77
        // get user profile via id
78
        $profile = ProfileRecords::find($id);
79
        if (false === $profile || null === $profile) {
80
            throw new NotFoundException();
81
        }
82
83
        // check if user id for this profile_id is exist
84
        if (!App::$User->isExist($profile->user_id)) {
85
            throw new NotFoundException();
86
        }
87
88
        // get user object from profile
89
        $user = $profile->User();
90
        $model = new FrontFormSettings($user);
91
92
        if ($model->send() && $model->validate()) {
93
            $model->save();
94
            App::$Session->getFlashBag()->add('success', __('Profile is updated'));
95
        }
96
97
        return App::$View->render('update', [
98
            'model' => $model->export(),
99
            'user' => $user,
100
            'profile' => $profile
101
        ]);
102
    }
103
104
    /**
105
     * List additional fields
106
     * @return string
107
     * @throws \Ffcms\Core\Exception\SyntaxException
108
     */
109
    public function actionFieldlist()
110
    {
111
        $records = ProfileField::all();
112
113
        return App::$View->render('field_list', [
114
            'records' => $records
115
        ]);
116
    }
117
118
    /**
119
     * Add and edit additional fields data
120
     * @param $id
121
     * @return string
122
     */
123 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...
124
    {
125
        // get current record or new and init form DI
126
        $record = ProfileField::findOrNew($id);
127
        $model = new FormFieldUpdate($record);
128
129
        $isNew = false;
130
        if ($record->id === null) {
131
            $isNew = true;
132
        }
133
        // check if form is submited
134
        if ($model->send() && $model->validate()) {
135
            $model->save();
136
            if (true === $isNew) {
137
                App::$Response->redirect('profile/fieldlist');
138
            }
139
            App::$Session->getFlashBag()->add('success', __('Profile field was successful updated'));
140
        }
141
142
        return App::$View->render('field_update', [
143
            'model' => $model->export()
144
        ]);
145
    }
146
147
    /**
148
     * Delete custom field action
149
     * @param int $id
150
     * @return string
151
     * @throws ForbiddenException
152
     */
153
    public function actionFielddelete($id)
154
    {
155
        if (!Obj::isLikeInt($id) || $id < 1) {
156
            throw new ForbiddenException();
157
        }
158
159
        // check if record with $id is exist
160
        $record = ProfileField::find($id);
161
        if ($record === null || $record === false) {
162
            throw new ForbiddenException();
163
        }
164
165
        $model = new FormFieldUpdate($record);
0 ignored issues
show
Compatibility introduced by
$record of type object<Ffcms\Core\Arch\ActiveModel> is not a sub-type of object<Apps\ActiveRecord\ProfileField>. It seems like you assume a child class of the class Ffcms\Core\Arch\ActiveModel to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
166
167
        // if delete is submited - lets remove this record
168
        if ($model->send()) {
169
            $model->delete();
170
            App::$Response->redirect('profile/fieldlist');
171
        }
172
173
        return App::$View->render('field_delete', [
174
            'model' => $model->export()
175
        ]);
176
    }
177
178
    /**
179
     * Show profiles settings
180
     * @return string
181
     * @throws \Ffcms\Core\Exception\SyntaxException
182
     */
183 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...
184
    {
185
        $model = new FormSettings($this->getConfigs());
186
187
        if ($model->send()) {
188
            if ($model->validate()) {
189
                $this->setConfigs($model->getAllProperties());
190
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
191
                App::$Response->redirect('profile/index');
192
            } else {
193
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
194
            }
195
        }
196
197
        return App::$View->render('settings', [
198
            'model' => $model
199
        ]);
200
    }
201
202
203
}