ManageController::findModel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace yii2mod\comments\controllers;
4
5
use Yii;
6
use yii\web\Controller;
7
use yii\web\NotFoundHttpException;
8
use yii2mod\comments\models\CommentModel;
9
use yii2mod\comments\traits\ModuleTrait;
10
11
/**
12
 * Class ManageController
13
 *
14
 * @package yii2mod\comments\controllers
15
 */
16
class ManageController extends Controller
17
{
18
    use ModuleTrait;
19
20
    /**
21
     * @var string path to index view file, which is used in admin panel
22
     */
23
    public $indexView = '@vendor/yii2mod/yii2-comments/views/manage/index';
24
25
    /**
26
     * @var string path to update view file, which is used in admin panel
27
     */
28
    public $updateView = '@vendor/yii2mod/yii2-comments/views/manage/update';
29
30
    /**
31
     * @var string search class name for searching
32
     */
33
    public $searchClass = 'yii2mod\comments\models\search\CommentSearch';
34
35
    /**
36
     * @var array verb filter config
37
     */
38
    public $verbFilterConfig = [
39
        'class' => 'yii\filters\VerbFilter',
40
        'actions' => [
41
            'index' => ['get'],
42
            'update' => ['get', 'post'],
43
            'delete' => ['post'],
44
        ],
45
    ];
46
47
    /**
48
     * @var array access control config
49
     */
50
    public $accessControlConfig = [
51
        'class' => 'yii\filters\AccessControl',
52
        'rules' => [
53
            [
54
                'allow' => true,
55
                'roles' => ['admin'],
56
            ],
57
        ],
58
    ];
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function behaviors()
64
    {
65
        return [
66
            'verbs' => $this->verbFilterConfig,
67
            'access' => $this->accessControlConfig,
68
        ];
69
    }
70
71
    /**
72
     * Lists all comments.
73
     *
74
     * @return mixed
75
     */
76
    public function actionIndex()
77
    {
78
        $searchModel = Yii::createObject($this->searchClass);
79
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
80
        $commentModel = $this->getModule()->commentModelClass;
81
82
        return $this->render($this->indexView, [
83
            'dataProvider' => $dataProvider,
84
            'searchModel' => $searchModel,
85
            'commentModel' => $commentModel,
86
        ]);
87
    }
88
89
    /**
90
     * Updates an existing CommentModel.
91
     *
92
     * If update is successful, the browser will be redirected to the 'index' page.
93
     *
94
     * @param int $id
95
     *
96
     * @return mixed
97
     */
98
    public function actionUpdate($id)
99
    {
100
        $model = $this->findModel($id);
101
102
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
103
            Yii::$app->session->setFlash('success', Yii::t('yii2mod.comments', 'Comment has been saved.'));
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

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

103
            Yii::$app->session->/** @scrutinizer ignore-call */ 
104
                                setFlash('success', Yii::t('yii2mod.comments', 'Comment has been saved.'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
105
            return $this->redirect(['index']);
106
        }
107
108
        return $this->render($this->updateView, [
109
            'model' => $model,
110
        ]);
111
    }
112
113
    /**
114
     * Deletes an existing comment with children.
115
     *
116
     * If deletion is successful, the browser will be redirected to the 'index' page.
117
     *
118
     * @param int $id
119
     *
120
     * @return mixed
121
     */
122
    public function actionDelete($id)
123
    {
124
        $this->findModel($id)->deleteWithChildren();
125
        Yii::$app->session->setFlash('success', Yii::t('yii2mod.comments', 'Comment has been deleted.'));
126
127
        return $this->redirect(['index']);
128
    }
129
130
    /**
131
     * Finds the CommentModel based on its primary key value.
132
     *
133
     * If the model is not found, a 404 HTTP exception will be thrown.
134
     *
135
     * @param int $id
136
     *
137
     * @throws NotFoundHttpException if the model cannot be found
138
     *
139
     * @return CommentModel
140
     */
141
    protected function findModel($id)
142
    {
143
        $commentModel = $this->getModule()->commentModelClass;
144
145
        if (null !== ($model = $commentModel::findOne($id))) {
146
            return $model;
147
        } else {
148
            throw new NotFoundHttpException(Yii::t('yii2mod.comments', 'The requested page does not exist.'));
149
        }
150
    }
151
}
152