Passed
Push — master ( dd4450...9a9614 )
by Andrey
05:55
created

CategoryController::actionView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 21
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 31
rs 9.584
1
<?php
2
3
namespace app\controllers;
4
5
use Yii;
6
use yii\data\Pagination;
7
use yii\web\NotFoundHttpException;
8
use yii\filters\{AccessControl, VerbFilter};
9
use yii\helpers\ArrayHelper;
10
use app\models\{Category, Product};
11
12
/**
13
 * Class CategoryController
14
 *
15
 * @package app\controllers
16
 */
17
class CategoryController extends BaseController
18
{
19
    /**
20
     * @return array
21
     */
22
    public function behaviors()
23
    {
24
        return ArrayHelper::merge(parent::behaviors(), [
25
            'access' => [
26
                'class' => AccessControl::class,
27
                'rules' => [
28
                    [
29
                        'allow' => true,
30
                        'actions' => ['view'],
31
                        'roles' => ['?', '@'],
32
                    ],
33
                ],
34
            ],
35
            'verbs' => [
36
                'class' => VerbFilter::class,
37
                'actions' => [
38
                    'view' => ['get'],
39
                ],
40
            ],
41
        ]);
42
    }
43
44
    /**
45
     * Displays Category.
46
     *
47
     * @param $alias
48
     * @return string
49
     *
50
     * @throws NotFoundHttpException
51
     */
52
    public function actionView($alias)
53
    {
54
        $model = Category::find()->where([
55
            'alias' => $alias
56
        ])->andWhere([
57
            'active' => 1
58
        ])->one();
59
60
        if (null === $model) {
61
            throw new NotFoundHttpException('Category not fount with alias = '.$alias.'.');
62
        }
63
64
        $this->setMetaParams($model);
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type array; however, parameter $model of app\controllers\BaseController::setMetaParams() does only seem to accept null|yii\db\ActiveRecord, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        $this->setMetaParams(/** @scrutinizer ignore-type */ $model);
Loading history...
65
66
        $productsQuery = Product::find()->where([
67
            'categoryId' => $model->id
68
        ])->andWhere([
69
            'active' => 1
70
        ]);
71
72
        $pagination = new Pagination([
73
            'totalCount' => $productsQuery->count(),
74
            'defaultPageSize' => Yii::$app->params['defaultPageSize']
75
        ]);
76
77
        return $this->render('view', [
78
            'model' => $model,
79
            'pagination' => $pagination,
80
            'products' => $productsQuery->offset($pagination->offset)
81
                ->limit($pagination->limit)
82
                ->all()
83
        ]);
84
    }
85
}
86