PostTypeController::findModel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace backend\controllers;
9
10
use common\components\Json;
11
use common\models\PostType;
12
use common\models\PostTypeTaxonomy;
13
use common\models\search\PostType as PostTypeSearch;
14
use common\models\Taxonomy;
15
use Yii;
16
use yii\filters\AccessControl;
17
use yii\filters\VerbFilter;
18
use yii\helpers\ArrayHelper;
19
use yii\web\Controller;
20
use yii\web\NotFoundHttpException;
21
22
/**
23
 * PostTypeController implements the CRUD actions for PostType model.
24
 *
25
 * @author Agiel K. Saputra <[email protected]>
26
 * @since 0.1.0
27
 */
28
class PostTypeController extends Controller
29
{
30
    /**
31
     * @inheritdoc
32
     */
33 View Code Duplication
    public function behaviors()
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...
34
    {
35
        return [
36
            'access' => [
37
                'class' => AccessControl::className(),
38
                'rules' => [
39
                    [
40
                        'actions' => ['index', 'view', 'create', 'update', 'delete', 'bulk-action'],
41
                        'allow' => true,
42
                        'roles' => ['administrator'],
43
                    ],
44
                ],
45
            ],
46
            'verbs' => [
47
                'class' => VerbFilter::className(),
48
                'actions' => [
49
                    'delete' => ['post'],
50
                    'bulk-action' => ['post'],
51
                ],
52
            ],
53
        ];
54
    }
55
56
    /**
57
     * Lists all PostType models.
58
     *
59
     * @return mixed
60
     */
61 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...
62
    {
63
        $searchModel = new PostTypeSearch();
64
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
65
66
        return $this->render('index', [
67
            'searchModel' => $searchModel,
68
            'dataProvider' => $dataProvider,
69
        ]);
70
    }
71
72
    /**
73
     * Displays a single PostType model.
74
     *
75
     * @param integer $id
76
     * @return mixed
77
     */
78
    public function actionView($id)
79
    {
80
        return $this->render('view', [
81
            'model' => $this->findModel($id),
82
        ]);
83
    }
84
85
    /**
86
     * Creates a new PostType and PostTypeTaxonomy model.
87
     * If creation is successful, the browser will be redirected to the 'view' page.
88
     *
89
     * @return mixed
90
     */
91
    public function actionCreate()
92
    {
93
        $model = new PostType();
94
        $taxonomies = ArrayHelper::map(Taxonomy::find()->all(), 'id', 'name');
95
96 View Code Duplication
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
97
            $postTypeTaxonomy = Yii::$app->request->post('PostTypeTaxonomy');
98
            if ($taxonomyIds = Json::decode($postTypeTaxonomy['taxonomyIds'])) {
99
                foreach ($taxonomyIds as $taxonomyId) {
100
                    $postTypeTaxonomy = new PostTypeTaxonomy();
101
                    $postTypeTaxonomy->setAttributes([
102
                        'post_type_id' => $model->id,
103
                        'taxonomy_id' => $taxonomyId,
104
                    ]);
105
                    $postTypeTaxonomy->save();
106
                }
107
            }
108
109
            return $this->redirect(['view', 'id' => $model->id]);
110
        }
111
112
        return $this->render('create', [
113
            'model' => $model,
114
            'taxonomy' => new Taxonomy(),
115
            'taxonomies' => $taxonomies,
116
        ]);
117
    }
118
119
    /**
120
     * Updates an existing PostType model.
121
     * If update is successful, the browser will be redirected to the 'view' page.
122
     *
123
     * @param integer $id
124
     * @return mixed
125
     */
126
    public function actionUpdate($id)
127
    {
128
        $model = $this->findModel($id);
129
        $taxonomies = ArrayHelper::map(Taxonomy::find()->all(), 'id', 'name');
130
131 View Code Duplication
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
0 ignored issues
show
Bug introduced by
The method load 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...
Bug introduced by
The method save 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...
Duplication introduced by
This code seems to be duplicated across 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...
132
            PostTypeTaxonomy::deleteAll(['post_type_id' => $id]);
133
            $postTypeTaxonomy = Yii::$app->request->post('PostTypeTaxonomy');
134
            if ($taxonomyIds = Json::decode($postTypeTaxonomy['taxonomyIds'])) {
135
                foreach ($taxonomyIds as $taxonomyId) {
136
                    $postTypeTaxonomy = new PostTypeTaxonomy();
137
                    $postTypeTaxonomy->setAttributes([
138
                        'post_type_id' => $model->id,
139
                        'taxonomy_id' => $taxonomyId,
140
                    ]);
141
                    $postTypeTaxonomy->save();
142
                }
143
            }
144
145
            return $this->redirect(['view', 'id' => $model->id]);
146
        }
147
148
        return $this->render('update', [
149
            'model' => $model,
150
            'taxonomy' => new Taxonomy(),
151
            'taxonomies' => $taxonomies,
152
        ]);
153
    }
154
155
    /**
156
     * Deletes an existing PostType model.
157
     * If deletion is successful, the browser will be redirected to the 'index' page.
158
     *
159
     * @param integer $id
160
     * @return mixed
161
     */
162
    public function actionDelete($id)
163
    {
164
        $this->findModel($id)->delete();
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $this->findModel($id) (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...
165
166
        return $this->redirect(['index']);
167
    }
168
169
    /**
170
     * Bulk action for PostType triggered when button 'Apply' clicked.
171
     * The action depends on the value of the dropdown next to the button.
172
     * Only accept POST HTTP method.
173
     */
174 View Code Duplication
    public function actionBulkAction()
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...
175
    {
176
        if (Yii::$app->request->post('action') === 'deleted') {
177
            foreach (Yii::$app->request->post('ids', []) as $id) {
178
                $this->findModel($id)->delete();
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $this->findModel($id) (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...
179
            }
180
        }
181
    }
182
183
    /**
184
     * Finds the PostType model based on its primary key value.
185
     * If the model is not found, a 404 HTTP exception will be thrown.
186
     *
187
     * @param integer $id
188
     * @return PostType the loaded model
189
     * @throws NotFoundHttpException if the model cannot be found
190
     */
191
    protected function findModel($id)
192
    {
193
        if (($model = PostType::findOne($id)) !== null) {
194
            return $model;
195
        }
196
197
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
198
    }
199
}
200