UsersController::actionEdit()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7.4572

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
rs 9.2248
c 1
b 0
f 0
ccs 7
cts 13
cp 0.5385
cc 5
nc 6
nop 1
crap 7.4572
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use Yii;
6
use yii\web\NotFoundHttpException;
7
use app\controllers\ControllerTrait;
8
use app\models\entity\{User, UserProfile};
9
use app\modules\admin\models\forms\{UserForm, UserProfileForm};
10
use app\modules\admin\models\search\UserSearch;
11
12
class UsersController extends \yii\web\Controller
13
{
14
    use ControllerTrait;
15
16
    public function behaviors()
17
    {
18 6
        return [
19
            'verbs' => [
20
                'class' => 'yii\filters\VerbFilter',
21 6
                'actions' => [
22
                    'set-active' => ['post'],
23
                    'set-block' => ['post'],
24
                    'delete' => ['post'],
25
                    'batch' => ['post'],
26
                    'photo-upload' => ['post'],
27
                    'autocomplete' => ['post'],
28
                ],
29
            ],
30
        ];
31
    }
32
33
    public function actions()
34 6
    {
35
        return [
36
            'batch' => [
37 6
                'class' => 'app\modules\admin\actions\BatchAction',
38
                'modelClass' => User::class,
39
                'actions' => [
40
                    'delete' => [],
41
                    'set-active' => ['status' => User::STATUS_ACTIVE],
42
                    'set-block' => ['status' => User::STATUS_BLOCKED]
43
                ]
44
            ],
45
            'set-active' => [
46
                'class' => 'app\modules\admin\actions\UpdateAttributesAction',
47
                'modelClass' => User::class,
48
                'attributes' => ['status' => User::STATUS_ACTIVE],
49
            ],
50
            'set-block' => [
51
                'class' => 'app\modules\admin\actions\UpdateAttributesAction',
52
                'modelClass' => User::class,
53
                'attributes' => ['status' => User::STATUS_BLOCKED],
54
            ],
55
            'delete' => [
56
                'class' => 'app\modules\admin\actions\DeleteAction',
57
                'modelClass' => User::class,
58
            ],
59
            'photo-upload' => [
60
                'class'     => 'rkit\filemanager\actions\UploadAction',
61
                'modelClass' => UserProfile::class,
62
                'attribute' => 'photo',
63
                'inputName' => 'file',
64
            ],
65
        ];
66
    }
67
68
    public function actionIndex()
69 6
    {
70
        $search = new UserSearch();
71 6
        $provider = $search->search(Yii::$app->request->get());
72 6
        $statuses = User::getStatuses();
73 6
74
        return $this->render('index', [
75 6
            'search' => $search,
76 6
            'provider' => $provider,
77 6
            'statuses' => $statuses,
78 6
            'roles' => Yii::$app->authManager->getRoles()
79 6
        ]);
80
    }
81
82
    public function actionEdit($id = null)
83 3
    {
84
        $model = new UserForm();
85 3
86
        if ($id) {
87 3
            $model->setModel($this->findUser($id));
0 ignored issues
show
Compatibility introduced by
$this->findUser($id) of type object<yii\base\Model> is not a sub-type of object<app\models\entity\User>. It seems like you assume a child class of the class yii\base\Model to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
88 2
        }
89
90
        if (Yii::$app->request->isPost) {
91 2
            Yii::$app->response->format = 'json';
92
93
            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
94
                $model->save();
95
96
                Yii::$app->session->setFlash('success', Yii::t('app', 'Saved successfully'));
97
                return $this->redirect(['edit', 'id' => $model->id]);
98
            }
99
            return $this->asJsonModelErrors($model);
0 ignored issues
show
Documentation introduced by
$model is of type object<app\modules\admin\models\forms\UserForm>, but the function expects a object<app\controllers\y...ollers\yii\base\Model>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
        }
101
102
        return $this->render('edit', [
103 2
            'model' => $model,
104 2
        ]);
105
    }
106
107
    public function actionProfile($id)
108 2
    {
109
        $model = new UserProfileForm($this->findProfile($id));
0 ignored issues
show
Compatibility introduced by
$this->findProfile($id) of type object<yii\base\Model> is not a sub-type of object<app\models\entity\UserProfile>. It seems like you assume a child class of the class yii\base\Model to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
110 2
111 2
        if (Yii::$app->request->isPost) {
112
            Yii::$app->response->format = 'json';
113 1
114
            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
115
                $model->save();
116
117
                Yii::$app->session->setFlash('success', Yii::t('app', 'Saved successfully'));
118
                return $this->redirect(['profile', 'id' => $model->user_id]);
119
            }
120
            return $this->asJsonModelErrors($model);
0 ignored issues
show
Documentation introduced by
$model is of type object<app\modules\admin...\forms\UserProfileForm>, but the function expects a object<app\controllers\y...ollers\yii\base\Model>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
        }
122
123
        return $this->render('profile', [
124
            'model' => $model
125 1
        ]);
126 1
    }
127
128
    /**
129
     * Find the model.
130
     * If the model is not found, then 404 HTTP exception will be thrown.
131
     *
132
     * @param int $id
133
     * @return Model
134
     * @throws NotFoundHttpException
135
     */
136
    private function findUser($id): yii\base\Model
137
    {
138
        $model = User::findOne($id);
139
140
        if ($model === null) {
141
            throw new NotFoundHttpException(Yii::t('app', 'Page not found'));
142
        }
143
144
        return $model;
145
    }
146
147
    /**
148
     * Find the model.
149
     * If the model is not found, then 404 HTTP exception will be thrown.
150
     *
151
     * @param int $id
152
     * @return Model
153
     * @throws NotFoundHttpException
154
     */
155
    private function findProfile($id): yii\base\Model
156
    {
157
        $model = UserProfile::findOne($id);
158
159
        if ($model === null) {
160
            throw new NotFoundHttpException(Yii::t('app', 'Page not found'));
161
        }
162
163
        return $model;
164
    }
165
}
166