Completed
Push — master ( c7544d...5e7707 )
by Igor
04:06
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
            '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 33
            ],
32
        ];
33
    }
34
35 33
    public function actions()
36
    {
37
        return [
38
            'operations' => [
39
                'class' => 'app\modules\admin\controllers\common\OperationsAction',
40
                'modelClass' => 'app\models\User',
41
                'operations' => [
42
                    'delete' => [],
43
                    'set-active' => ['status' => User::STATUS_ACTIVE],
44
                    'set-block' => ['status' => User::STATUS_BLOCKED]
45
                ]
46 33
            ],
47
            'set-active' => [
48
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
49
                'modelClass' => 'app\models\User',
50
                'attributes' => ['status' => User::STATUS_ACTIVE],
51
            ],
52
            'set-block' => [
53
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
54
                'modelClass' => 'app\models\User',
55
                '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 10
    public function actionEdit($id = null)
85
    {
86 10
        $model = new User();
87
88 10
        if ($id) {
89 6
            $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 10
        if (Yii::$app->request->isPost) {
93 6
            if ($model->isNewRecord) {
94 4
                $model->setConfirmed();
95
            }
96
97 6
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
98 4
                $this->assignRole($model);
99
100 4
                Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully'));
101 4
                $urlToModel = Url::toRoute(['edit', 'id' => $model->id]);
102 4
                if (Yii::$app->request->isAjax) {
103 1
                    return $this->asJson(['redirect' => $urlToModel]);
104
                }
105 3
                return $this->redirect($urlToModel);
106
            }
107 2
            if (Yii::$app->request->isAjax) {
108 1
                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 9
        return $this->render('edit', [
113 9
            'model' => $model,
114 9
            'roles' => Yii::$app->authManager->getRoles()
115
        ]);
116
    }
117
118 5
    public function actionProfile($id)
119
    {
120 5
        $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 5
        if (Yii::$app->request->isPost) {
123 5
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
124 3
                Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully'));
125 3
                $urlToModel = Url::toRoute(['profile', 'id' => $model->user_id]);
126 3
                if (Yii::$app->request->isAjax) {
127 1
                    return $this->asJson(['redirect' => $urlToModel]);
128
                }
129 2
                return $this->redirect($urlToModel);
130
            }
131 2
            if (Yii::$app->request->isAjax) {
132 1
                return $this->asJson($this->collectErrors($model));
133
            }
134
        }
135
136 3
        return $this->render('profile', [
137 3
            'model' => $model
138
        ]);
139
    }
140
141 1
    public function actionAutocomplete()
142
    {
143 1
        $result = [];
144 1
        if (($term = Yii::$app->request->post('term')) !== null) {
145 1
            $data = User::find()
146 1
                ->like('username', $term)
147 1
                ->asArray()
148 1
                ->limit(10)
149 1
                ->all();
150
151 1
            foreach ($data as $item) {
152 1
                $result[] = [
153 1
                    'text' => $item['username'],
154 1
                    'id' => $item['id']
155
                ];
156
            }
157
        }
158
159 1
        return $this->asJson($result);
160
    }
161
162 4
    private function assignRole($model)
163
    {
164 4
        $auth = Yii::$app->authManager;
165 4
        $auth->revokeAll($model->id);
166
167 4
        if (!empty($model->role)) {
168 1
            $role = $auth->getRole($model->role);
169 1
            if ($role) {
170 1
                $auth->assign($role, $model->id);
171
            }
172
        }
173 4
    }
174
}
175