Completed
Push — master ( 2e8683...7e20fc )
by Igor
07:20
created

UsersController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
crap 1
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use Yii;
6
use yii\filters\VerbFilter;
7
use yii\helpers\Url;
8
use yii\web\Response;
9
use app\traits\ModelTrait;
10
use app\models\User;
11
use app\models\UserProfile;
12
use app\modules\admin\models\search\UserSearch;
13
14
class UsersController extends \yii\web\Controller
15
{
16
    use ModelTrait;
17
18 33
    public function behaviors()
19
    {
20
        return [
21 33
            'verbs' => [
22
                'class' => VerbFilter::class,
23
                'actions' => [
24
                    'set-active' => ['post'],
25
                    'set-block' => ['post'],
26
                    'delete' => ['post'],
27
                    'operations' => ['post'],
28
                    'photo-upload' => ['post'],
29
                    'autocomplete' => ['post'],
30
                ],
31
            ],
32
        ];
33
    }
34
35 33
    public function actions()
36
    {
37
        return [
38 33
            'operations' => [
39 33
                'class' => 'app\modules\admin\controllers\common\OperationsAction',
40 33
                'modelClass' => 'app\models\User',
41
                'operations' => [
42
                    'delete' => [],
43 33
                    'set-active' => ['status' => User::STATUS_ACTIVE],
44 33
                    'set-block' => ['status' => User::STATUS_BLOCKED]
45
                ]
46
            ],
47
            'set-active' => [
48 33
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
49 33
                'modelClass' => 'app\models\User',
50 33
                'attributes' => ['status' => User::STATUS_ACTIVE],
51
            ],
52
            'set-block' => [
53 33
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
54 33
                'modelClass' => 'app\models\User',
55 33
                'attributes' => ['status' => User::STATUS_BLOCKED],
56
            ],
57
            'delete' => [
58
                'class' => 'app\modules\admin\controllers\common\DeleteAction',
59
                'modelClass' => 'app\models\User',
60
            ],
61
            'photo-upload' => [
62
                'class'     => 'rkit\filemanager\actions\UploadAction',
63
                'modelClass' => 'app\models\UserProfile',
64
                'attribute' => 'photo',
65
                'inputName' => 'file',
66
            ],
67
        ];
68
    }
69
70 33
    public function actionIndex()
71
    {
72 33
        $userSearch = new UserSearch();
73 33
        $dataProvider = $userSearch->search(Yii::$app->request->get());
74 33
        $statuses = User::getStatuses();
75
76 33
        return $this->render('index', [
77 33
            'userSearch' => $userSearch,
78 33
            'dataProvider' => $dataProvider,
79 33
            'statuses' => $statuses,
80 33
            'roles' => Yii::$app->authManager->getRoles()
81
        ]);
82
    }
83
84
    public function actionEdit($id = null)
85
    {
86
        $model = new User();
87
88
        if ($id) {
89
            $model = $this->findModel($model, $id);
0 ignored issues
show
Documentation introduced by
$model is of type object<app\models\User>, but the function expects a object<app\traits\ActiveRecord>.

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...
90
        }
91
92
        if (Yii::$app->request->isPost) {
93
            if ($model->isNewRecord) {
94
                $model->setConfirmed();
95
            }
96
97
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
98
                $this->assignRole($model);
99
100
                Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully'));
101
                $urlToModel = Url::toRoute(['edit', 'id' => $model->id]);
102
                if (Yii::$app->request->isAjax) {
103
                    return $this->asJson(['redirect' => $urlToModel]);
104
                }
105
                return $this->redirect($urlToModel);
106
            }
107
            if (Yii::$app->request->isAjax) {
108
                return $this->asJson($this->collectErrors($model));
0 ignored issues
show
Bug introduced by
It seems like $model defined by new \app\models\User() on line 86 can also be of type object<app\models\User>; however, app\traits\ModelTrait::collectErrors() does only seem to accept object<app\traits\Model>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
109
            }
110
        }
111
112
        return $this->render('edit', [
113
            'model' => $model,
114
            'roles' => Yii::$app->authManager->getRoles()
115
        ]);
116
    }
117
118
    public function actionProfile($id)
119
    {
120
        $model = $this->findModel(new UserProfile(), $id);
0 ignored issues
show
Documentation introduced by
new \app\models\UserProfile() is of type object<app\models\UserProfile>, but the function expects a object<app\traits\ActiveRecord>.

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
        if (Yii::$app->request->isPost) {
123
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
124
                Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully'));
125
                $urlToModel = Url::toRoute(['profile', 'id' => $model->user_id]);
126
                if (Yii::$app->request->isAjax) {
127
                    return $this->asJson(['redirect' => $urlToModel]);
128
                }
129
                return $this->redirect($urlToModel);
130
            }
131
            if (Yii::$app->request->isAjax) {
132
                return $this->asJson($this->collectErrors($model));
133
            }
134
        }
135
136
        return $this->render('profile', [
137
            'model' => $model
138
        ]);
139
    }
140
141
    public function actionAutocomplete()
142
    {
143
        $result = [];
144
        if (($term = Yii::$app->request->post('term')) !== null) {
145
            $data = User::find()
146
                ->like('username', $term)
147
                ->asArray()
148
                ->limit(10)
149
                ->all();
150
151
            foreach ($data as $item) {
152
                $result[] = [
153
                    'text' => $item['username'],
154
                    'id' => $item['id']
155
                ];
156
            }
157
        }
158
159
        return $this->asJson($result);
160
    }
161
162
    private function assignRole($model)
163
    {
164
        $auth = Yii::$app->authManager;
165
        $auth->revokeAll($model->id);
166
167
        if (!empty($model->role)) {
168
            $role = $auth->getRole($model->role);
169
            if ($role) {
170
                $auth->assign($role, $model->id);
171
            }
172
        }
173
    }
174
}
175