Completed
Push — master ( c064eb...85769a )
by Igor
03:38
created

UsersController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 64.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 9
dl 0
loc 125
ccs 33
cts 51
cp 0.6471
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 0 13 1
A behaviors() 0 15 1
B actions() 0 34 1
B actionEdit() 0 27 6
B actionProfile() 0 27 5
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 app\traits\ModelTrait;
9
use app\models\User;
10
use app\models\UserProfile;
11
use app\modules\admin\models\forms\UserForm;
12
use app\modules\admin\models\forms\UserProfileForm;
13
use app\modules\admin\models\search\UserSearch;
14
15
class UsersController extends \yii\web\Controller
16
{
17
    use ModelTrait;
18
19 6
    public function behaviors()
20
    {
21
        return [
22 6
            'verbs' => [
23
                'class' => VerbFilter::class,
24
                'actions' => [
25
                    'set-active' => ['post'],
26
                    'set-block' => ['post'],
27
                    'delete' => ['post'],
28
                    'operations' => ['post'],
29
                    'photo-upload' => ['post'],
30
                ],
31
            ],
32
        ];
33
    }
34
35 6
    public function actions()
36
    {
37
        return [
38 6
            'operations' => [
39 6
                'class' => 'app\modules\admin\controllers\common\OperationsAction',
40
                'modelClass' => User::class,
41
                'operations' => [
42
                    'delete' => [],
43 6
                    'set-active' => ['status' => User::STATUS_ACTIVE],
44 6
                    'set-block' => ['status' => User::STATUS_BLOCKED]
45
                ]
46
            ],
47
            'set-active' => [
48 6
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
49
                'modelClass' => User::class,
50 6
                'attributes' => ['status' => User::STATUS_ACTIVE],
51
            ],
52
            'set-block' => [
53 6
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
54
                'modelClass' => User::class,
55 6
                'attributes' => ['status' => User::STATUS_BLOCKED],
56
            ],
57
            'delete' => [
58
                'class' => 'app\modules\admin\controllers\common\DeleteAction',
59
                'modelClass' => User::class,
60
            ],
61
            'photo-upload' => [
62
                'class'     => 'rkit\filemanager\actions\UploadAction',
63
                'modelClass' => UserProfile::class,
64
                'attribute' => 'photo',
65
                'inputName' => 'file',
66
            ],
67
        ];
68
    }
69
70 6
    public function actionIndex()
71
    {
72 6
        $userSearch = new UserSearch();
73 6
        $dataProvider = $userSearch->search(Yii::$app->request->get());
74 6
        $statuses = User::getStatuses();
75
76 6
        return $this->render('index', [
77 6
            'userSearch' => $userSearch,
78 6
            'dataProvider' => $dataProvider,
79 6
            'statuses' => $statuses,
80 6
            'roles' => Yii::$app->authManager->getRoles()
81
        ]);
82
    }
83
84 3
    public function actionEdit($id = null)
85
    {
86 3
        $model = new UserForm();
87
88 3
        if ($id) {
89 2
            $model->setModel($this->findModel(new User, $id));
0 ignored issues
show
Compatibility introduced by
$this->findModel(new \app\models\User(), $id) of type object<yii\db\ActiveRecord> is not a sub-type of object<app\models\User>. It seems like you assume a child class of the class yii\db\ActiveRecord 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...
90
        }
91
92 2
        if (Yii::$app->request->isPost) {
93
            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
94
                try {
95
                    $model->save();
96
97
                    Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Saved successfully'));
98
                    return $this->asJson(['redirect' => Url::toRoute(['edit', 'id' => $model->id])]);
99
                } catch (\Exception $e) {
100
                    Yii::error($e);
101
                    return $this->asJson(false);
102
                }
103
            }
104
            return $this->asJson($this->collectErrors($model));
105
        }
106
107 2
        return $this->render('edit', [
108 2
            'model' => $model,
109
        ]);
110
    }
111
112 2
    public function actionProfile($id)
113
    {
114 2
        $model = new UserProfileForm();
115 2
        $model->setModel($this->findModel(new UserProfile, $id));
0 ignored issues
show
Compatibility introduced by
$this->findModel(new \ap...els\UserProfile(), $id) of type object<yii\db\ActiveRecord> is not a sub-type of object<app\models\UserProfile>. It seems like you assume a child class of the class yii\db\ActiveRecord 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...
116
117 1
        if (Yii::$app->request->isPost) {
118
            $data = Yii::$app->request->post();
119
            $data['UserProfileForm'] = $data['UserProfileForm'] + $data['UserProfile'];
120
121
            if ($model->load($data) && $model->validate()) {
122
                try {
123
                    $model->save();
124
125
                    Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Saved successfully'));
126
                    return $this->asJson(['redirect' => Url::toRoute(['profile', 'id' => $model->user_id])]);
127
                } catch (\Exception $e) {
128
                    Yii::error($e);
129
                    return $this->asJson(false);
130
                }
131
            }
132
            return $this->asJson($this->collectErrors($model));
133
        }
134
135 1
        return $this->render('profile', [
136 1
            'model' => $model
137
        ]);
138
    }
139
}
140