| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace app\controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use yii\data\Pagination; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use yii\helpers\ArrayHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use yii\web\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use yii\filters\{AccessControl, VerbFilter}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use app\models\{Category, Product}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class CategoryController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @package app\controllers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 17 |  | View Code Duplication | 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' => ['index','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 page with products. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param $alias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @throws NotFoundHttpException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function actionView($alias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $model = Category::find()->where([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             'alias' => $alias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         ])->andWhere([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             'active' => 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ])->one(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if (null === $model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             throw new NotFoundHttpException('Category not fount with alias = '.$alias.'.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->setMetaParams($model); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $productsQuery = Product::find()->where([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'categoryId' => $model->id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         ])->andWhere([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             'active' => 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $pagination = new Pagination([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             'totalCount' => $productsQuery->count(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             'defaultPageSize' => Yii::$app->params['defaultPageSize'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $this->render('view', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'model' => $model, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             'pagination' => $pagination, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             'products' => $productsQuery->offset($pagination->offset) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 ->limit($pagination->limit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 ->all() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                        
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.