Passed
Push — master ( f4d26c...c933cb )
by Alexey
03:04
created

DefaultController::actionDelete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace modules\users\controllers\backend;
4
5
use Yii;
6
use modules\users\models\User;
7
use modules\users\models\search\UserSearch;
8
use yii\web\NotFoundHttpException;
9
use modules\rbac\models\Assignment;
10
use modules\users\Module;
11
12
/**
13
 * Class DefaultController
14
 * @package modules\users\controllers\backend
15
 */
16
class DefaultController extends BaseController
17
{
18
    /**
19
     * @return string
20
     * @throws \yii\base\InvalidConfigException
21
     */
22
    public function actionIndex()
23
    {
24
        $searchModel = new UserSearch();
25
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
26
        $assignModel = new Assignment();
27
        return $this->render('index', [
28
            'searchModel' => $searchModel,
29
            'dataProvider' => $dataProvider,
30
            'assignModel' => $assignModel,
31
        ]);
32
    }
33
34
    /**
35
     * Displays a single User model.
36
     * @param int|string $id
37
     * @return string|\yii\web\Response
38
     * @throws NotFoundHttpException
39
     */
40
    public function actionView($id)
41
    {
42
        if ($model = $this->findModel($id)) {
43
            $assignModel = new Assignment([
44
                'user' => $model
45
            ]);
46
            return $this->render('view', [
47
                'model' => $model,
48
                'assignModel' => $assignModel,
49
            ]);
50
        }
51
        return $this->redirect(['index']);
52
    }
53
54
    /**
55
     * Creates a new User model.
56
     * If creation is successful, the browser will be redirected to the 'view' page.
57
     * @return string|\yii\web\Response
58
     */
59
    public function actionCreate()
60
    {
61
        $model = new User();
62
        $model->scenario = $model::SCENARIO_ADMIN_CREATE;
63
        $model->status = $model::STATUS_WAIT;
64
        /** @var \modules\users\models\User $identity */
65
        $identity = Yii::$app->user->identity;
66
        $model->registration_type = $identity->id;
67
68
        if ($model->load(Yii::$app->request->post())) {
69
            if ($model->save()) {
70
                return $this->redirect(['view', 'id' => $model->id]);
71
            }
72
        }
73
        return $this->render('create', [
74
            'model' => $model,
75
        ]);
76
    }
77
78
    /**
79
     * Updates an existing User model.
80
     * @param int|string $id
81
     * @return string|\yii\web\Response
82
     * @throws NotFoundHttpException
83
     */
84
    public function actionUpdate($id)
85
    {
86
        $model = $this->findModel($id);
87
        $model->scenario = $model::SCENARIO_ADMIN_UPDATE;
88
        if ($model->load(Yii::$app->request->post())) {
89
            if ($model->save()) {
90
                return $this->redirect(['view', 'id' => $model->id]);
91
            }
92
        }
93
        return $this->render('update_admin', [
94
            'model' => $model,
95
        ]);
96
    }
97
98
    /**
99
     * Change Status
100
     * @param int|string $id
101
     * @return array|\yii\web\Response
102
     * @throws NotFoundHttpException
103
     */
104
    public function actionStatus($id)
105
    {
106
        if ($model = $this->processChangeStatus($id)) {
107
            if (Yii::$app->request->isAjax) {
108
                Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
109
                return [
110
                    'body' => $model->getStatusLabelName(),
111
                    'success' => true,
112
                ];
113
            }
114
        }
115
        return $this->redirect(['index']);
116
    }
117
118
    /**
119
     * @param int|string $id
120
     * @return bool|User|null
121
     * @throws NotFoundHttpException
122
     */
123
    private function processChangeStatus($id)
124
    {
125
        $model = $this->findModel($id);
126
        /** @var object $identity */
127
        $identity = Yii::$app->user->identity;
128
        if ($model->id !== $identity->id && !$model->isSuperAdmin($model->id)) {
129
            $model->setStatus();
130
            $model->save(false);
131
            return $model;
132
        }
133
        return false;
134
    }
135
136
    /**
137
     * Deletes an existing User model.
138
     * If deletion is successful, the browser will be redirected to the 'index' page.
139
     * @param int|string $id
140
     * @return \yii\web\Response
141
     * @throws NotFoundHttpException
142
     * @throws \Exception
143
     * @throws \Throwable
144
     * @throws \yii\db\StaleObjectException
145
     */
146
    public function actionDelete($id)
147
    {
148
        $model = $this->findModel($id);
149
        if (!$model->isSuperAdmin()) {
150
            if ($model->isDeleted()) {
151
                $model->delete();
152
                Yii::$app->session->setFlash('success', Module::t('module', 'The user "{:name}" have been successfully deleted.', [':name' => $model->username]));
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
                Yii::$app->session->/** @scrutinizer ignore-call */ 
153
                                    setFlash('success', Module::t('module', 'The user "{:name}" have been successfully deleted.', [':name' => $model->username]));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
            } else {
154
                /** @var $model \yii2tech\ar\softdelete\SoftDeleteBehavior */
155
                $model->softDelete();
156
                /** @var $model User */
157
                Yii::$app->session->setFlash('success', Module::t('module', 'The user "{:name}" are marked as deleted.', [':name' => $model->username]));
158
            }
159
        }
160
        return $this->redirect(['index']);
161
    }
162
}
163