Completed
Push — master ( d0bd4d...b4b0fd )
by Igor
23:04
created

UsersController::actionEdit()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 13

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
c 1
b 0
f 0
ccs 7
cts 13
cp 0.5385
rs 8.5125
cc 5
eloc 13
nc 6
nop 1
crap 7.4572
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use Yii;
6
use yii\filters\VerbFilter;
7
use app\traits\ModelTrait;
8
use app\models\entity\User;
9
use app\models\entity\UserProfile;
10
use app\modules\admin\models\forms\UserForm;
11
use app\modules\admin\models\forms\UserProfileForm;
12
use app\modules\admin\models\search\UserSearch;
13
14
class UsersController extends \yii\web\Controller
15
{
16
    use ModelTrait;
17
18 6
    public function behaviors()
19
    {
20
        return [
21 6
            'verbs' => [
22
                'class' => VerbFilter::class,
23
                'actions' => [
24
                    'set-active' => ['post'],
25
                    'set-block' => ['post'],
26
                    'delete' => ['post'],
27
                    'batch' => ['post'],
28
                    'photo-upload' => ['post'],
29
                ],
30
            ],
31
        ];
32
    }
33
34 6
    public function actions()
35
    {
36
        return [
37 6
            'batch' => [
38
                'class' => 'app\modules\admin\actions\BatchAction',
39
                'modelClass' => User::class,
40
                'actions' => [
41
                    'delete' => [],
42
                    'set-active' => ['status' => User::STATUS_ACTIVE],
43
                    'set-block' => ['status' => User::STATUS_BLOCKED]
44
                ]
45
            ],
46
            'set-active' => [
47
                'class' => 'app\modules\admin\actions\UpdateAttributesAction',
48
                'modelClass' => User::class,
49
                'attributes' => ['status' => User::STATUS_ACTIVE],
50
            ],
51
            'set-block' => [
52
                'class' => 'app\modules\admin\actions\UpdateAttributesAction',
53
                'modelClass' => User::class,
54
                'attributes' => ['status' => User::STATUS_BLOCKED],
55
            ],
56
            'delete' => [
57
                'class' => 'app\modules\admin\actions\DeleteAction',
58
                'modelClass' => User::class,
59
            ],
60
            'photo-upload' => [
61
                'class'     => 'rkit\filemanager\actions\UploadAction',
62
                'modelClass' => UserProfile::class,
63
                'attribute' => 'photo',
64
                'inputName' => 'file',
65
            ],
66
        ];
67
    }
68
69 6
    public function actionIndex()
70
    {
71 6
        $userSearch = new UserSearch();
72 6
        $dataProvider = $userSearch->search(Yii::$app->request->get());
73 6
        $statuses = User::getStatuses();
74
75 6
        return $this->render('index', [
76 6
            'userSearch' => $userSearch,
77 6
            'dataProvider' => $dataProvider,
78 6
            'statuses' => $statuses,
79 6
            'roles' => Yii::$app->authManager->getRoles()
80
        ]);
81
    }
82
83 3
    public function actionEdit($id = null)
84
    {
85 3
        $model = new UserForm();
86
87 3
        if ($id) {
88 2
            $model->setModel($this->findModel(new User, $id));
0 ignored issues
show
Compatibility introduced by
$this->findModel(new \ap...els\entity\User(), $id) of type object<yii\db\ActiveRecord> is not a sub-type of object<app\models\entity\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...
89
        }
90
91 2
        if (Yii::$app->request->isPost) {
92
            Yii::$app->response->format = 'json';
93
94
            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
95
                $model->save();
96
97
                Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Saved successfully'));
98
                return $this->redirect(['edit', 'id' => $model->id]);
99
            }
100
            return $this->asJsonModelErrors($model);
101
        }
102
103 2
        return $this->render('edit', [
104 2
            'model' => $model,
105
        ]);
106
    }
107
108 2
    public function actionProfile($id)
109
    {
110 2
        $model = new UserProfileForm();
111 2
        $model->setModel($this->findModel(new UserProfile, $id));
0 ignored issues
show
Compatibility introduced by
$this->findModel(new \ap...ity\UserProfile(), $id) of type object<yii\db\ActiveRecord> is not a sub-type of object<app\models\entity\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...
112
113 1
        if (Yii::$app->request->isPost) {
114
            Yii::$app->response->format = 'json';
115
116
            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
117
                $model->save();
118
119
                Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Saved successfully'));
120
                return $this->redirect(['profile', 'id' => $model->user_id]);
121
            }
122
            return $this->asJsonModelErrors($model);
123
        }
124
125 1
        return $this->render('profile', [
126 1
            'model' => $model
127
        ]);
128
    }
129
}
130