FlowController::behaviors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace app\controllers;
4
5
use Yii;
6
use app\models\Flow;
7
use app\models\Content;
8
use app\models\ContentType;
9
use yii\data\ActiveDataProvider;
10
use yii\web\NotFoundHttpException;
11
use yii\helpers\ArrayHelper;
12
use yii\filters\VerbFilter;
13
use yii\filters\AccessControl;
14
15
/**
16
 * FlowController implements the CRUD actions for Flow model.
17
 */
18
class FlowController extends BaseController
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function behaviors()
24
    {
25
        return [
26
            'verbs' => [
27
                'class' => VerbFilter::class,
28
                'actions' => [
29
                    'delete' => ['POST'],
30
                ],
31
            ],
32
            'access' => [
33
                'class' => AccessControl::class,
34
                'only' => ['index', 'view', 'create', 'update', 'delete'],
35
                'rules' => [
36
                    ['allow' => true, 'actions' => ['index', 'view'], 'roles' => ['@']],
37
                    ['allow' => true, 'actions' => ['create', 'update', 'delete'], 'roles' => ['setFlows']],
38
                ],
39
            ],
40
        ];
41
    }
42
43
    /**
44
     * Lists all Flow models.
45
     *
46
     * @return string
47
     */
48
    public function actionIndex()
49
    {
50
        $query = Flow::availableQuery(Yii::$app->user);
51
        if ($query === null) {
52
            throw new \yii\web\ForbiddenHttpException(Yii::t('app', 'You do not have enough rights to view this flow.'));
53
        }
54
55
        $dataProvider = new ActiveDataProvider([
56
            'query' => $query,
57
        ]);
58
59
        return $this->render('index', [
60
            'dataProvider' => $dataProvider,
61
        ]);
62
    }
63
64
    /**
65
     * Displays a single Flow model.
66
     *
67
     * @param int $id
68
     *
69
     * @return string
70
     */
71
    public function actionView($id)
72
    {
73
        $model = $this->findModel($id);
74
        if (!$model->canView(Yii::$app->user)) {
75
            throw new \yii\web\ForbiddenHttpException(Yii::t('app', 'You do not have enough rights to view this flow.'));
76
        }
77
78
        $dataProvider = new ActiveDataProvider([
79
            'query' => Content::find()->joinWith(['type', 'flow'])->where([Flow::tableName() . '.id' => $id]),
80
        ]);
81
82
        $dataProvider->sort->attributes['type.name'] = [
83
            'asc' => [ContentType::tableName() . '.id' => SORT_ASC],
84
            'desc' => [ContentType::tableName() . '.id' => SORT_DESC],
85
        ];
86
87
        return $this->render('view', [
88
            'model' => $this->findModel($id),
89
            'dataProvider' => $dataProvider,
90
        ]);
91
    }
92
93
    /**
94
     * Creates a new Flow model.
95
     * If creation is successful, the browser will be redirected to the 'view' page.
96
     *
97
     * @return \yii\web\Response|string redirect or render
98
     */
99 View Code Duplication
    public function actionCreate()
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...
100
    {
101
        $model = new Flow();
102
103
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
104
            return $this->redirect(['view', 'id' => $model->id]);
105
        } else {
106
            $flows = ArrayHelper::map(Flow::find()->all(), 'id', 'name');
107
108
            return $this->render('create', [
109
                'model' => $model,
110
                'flows' => ['' => Yii::t('app', '(none)')] + $flows,
111
            ]);
112
        }
113
    }
114
115
    /**
116
     * Updates an existing Flow model.
117
     * If update is successful, the browser will be redirected to the 'view' page.
118
     *
119
     * @param int $id
120
     *
121
     * @return \yii\web\Response|string redirect or render
122
     */
123 View Code Duplication
    public function actionUpdate($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...
124
    {
125
        $model = $this->findModel($id);
126
127
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
128
            return $this->redirect(['view', 'id' => $model->id]);
129
        } else {
130
            $flows = ArrayHelper::map(Flow::find()->all(), 'id', 'name');
131
132
            return $this->render('update', [
133
                'model' => $model,
134
                'flows' => ['' => Yii::t('app', '(none)')] + $flows,
135
            ]);
136
        }
137
    }
138
139
    /**
140
     * Deletes an existing Flow model.
141
     * If deletion is successful, the browser will be redirected to the 'index' page.
142
     *
143
     * @param int $id
144
     *
145
     * @return \yii\web\Response
146
     */
147
    public function actionDelete($id)
148
    {
149
        $this->findModel($id)->delete();
150
151
        return $this->redirect(['index']);
152
    }
153
154
    /**
155
     * Finds the Flow model based on its primary key value.
156
     * If the model is not found, a 404 HTTP exception will be thrown.
157
     *
158
     * @param int $id
159
     *
160
     * @return Flow the loaded model
161
     *
162
     * @throws NotFoundHttpException if the model cannot be found
163
     */
164 View Code Duplication
    protected function findModel($id)
165
    {
166
        if (($model = Flow::findOne($id)) !== null) {
0 ignored issues
show
Bug Compatibility introduced by
The expression \app\models\Flow::findOne($id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 167 which is incompatible with the return type documented by app\controllers\FlowController::findModel of type app\models\Flow.
Loading history...
167
            return $model;
168
        } else {
169
            throw new NotFoundHttpException(Yii::t('app', 'The requested flow does not exist.'));
170
        }
171
    }
172
}
173