UserController::actionCreate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 6
1
<?php
2
3
namespace zacksleo\yii2\backend\tests\controllers;
4
5
use Yii;
6
use yii\data\ActiveDataProvider;
7
use yii\web\Controller;
8
use yii\web\NotFoundHttpException;
9
use yii\filters\VerbFilter;
10
use yii\filters\AccessControl;
11
use zacksleo\yii2\backend\tests\models\User;
12
13
/**
14
 * UserController implements the CRUD actions for User model.
15
 */
16
class UserController extends Controller
17
{
18
    /**
19
     * Lists all User models.
20
     * @return ActiveDataProvider
21
     */
22
    public function actionIndex()
23
    {
24
        $params = Yii::$app->request->bodyParams;
25
        $userSearch = new \mdm\admin\models\searchs\User();
26
        return $userSearch->search($params);
27
    }
28
29
    /**
30
     * Displays a single User model.
31
     * @param integer $id
32
     * @return mixed
33
     */
34
    public function actionView($id)
35
    {
36
        return $this->findModel($id);
37
    }
38
39
    /**
40
     * Creates a new User model.
41
     * If creation is successful, the browser will be redirected to the 'view' page.
42
     * @return User
43
     */
44
    public function actionCreate()
45
    {
46
        $model = new User();
47
        $model->load(Yii::$app->request->bodyParams) && $model->save();
48
        return $model;
49
    }
50
51
    /**
52
     * Updates an existing User model.
53
     * If update is successful, the browser will be redirected to the 'view' page.
54
     * @param integer $id
55
     * @return mixed
56
     */
57
    public function actionUpdate($id)
58
    {
59
        $model = $this->findModel($id);
60
        return $model->load(Yii::$app->request->post()) && $model->save();
61
    }
62
63
    /**
64
     * Deletes an existing User model.
65
     * If deletion is successful, the browser will be redirected to the 'index' page.
66
     * @param integer $id
67
     * @return mixed
68
     */
69
    public function actionDelete($id)
70
    {
71
        return $this->findModel($id)->delete();
72
    }
73
74
    /**
75
     * Finds the User model based on its primary key value.
76
     * If the model is not found, a 404 HTTP exception will be thrown.
77
     * @param integer $id
78
     * @return User the loaded model
79
     * @throws NotFoundHttpException if the model cannot be found
80
     */
81
    protected function findModel($id)
82
    {
83
        if (($model = User::findOne($id)) !== null) {
0 ignored issues
show
Bug Compatibility introduced by
The expression \zacksleo\yii2\backend\t...els\User::findOne($id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 84 which is incompatible with the return type documented by zacksleo\yii2\backend\te...erController::findModel of type zacksleo\yii2\backend\tests\models\User.
Loading history...
84
            return $model;
85
        } else {
86
            throw new NotFoundHttpException('The requested page does not exist.');
87
        }
88
    }
89
}
90