GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — filters ( 233a3a...88ffb1 )
by Alexander
29:32
created

BackendWarehouseController   C

Complexity

Total Complexity 38

Size/Duplication

Total Lines 288
Duplicated Lines 54.86 %

Coupling/Cohesion

Components 1
Dependencies 20
Metric Value
wmc 38
lcom 1
cbo 20
dl 158
loc 288
rs 5.4353

10 Methods

Rating   Name   Duplication   Size   Complexity  
A actions() 0 19 1
A actionIndex() 0 19 1
D actionEdit() 57 105 17
A actionDelete() 19 19 3
A actionEditPhone() 16 16 3
A actionEditEmail() 14 14 3
A actionDeletePhone() 19 19 3
A actionDeleteEmail() 19 19 3
A actionUpdateRemains() 0 22 3
A behaviors() 14 14 1

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 app\modules\shop\controllers;
4
5
use app\backend\components\BackendController;
6
use app\components\SearchModel;
7
use app\modules\shop\models\WarehouseEmail;
8
use app\modules\shop\models\WarehouseOpeninghours;
9
use app\modules\shop\models\WarehousePhone;
10
use devgroup\TagDependencyHelper\ActiveRecordHelper;
11
use app\backend\actions\DeleteOne;
12
use app\backend\actions\MultipleDelete;
13
use app\backend\actions\UpdateEditable;
14
use app\modules\shop\models\Warehouse;
15
use app\modules\shop\models\WarehouseProduct;
16
use yii\helpers\Url;
17
use Yii;
18
use yii\data\ActiveDataProvider;
19
use yii\filters\AccessControl;
20
use yii\web\HttpException;
21
use yii\web\NotFoundHttpException;
22
use yii\caching\TagDependency;
23
24
class BackendWarehouseController extends BackendController
25
{
26
    /**
27
     * @inheritdoc
28
     */
29 View Code Duplication
    public function behaviors()
1 ignored issue
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
        return [
32
            'access' => [
33
                'class' => AccessControl::className(),
34
                'rules' => [
35
                    [
36
                        'allow' => true,
37
                        'roles' => ['product manage'],
38
                    ],
39
                ],
40
            ],
41
        ];
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function actions()
48
    {
49
        return [
50
            'remove-all' => [
51
                'class' => MultipleDelete::className(),
52
                'modelName' => Warehouse::className(),
53
            ],
54
            'delete' => [
55
                'class' => DeleteOne::className(),
56
                'modelName' => Warehouse::className(),
57
            ],
58
            'update-editable' => [
59
                'class' => UpdateEditable::className(),
60
                'modelName' => Warehouse::className(),
61
                'allowedAttributes' => [
62
                ],
63
            ],
64
        ];
65
    }
66
67
    public function actionIndex()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
68
    {
69
        ;
70
        $searchModel = new SearchModel(
71
            [
72
                'model' => Warehouse::className(),
73
                'partialMatchAttributes' => ['name'],
74
                'scenario' => 'default',
75
            ]
76
        );
77
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
78
        return $this->render(
79
            'index',
80
            [
81
                'dataProvider' => $dataProvider,
82
                'searchModel' => $searchModel,
83
            ]
84
        );
85
    }
86
87
    public function actionEdit($id = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
88
    {
89
        $model = new Warehouse;
90
        $model->loadDefaultValues();
91
92
        if ($id !== null) {
93
            $model = Warehouse::findOne($id);
94
        }
95
96
97 View Code Duplication
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
98
            if ($model->save()) {
99
                Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved'));
100
                $returnUrl = Yii::$app->request->get(
101
                    'returnUrl',
102
                    ['/shop/backend-warehouse/index']
103
                );
104
                switch (Yii::$app->request->post('action', 'save')) {
105
                    case 'next':
106
                        return $this->redirect(
107
                            [
108
                                '/shop/backend-warehouse/edit',
109
                                'returnUrl' => $returnUrl,
110
                            ]
111
                        );
112
                    case 'back':
113
                        return $this->redirect($returnUrl);
114
                    default:
115
                        return $this->redirect(
116
                            Url::toRoute(
117
                                [
118
                                    '/shop/backend-warehouse/edit',
119
                                    'id' => $model->id,
120
                                    'returnUrl' => $returnUrl,
121
                                ]
122
                            )
123
                        );
124
                }
125
            }
126
127
128
        }
129
130
        $wareHouseOpeningHours = WarehouseOpeninghours::find()->where(['warehouse_id' => $model->id])->one();
131
        if ($wareHouseOpeningHours === null) {
132
            $wareHouseOpeningHours = new WarehouseOpeninghours();
133
        }
134
        $wareHouseOpeningHours->loadDefaultValues();
135 View Code Duplication
        if (Yii::$app->request->post('WarehouseOpeninghours') && !$model->isNewRecord) {
136
            $wareHouseOpeningHours->load(Yii::$app->request->post());
137
            $wareHouseOpeningHours->warehouse_id = $model->id;
138
            if ($wareHouseOpeningHours->save()) {
139
                $this->refresh();
140
            }
141
        }
142
143
        $warehousePhone = new WarehousePhone();
144
145 View Code Duplication
        if (Yii::$app->request->post('WarehousePhone') && !$model->isNewRecord) {
146
            $warehousePhone->loadDefaultValues();
147
            $warehousePhone->load(Yii::$app->request->post());
148
            $warehousePhone->warehouse_id = $model->id;
149
            if ($warehousePhone->save()) {
150
                $this->refresh();
151
            }
152
153
        }
154
        $warehousePhoneProvider = new ActiveDataProvider([
155
            'query' => $warehousePhone::find()->where(['warehouse_id' => $model->id]),
156
            'pagination' => [
157
                'pageSize' => 20,
158
            ],
159
        ]);
160
161
162
        $warehouseEmail = new WarehouseEmail();
163 View Code Duplication
        if (Yii::$app->request->post('WarehouseEmail') && !$model->isNewRecord) {
164
            $warehouseEmail->loadDefaultValues();
165
            $warehouseEmail->load(Yii::$app->request->post());
166
            $warehouseEmail->warehouse_id = $model->id;
167
            if ($warehouseEmail->save()) {
168
                $this->refresh();
169
            }
170
171
        }
172
        $warehouseEmailProvider = new ActiveDataProvider([
173
            'query' => $warehouseEmail::find()->where(['warehouse_id' => $model->id]),
174
            'pagination' => [
175
                'pageSize' => 20,
176
            ],
177
        ]);
178
179
180
        return $this->render(
181
            'form',
182
            [
183
                'model' => $model,
184
                'wareHouseOpeningHours' => $wareHouseOpeningHours,
185
                'warehousePhone' => $warehousePhone,
186
                'warehousePhoneProvider' => $warehousePhoneProvider,
187
                'warehouseEmail' => $warehouseEmail,
188
                'warehouseEmailProvider' => $warehouseEmailProvider
189
            ]
190
        );
191
    }
192
193
194 View Code Duplication
    public function actionDelete($id)
195
    {
196
        if (!$model = Warehouse::findOne($id)) {
197
            throw new NotFoundHttpException;
198
        }
199
200
        if (!$model->delete()) {
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $model (of type array|boolean).

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...
201
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object not removed'));
202
        } else {
203
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed'));
204
        }
205
206
        return $this->redirect(
207
            Yii::$app->request->get(
208
                'returnUrl',
209
                '/shop/backend-warehouse/index'
210
            )
211
        );
212
    }
213
214 View Code Duplication
    public function actionEditPhone($id, $returnUrl)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
215
    {
216
        $warehousePhone = WarehousePhone::findOne($id);
217
218
        if (Yii::$app->request->post('WarehousePhone')) {
219
            $warehousePhone->loadDefaultValues();
0 ignored issues
show
Bug introduced by
The method loadDefaultValues cannot be called on $warehousePhone (of type array|boolean).

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...
220
            $warehousePhone->load(Yii::$app->request->post());
0 ignored issues
show
Bug introduced by
The method load cannot be called on $warehousePhone (of type array|boolean).

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...
221
            if ($warehousePhone->save()) {
0 ignored issues
show
Bug introduced by
The method save cannot be called on $warehousePhone (of type array|boolean).

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...
222
                $this->redirect($returnUrl);
223
            }
224
        }
225
226
227
228
        return $this->render('form_edit_phone', ['warehousePhone' => $warehousePhone]);
229
    }
230
231
232 View Code Duplication
    public function actionEditEmail($id, $returnUrl)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
233
    {
234
        $warehouseEmail = WarehouseEmail::findOne($id);
235
236
        if (Yii::$app->request->post('WarehouseEmail')) {
237
            $warehouseEmail->loadDefaultValues();
0 ignored issues
show
Bug introduced by
The method loadDefaultValues cannot be called on $warehouseEmail (of type array|boolean).

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...
238
            $warehouseEmail->load(Yii::$app->request->post());
0 ignored issues
show
Bug introduced by
The method load cannot be called on $warehouseEmail (of type array|boolean).

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...
239
            if ($warehouseEmail->save()) {
0 ignored issues
show
Bug introduced by
The method save cannot be called on $warehouseEmail (of type array|boolean).

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...
240
                $this->redirect($returnUrl);
241
            }
242
        }
243
244
        return $this->render('form_edit_email', ['warehouseEmail' => $warehouseEmail]);
245
    }
246
247 View Code Duplication
    public function actionDeletePhone($id)
248
    {
249
        if (!$model = WarehousePhone::findOne($id)) {
250
            throw new NotFoundHttpException;
251
        }
252
253
        if (!$model->delete()) {
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $model (of type array|boolean).

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...
254
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object not removed'));
255
        } else {
256
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed'));
257
        }
258
259
        return $this->redirect(
260
            Yii::$app->request->get(
261
                'returnUrl',
262
                '/shop/backend-warehouse/index'
263
            )
264
        );
265
    }
266
267 View Code Duplication
    public function actionDeleteEmail($id)
268
    {
269
        if (!$model = WarehouseEmail::findOne($id)) {
270
            throw new NotFoundHttpException;
271
        }
272
273
        if (!$model->delete()) {
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $model (of type array|boolean).

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...
274
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object not removed'));
275
        } else {
276
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed'));
277
        }
278
279
        return $this->redirect(
280
            Yii::$app->request->get(
281
                'returnUrl',
282
                '/shop/backend-warehouse/index'
283
            )
284
        );
285
    }
286
287
288
    public function actionUpdateRemains()
289
    {
290
        $post = Yii::$app->request->post('remain', null);
291
        if (isset($post)) {
292
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
293
            $remainId = current(array_keys($post));
294
            /** @var WarehouseProduct $model */
295
            $model = WarehouseProduct::findOne($remainId);
296
            if ($model === null) {
297
                throw new NotFoundHttpException;
298
            }
299
300
            $model->setAttributes(current($post));
301
            TagDependency::invalidate(Yii::$app->cache,
302
                ActiveRecordHelper::getObjectTag(\app\modules\shop\models\Product::className(), $model->product_id));
303
            return $model->save();
304
305
306
        } else {
307
            throw new HttpException(400);
308
        }
309
    }
310
311
}
312