MediaCommentController   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 194
Duplicated Lines 21.13 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 27
c 4
b 0
f 0
lcom 1
cbo 8
dl 41
loc 194
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 0 18 2
A actionDelete() 14 14 3
C actionBulkAction() 27 27 11
A actionReply() 0 20 3
A findModel() 0 8 2
A findMedia() 0 8 2
A behaviors() 0 22 1
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\Media;
11
use common\models\MediaComment;
12
use common\models\search\MediaComment as MediaCommentSearch;
13
use Yii;
14
use yii\filters\AccessControl;
15
use yii\filters\VerbFilter;
16
use yii\web\Controller;
17
use yii\web\NotFoundHttpException;
18
19
/**
20
 * Class MediaCommentController, controlling the actions for Media model.
21
 *
22
 * @author Agiel K. Saputra <[email protected]>
23
 * @since 0.1.0
24
 */
25
class MediaCommentController extends Controller
26
{
27
    /**
28
     * @inheritdoc
29
     */
30
    public function behaviors()
31
    {
32
        return [
33
            'access' => [
34
                'class' => AccessControl::className(),
35
                'rules' => [
36
                    [
37
                        'actions' => ['index', 'update', 'delete', 'bulk-action', 'reply'],
38
                        'allow' => true,
39
                        'roles' => ['editor'],
40
                    ],
41
                ],
42
            ],
43
            'verbs' => [
44
                'class' => VerbFilter::className(),
45
                'actions' => [
46
                    'delete' => ['post'],
47
                    'bulk-action' => ['post'],
48
                ],
49
            ],
50
        ];
51
    }
52
53
    /**
54
     * Lists all MediaComment models.
55
     * If there is media, the action will generate list of all MediaComment models based on Media ID.
56
     *
57
     * @param null|integer $media Media ID
58
     * @return mixed
59
     */
60
    public function actionIndex($media = null)
61
    {
62
        $mediaId = null;
63
64
        if ($media) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $media 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...
65
            $media = $this->findMedia($media);
66
            $mediaId = $media->id;
67
        }
68
69
        $searchModel = new MediaCommentSearch();
70
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $mediaId);
71
72
        return $this->render('index', [
73
            'searchModel' => $searchModel,
74
            'dataProvider' => $dataProvider,
75
            'media' => $media,
76
        ]);
77
    }
78
79
    /**
80
     * Updates an existing MediaComment model.
81
     * If update is successful, the browser will be redirected to the 'update' page.
82
     *
83
     * @param integer $id
84
     * @return mixed
85
     */
86
    public function actionUpdate($id)
87
    {
88
        $model = $this->findModel($id);
89
90
        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...
91
            $model->date = date('Y-m-d H:i:s', strtotime($model->date));
92
            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...
93
                return $this->redirect(['update', 'id' => $model->id]);
94
            }
95
        }
96
97
        return $this->render('update', [
98
            'model' => $model,
99
        ]);
100
    }
101
102
    /**
103
     * Deletes an existing MediaComment model.
104
     * If deletion is successful, the browser will be redirected to the 'index' page.
105
     *
106
     * @param integer $id
107
     * @return mixed
108
     */
109 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...
110
    {
111
        $model = $this->findModel($id);
112
        $media = $model->commentMedia;
113
114
        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...
115
            if (!$model->parent) {
116
                $media->updateAttributes(['comment_count', --$media->comment_count]);
117
            }
118
            MediaComment::deleteAll(['parent' => $model->id]);
119
        }
120
121
        return $this->redirect(['index']);
122
    }
123
124
    /**
125
     * Bulk action for MediaComment triggered when button 'Apply' clicked.
126
     * The action depends on the value of the dropdown next to the button.
127
     * Only accept POST HTTP method.
128
     */
129 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...
130
    {
131
        if (Yii::$app->request->post('action') === MediaComment::STATUS_APPROVED) {
132
            foreach (Yii::$app->request->post('ids', []) as $id) {
133
                $this->findModel($id)->updateAttributes(['status' => MediaComment::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...
134
            }
135
        } elseif (Yii::$app->request->post('action') === MediaComment::STATUS_NOT_APPROVED) {
136
            foreach (Yii::$app->request->post('ids', []) as $id) {
137
                $this->findModel($id)->updateAttributes(['status' => MediaComment::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...
138
            }
139
        } elseif (Yii::$app->request->post('action') === MediaComment::STATUS_TRASHED) {
140
            foreach (Yii::$app->request->post('ids', []) as $id) {
141
                $this->findModel($id)->updateAttributes(['status' => MediaComment::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...
142
            }
143
        } elseif (Yii::$app->request->post('action') === 'delete') {
144
            foreach (Yii::$app->request->post('ids', []) as $id) {
145
                $model = $this->findModel($id);
146
                $media = $model->commentMedia;
147
                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...
148
                    if (!$model->parent) {
149
                        $media->updateAttributes(['comment_count', --$media->comment_count]);
150
                    }
151
                    MediaComment::deleteAll(['parent' => $model->id]);
152
                }
153
            }
154
        }
155
    }
156
157
    /**
158
     * Reply an existing MediaComment model.
159
     * If reply is successful, the browser will be redirected to 'update' page.
160
     *
161
     * @param int $id Find MediaComment model based on id as its parent
162
     * @return string
163
     */
164
    public function actionReply($id)
165
    {
166
        $commentParent = $this->findModel($id);
167
        $model = new MediaComment(['scenario' => 'reply']);
168
169
        if ($model->load(Yii::$app->request->post())) {
170
            $model->setAttributes([
171
                'media_id' => $commentParent->media_id,
172
                'parent' => $commentParent->id,
173
            ]);
174
            if ($model->save()) {
175
                $this->redirect(['/media-comment/update', 'id' => $model->id]);
176
            }
177
        }
178
179
        return $this->render('reply', [
180
            'commentParent' => $commentParent,
181
            'model' => $model,
182
        ]);
183
    }
184
185
    /**
186
     * Finds the MediaComment model based on its primary key value.
187
     * If the model is not found, a 404 HTTP exception will be thrown.
188
     *
189
     * @param integer $id
190
     * @return MediaComment the loaded model
191
     * @throws NotFoundHttpException if the model cannot be found
192
     */
193
    protected function findModel($id)
194
    {
195
        if (($model = MediaComment::findOne($id)) !== null) {
196
            return $model;
197
        }
198
199
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
200
    }
201
202
    /**
203
     * Finds the Media model based on its primary key value.
204
     * If the model is not found, a 404 HTTP exception will be thrown.
205
     *
206
     * @param integer $id
207
     * @return Media the loaded model
208
     * @throws NotFoundHttpException if the model cannot be found
209
     */
210
    protected function findMedia($id)
211
    {
212
        if (($model = Media::findOne($id)) !== null) {
213
            return $model;
214
        }
215
216
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
217
    }
218
}
219