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 — action_getCatTree_order ( 77944b )
by
unknown
23:56
created

BackendReviewController::actionUpdateStatus()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 33
rs 8.439
cc 5
eloc 24
nc 4
nop 1
1
<?php
2
3
namespace app\modules\review\controllers;
4
5
use app\models\Object;
6
use app\modules\page\models\Page;
7
use app\modules\review\models\Review;
8
use app\modules\shop\models\Category;
9
use app\modules\shop\models\Product;
10
use yii\db\ActiveRecord;
11
use yii\filters\AccessControl;
12
use yii\helpers\Url;
13
use yii\web\BadRequestHttpException;
14
use Yii;
15
use yii\web\NotFoundHttpException;
16
use app\components\SearchModel;
17
use app\models\Submission;
18
use yii\web\Response;
19
20
class BackendReviewController extends \app\backend\components\BackendController
21
{
22
    public function behaviors()
23
    {
24
        return [
25
            'access' => [
26
                'class' => AccessControl::className(),
27
                'rules' => [
28
                    [
29
                        'allow' => true,
30
                        'roles' => ['review manage'],
31
                    ],
32
                ],
33
            ],
34
        ];
35
    }
36
37
    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...
38
    {
39
        $searchModelConfig = [
40
            'defaultOrder' => ['id' => SORT_DESC],
41
            'model' => Review::className(),
42
            'relations' => [
43
                'submission.form' => ['name'],
44
            ],
45
            'additionalConditions' => [
46
                ['parent_id' => 0],
47
            ]
48
        ];
49
        $searchModel = new SearchModel($searchModelConfig);
50
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
51
        return $this->render(
52
            'index',
53
            [
54
                'dataProvider' => $dataProvider,
55
                'searchModel' => $searchModel,
56
            ]
57
        );
58
    }
59
60
    public function actionView($id)
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...
61
    {
62
        $model = Review::find()
63
            ->with(['submission'])
64
            ->where(['id' => $id])
65
            ->one();
66
        if (null === $model) {
67
            throw new NotFoundHttpException;
68
        }
69
70 View Code Duplication
        if (true === Yii::$app->request->isPost) {
71
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
72
                return $this->redirect(Url::toRoute(['view', 'id' => $model->id]));
73
            }
74
        }
75
76
        return $this->render('edit', [
77
            'review' => $model
78
        ]);
79
    }
80
81
    public function actionUpdateStatus($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...
82
    {
83
        if (null === $id) {
84
            $id = \Yii::$app->request->post('editableKey');
85
            $index = \Yii::$app->request->post('editableIndex');
86
            if (null === $id || null === $index) {
87
                throw new BadRequestHttpException;
88
            } else {
89
                $review = $this->loadModel($id);
90
                $reviews = \Yii::$app->request->post('Review', []);
91
                $review->status = $reviews[$index]['status'];
92
                return $review->update();
0 ignored issues
show
Bug introduced by
The method update cannot be called on $review (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...
93
            }
94
        } else {
95
            $reviews = $reviews = \Yii::$app->request->post('Review');
96
            $status = $reviews['status'];
97
            $review = $this->loadModel($id);
98
            $review->status = $status;
99
            if ($review->update()) {
0 ignored issues
show
Bug introduced by
The method update cannot be called on $review (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...
100
                Yii::$app->session->setFlash('success', Yii::t('app', 'Review successfully updated'));
101
            } else {
102
                Yii::$app->session->setFlash('error', Yii::t('app', 'Error occurred while updating review'));
103
            }
104
            return $this->redirect(
105
                Url::toRoute(
106
                    [
107
                        'view',
108
                        'id' => $review->id
109
                    ]
110
                )
111
            );
112
        }
113
    }
114
115
    public function actionMarkSpam($id, $spam = 1)
116
    {
117
        if ($spam === 1) {
118
            $message = Yii::t('app', 'Entry successfully marked as spam');
119
        } else {
120
            $message = Yii::t('app', 'Entry successfully marked as not spam');
121
        }
122
        /** @var Submission $submission */
123
        $submission = Submission::findOne($id);
124
        if (is_null($submission)) {
125
            throw new NotFoundHttpException;
126
        }
127
        $submission->spam = $spam;
128
        if ($spam == 1) {
129
            /** @var Review $review */
130
            $review = Review::findOne(['submission_id' => $id]);
131
            if (!is_null($review)) {
132
                $review->status = Review::STATUS_NOT_APPROVED;
133
                $review->save(true, ['status']);
134
            }
135
        }
136
        if ($submission->save(true, ['spam'])) {
137
            Yii::$app->session->setFlash('success', $message);
138
        }
139
        return $this->redirect(
140
            Url::toRoute(
141
                [
142
                    'view',
143
                    'id' => $id
144
                ]
145
            )
146
        );
147
    }
148
149
    /**
150
     * @param $id
151
     * @param null $returnUrl
152
     * @return Response
153
     * @throws NotFoundHttpException
154
     * @throws \Exception
155
     */
156
    public function actionDelete($id, $returnUrl = null)
157
    {
158
        $model = $this->loadModel($id);
159
        $parent_id = $model->parent_id;
160
        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...
161
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed'));
162
        }
163
164
        $returnUrl = !empty($returnUrl)
165
            ? $returnUrl
166
            : (0 === intval($parent_id) ? Url::toRoute(['index']) : Url::toRoute(['view', 'id' => $parent_id]));
167
        return $this->redirect($returnUrl);
168
    }
169
170
    /**
171
     * @param array $returnUrl
172
     * @return Response
173
     * @throws \Exception
174
     */
175
    public function actionRemoveAll($returnUrl = ['index'])
176
    {
177
        $items = Yii::$app->request->post('items', []);
178
        if (!empty($items)) {
179
            $items = Review::findAll(['id' => $items]);
180
            foreach ($items as $item) {
181
                $item->delete();
182
            }
183
            Yii::$app->session->setFlash('info', Yii::t('app', 'Objects removed'));
184
        }
185
        return $this->redirect($returnUrl);
186
    }
187
188
    /**
189
     * Load review model by id
190
     * @param $id
191
     * @return Review
0 ignored issues
show
Documentation introduced by
Should the return type not be array|boolean? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
192
     * @throws NotFoundHttpException
193
     */
194
    protected function loadModel($id)
195
    {
196
        $model = Review::findOne($id);
197
        if (is_null($model)) {
198
            throw new NotFoundHttpException;
199
        }
200
        return $model;
201
    }
202
203
    public function actionCreate($parent_id = 0)
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...
204
    {
205
        $parent_id = intval($parent_id);
206
        $model = new Review();
207
        $model->loadDefaultValues();
208
209
        if (0 === $parent_id) {
210
            $model->parent_id = $parent_id;
211
            $model->submission_id = 0;
212
            $model->object_model_id = 0;
213
            $model->object_id = 0;
214
        } elseif (null !== $parent = Review::findOne(['id' => $parent_id])) {
215
            /** @var Review $parent */
216
            $model->parent_id = $parent_id;
217
            $model->object_id = $parent->object_id;
218
            $model->object_model_id = $parent->object_model_id;
219
            $model->root_id = $parent->root_id;
220
            $model->submission_id = $parent->submission_id;
221
        }
222
223 View Code Duplication
        if (true === Yii::$app->request->isPost) {
224
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
225
                return $this->redirect(Url::toRoute(['view', 'id' => $model->id]));
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<app\modules\review\models\Review>. 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...
226
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
227
                // @todo add alert and may be something else here
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
228
            }
229
        }
230
231
        return $this->render('edit', [
232
            'review' => $model
233
        ]);
234
    }
235
236
    /**
237
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,false|array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
238
     */
239
    public function actionAjaxSearch()
240
    {
241
        Yii::$app->response->format = Response::FORMAT_JSON;
242
        $search = \Yii::$app->request->get('search', []);
243
        $object = !empty($search['object']) ?  intval($search['object']) : 0;
244
        $term = !empty($search['term']) ?  $search['term'] : '';
245
246
        $result = [
247
            'more' => false,
248
            'results' => []
249
        ];
250
251
        if (null === $object = Object::findById($object)) {
252
            return $result;
253
        }
254
255
        /** @var ActiveRecord $class */
256
        $class = $object->object_class;
257
        $list = [
258
            Product::className(),
259
            Category::className(),
260
            Page::className(),
261
        ];
262
        if (!in_array($class, $list)) {
263
            return $result;
264
        }
265
266
        $query = $class::find()
267
            ->select('id, name, "#" `url`')
268
            ->andWhere(['like', 'name', $term])
269
            ->asArray(true);
270
        $result['results'] = array_values($query->all());
271
        array_walk($result['results'],
272
            function (&$val) use ($class)
273
            {
274
                if (null !== $model = $class::findOne(['id' => $val['id']])) {
275
                    if (Product::className() === $model->className()) {
0 ignored issues
show
Bug introduced by
The method className 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...
276
                        $val['url'] = Url::toRoute([
277
                            '@product',
278
                            'model' => $model,
279
                            'category_group_id' => $model->category->category_group_id,
280
                        ]);
281
                    } elseif (Category::className() === $model->className()) {
0 ignored issues
show
Bug introduced by
The method className 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...
282
                        $val['url'] = Url::toRoute([
283
                            '@category',
284
                            'last_category_id' => $model->id,
285
                            'category_group_id' => $model->category_group_id,
286
                        ]);
287
                    } else if (Page::className() === $model->className()) {
0 ignored issues
show
Bug introduced by
The method className 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...
288
                        $val['url'] = Url::toRoute([
289
                            '@article',
290
                            'id' => $model->id,
291
                        ]);
292
                    }
293
                }
294
            });
295
296
        return $result;
297
    }
298
299
    /**
300
     * @return array
301
     */
302
    public function actionAjaxGetTree($root_id = null, $current_id = 0)
303
    {
304
        Yii::$app->response->format = Response::FORMAT_JSON;
305
306
        $q = Review::find()
307
            ->select('*')
308
            ->where(['root_id' => $root_id])
309
            ->orderBy(['parent_id' => SORT_ASC])
310
            ->asArray(true);
311
312
        $result = array_reduce($q->all(),
313
            function ($res, $item) use ($current_id)
314
            {
315
                $res[] = [
316
                    'id' => $item['id'],
317
                    'parent' => 0 === intval($item['parent_id']) ? '#' : $item['parent_id'],
318
                    'text' => $item['id'],
319
                    'type' => intval($item['id']) === intval($current_id) ? 'current' : 'leaf',
320
                    'a_attr' => [
321
                        'data-id' => $item['id'],
322
                    ]
323
                ];
324
                return $res;
325
            }, []);
326
327
        return $result;
328
    }
329
}
330