PostCommentController   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 216
Duplicated Lines 29.17 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 29
c 4
b 0
f 0
lcom 1
cbo 9
dl 63
loc 216
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 22 22 1
A actionIndex() 0 20 2
A actionDelete() 14 14 3
C actionBulkAction() 27 27 11
A actionReply() 0 18 3
A findModel() 0 8 2
A findPostType() 0 8 2
A findPost() 0 8 2
A actionUpdate() 0 15 3

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
 * @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\models\Post;
11
use common\models\PostComment;
12
use common\models\PostType;
13
use common\models\search\PostComment as PostCommentSearch;
14
use Yii;
15
use yii\filters\AccessControl;
16
use yii\filters\VerbFilter;
17
use yii\web\Controller;
18
use yii\web\NotFoundHttpException;
19
20
/**
21
 * PostCommentController, controlling the actions for PostComment model.
22
 *
23
 * @author Agiel K. Saputra <[email protected]>
24
 * @since 0.1.0
25
 */
26
class PostCommentController extends Controller
27
{
28
    /**
29
     * @inheritdoc
30
     */
31 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...
32
    {
33
        return [
34
            'access' => [
35
                'class' => AccessControl::className(),
36
                'rules' => [
37
                    [
38
                        'actions' => ['index', 'update', 'delete', 'bulk-action', 'reply'],
39
                        'allow' => true,
40
                        'roles' => ['editor'],
41
                    ],
42
                ],
43
            ],
44
            'verbs' => [
45
                'class' => VerbFilter::className(),
46
                'actions' => [
47
                    'delete' => ['post'],
48
                    'bulk-action' => ['post'],
49
                ],
50
            ],
51
        ];
52
    }
53
54
55
    /**
56
     * Lists all PostComment models on specific post type.
57
     * If there is post_id the action will generate list of all PostComment models based on post_id.
58
     *
59
     * @param integer $posttype Post type ID
60
     * @param null|integer $post Post ID
61
     * @throws \yii\web\NotFoundHttpException
62
     * @return string
63
     */
64
    public function actionIndex($posttype, $post = null)
65
    {
66
        $postId = null;
67
        $postType = $this->findPostType($posttype);
68
69
        if ($post) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $post of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
70
            $post = $this->findPost($post);
71
            $postId = $post->id;
72
        }
73
74
        $searchModel = new PostCommentSearch();
75
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $posttype, $postId);
76
77
        return $this->render('index', [
78
            'post' => $post,
79
            'postType' => $postType,
80
            'searchModel' => $searchModel,
81
            'dataProvider' => $dataProvider,
82
        ]);
83
    }
84
85
86
    /**
87
     * Updates an existing PostComment model.
88
     * If update is successful, the browser will be redirected to the 'view' page.
89
     *
90
     * @param integer $id
91
     * @return mixed
92
     */
93
    public function actionUpdate($id)
94
    {
95
        $model = $this->findModel($id);
96
97
        if ($model->load(Yii::$app->request->post())) {
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...
98
            $model->date = date('Y-m-d H:i:s', strtotime($model->date));
99
            if ($model->save()) {
0 ignored issues
show
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...
100
                return $this->redirect(['update', 'id' => $model->id]);
101
            }
102
        }
103
104
        return $this->render('update', [
105
            'model' => $model,
106
        ]);
107
    }
108
109
    /**
110
     * Deletes an existing PostComment model.
111
     * If deletion is successful, the browser will be redirected to the 'index' page.
112
     *
113
     * @param integer $id
114
     * @return mixed
115
     */
116 View Code Duplication
    public function actionDelete($id)
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...
117
    {
118
        $model = $this->findModel($id);
119
        $post = $model->commentPost;
120
121
        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...
122
            if (!$model->parent) {
123
                $post->updateAttributes(['comment_count', --$post->comment_count]);
124
            }
125
            PostComment::deleteAll(['parent' => $model->id]);
126
        }
127
128
        return $this->redirect(['index', 'posttype' => $post->type]);
129
    }
130
131
    /**
132
     * Bulk action for PostComment triggered when button 'Apply' clicked.
133
     * The action depends on the value of the dropdown next to the button.
134
     * Only accept POST HTTP method.
135
     */
136 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...
137
    {
138
        if (Yii::$app->request->post('action') === PostComment::STATUS_APPROVED) {
139
            foreach (Yii::$app->request->post('ids', []) as $id) {
140
                $this->findModel($id)->updateAttributes(['status' => PostComment::STATUS_APPROVED]);
0 ignored issues
show
Bug introduced by
The method updateAttributes 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...
141
            }
142
        } elseif (Yii::$app->request->post('action') === PostComment::STATUS_NOT_APPROVED) {
143
            foreach (Yii::$app->request->post('ids', []) as $id) {
144
                $this->findModel($id)->updateAttributes(['status' => PostComment::STATUS_NOT_APPROVED]);
0 ignored issues
show
Bug introduced by
The method updateAttributes 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...
145
            }
146
        } elseif (Yii::$app->request->post('action') === PostComment::STATUS_TRASHED) {
147
            foreach (Yii::$app->request->post('ids', []) as $id) {
148
                $this->findModel($id)->updateAttributes(['status' => PostComment::STATUS_TRASHED]);
0 ignored issues
show
Bug introduced by
The method updateAttributes 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...
149
            }
150
        } elseif (Yii::$app->request->post('action') === 'delete') {
151
            foreach (Yii::$app->request->post('ids', []) as $id) {
152
                $model = $this->findModel($id);
153
                $post = $model->commentPost;
154
                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...
155
                    if (!$model->parent) {
156
                        $post->updateAttributes(['comment_count', --$post->comment_count]);
157
                    }
158
                    PostComment::deleteAll(['parent' => $model->id]);
159
                }
160
            }
161
        }
162
    }
163
164
    /**
165
     * Reply an existing PostComment model.
166
     * If reply is successful, the browser will be redirected to 'update' page.
167
     *
168
     * @param int $id Find PostComment model based on id as parent.
169
     * @return string
170
     */
171
    public function actionReply($id)
172
    {
173
        $commentParent = $this->findModel($id);
174
        $model = new PostComment(['scenario' => 'reply']);
175
176
        if ($model->load(Yii::$app->request->post())) {
177
            $model->post_id = $commentParent->post_id;
178
            $model->parent = $commentParent->id;
179
            if ($model->save()) {
180
                $this->redirect(['post-comment/update', 'id' => $model->id]);
181
            }
182
        }
183
184
        return $this->render('reply', [
185
            'commentParent' => $commentParent,
186
            'model' => $model,
187
        ]);
188
    }
189
190
    /**
191
     * Finds the PostComment model based on its primary key value.
192
     * If the model is not found, a 404 HTTP exception will be thrown.
193
     *
194
     * @param integer $id
195
     * @return PostComment the loaded model
196
     * @throws NotFoundHttpException if the model cannot be found
197
     */
198
    protected function findModel($id)
199
    {
200
        if (($model = PostComment::findOne($id)) !== null) {
201
            return $model;
202
        }
203
204
        throw new NotFoundHttpException('The requested page does not exist.');
205
    }
206
207
    /**
208
     * Finds the PostType model based on its primary key value.
209
     * If the model is not found, a 404 HTTP exception will be thrown.
210
     *
211
     * @param integer $id
212
     * @return PostType the loaded model
213
     * @throws NotFoundHttpException if the model cannot be found
214
     */
215
    protected function findPostType($id)
216
    {
217
        if (($model = PostType::findOne($id)) !== null) {
218
            return $model;
219
        }
220
221
        throw new NotFoundHttpException('The requested page does not exist.');
222
    }
223
224
225
    /**
226
     * Finds the Post model based on its primary key value.
227
     * If the model is not found, a 404 HTTP exception will be thrown.
228
     *
229
     * @param integer $id
230
     * @return Post the loaded model
231
     * @throws NotFoundHttpException if the model cannot be found
232
     */
233
    protected function findPost($id)
234
    {
235
        if (($model = Post::findOne($id)) !== null) {
236
            return $model;
237
        }
238
239
        throw new NotFoundHttpException('The requested page does not exist.');
240
    }
241
}
242