Completed
Push — master ( 51f787...8bc09a )
by Alexey
03:06
created

ProfileController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
c 0
b 0
f 0
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
namespace modules\users\controllers\common;
4
5
use Yii;
6
use yii\web\Controller;
7
use modules\users\models\User;
8
use modules\users\models\UpdatePasswordForm;
9
use modules\users\models\UserDeleteForm;
10
use modules\rbac\models\Assignment;
11
use yii\web\NotFoundHttpException;
12
use yii\bootstrap\ActiveForm;
13
use yii\web\Response;
14
use modules\users\Module;
15
16
/**
17
 * Class ProfileController
18
 * @package modules\users\controllers\common
19
 */
20
class ProfileController extends Controller
21
{
22
    /**
23
     * @return string
24
     * @throws NotFoundHttpException
25
     */
26
    public function actionIndex()
27
    {
28
        $model = $this->findModel();
29
30
        $assignModel = new Assignment();
31
        $assignModel->user = $model;
32
33
        return $this->render('index', [
34
            'model' => $model,
35
            'assignModel' => $assignModel,
36
        ]);
37
    }
38
39
    /**
40
     * @return string
41
     * @throws NotFoundHttpException
42
     */
43
    public function actionUpdate()
44
    {
45
        $model = $this->findModel();
46
        if ($model->profile->load(Yii::$app->request->post()) && $model->profile->save()) {
47
            Yii::$app->session->setFlash('success', Module::t('module', 'Profile successfully save.'));
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

47
            Yii::$app->session->/** @scrutinizer ignore-call */ 
48
                                setFlash('success', Module::t('module', 'Profile successfully save.'));

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...
48
            return $this->redirect(['update', 'tab' => 'profile']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->redirect(a...', 'tab' => 'profile')) returns the type yii\web\Response which is incompatible with the documented return type string.
Loading history...
49
        }
50
        return $this->render('update', [
51
            'model' => $model,
52
            'passwordForm' => new UpdatePasswordForm($model),
53
        ]);
54
    }
55
56
    /**
57
     * @return Response
58
     * @throws NotFoundHttpException
59
     * @throws \yii\base\Exception
60
     */
61
    public function actionUpdateAvatar()
62
    {
63
        $model = $this->findModel();
64
        if ($model->profile->load(Yii::$app->request->post()) && $model->profile->save()) {
65
            Yii::$app->session->setFlash('success', Module::t('module', 'Form successfully saved.'));
66
        } else {
67
            Yii::$app->session->setFlash('error', Module::t('module', 'Error! Failed to save the form.'));
68
        }
69
        return $this->redirect(['update', 'tab' => 'avatar']);
70
    }
71
72
    /**
73
     * @return array|Response
74
     * @throws NotFoundHttpException
75
     */
76
    public function actionAjaxValidateAvatarForm()
77
    {
78
        $model = $this->findModel();
79
        if (Yii::$app->request->isAjax && $model->profile->load(Yii::$app->request->post())) {
80
            Yii::$app->response->format = Response::FORMAT_JSON;
81
            return ActiveForm::validate($model->profile);
82
        }
83
        return $this->redirect(['index']);
84
    }
85
86
    /**
87
     * @return Response
88
     * @throws NotFoundHttpException
89
     * @throws \yii\base\Exception
90
     */
91
    public function actionUpdatePassword()
92
    {
93
        $model = new UpdatePasswordForm($this->findModel());
94
        if ($model->load(Yii::$app->request->post()) && $model->resetPassword()) {
95
            Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.'));
96
        } else {
97
            Yii::$app->session->setFlash('error', Module::t('module', 'Error! Password changed not successfully.'));
98
        }
99
        return $this->redirect(['update', 'tab' => 'password']);
100
    }
101
102
    /**
103
     * @return array|Response
104
     * @throws NotFoundHttpException
105
     */
106
    public function actionAjaxValidatePasswordForm()
107
    {
108
        $model = new UpdatePasswordForm($this->findModel());
109
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
110
            Yii::$app->response->format = Response::FORMAT_JSON;
111
            return ActiveForm::validate($model);
112
        }
113
        return $this->redirect(['index']);
114
    }
115
116
    /**
117
     * @return array|Response
118
     * @throws NotFoundHttpException
119
     */
120
    public function actionAjaxValidatePasswordDeleteForm()
121
    {
122
        $model = new UserDeleteForm($this->findModel());
123
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
124
            Yii::$app->response->format = Response::FORMAT_JSON;
125
            return ActiveForm::validate($model);
126
        }
127
        return $this->redirect(['delete']);
128
    }
129
130
    /**
131
     * Action Generate new auth key
132
     * @throws NotFoundHttpException
133
     */
134
    public function actionGenerateAuthKey()
135
    {
136
        $model = $this->processGenerateAuthKey();
137
        if (Yii::$app->request->isAjax) {
138
            Yii::$app->response->format = Response::FORMAT_JSON;
139
            return [
140
                'success' => $model->auth_key,
141
            ];
142
        }
143
        return $this->redirect(['index']);
144
    }
145
146
    /**
147
     * Generate new auth key
148
     * @return User|null
149
     * @throws NotFoundHttpException
150
     */
151
    private function processGenerateAuthKey()
152
    {
153
        $model = $this->findModel();
154
        $model->generateAuthKey();
155
        $model->save();
156
        return $model;
157
    }
158
159
    /**
160
     * @return string|Response
161
     * @throws NotFoundHttpException
162
     * @throws \Throwable
163
     * @throws \yii\db\StaleObjectException
164
     */
165
    public function actionDelete()
166
    {
167
        $model = new UserDeleteForm($this->findModel());
168
        if ($model->load(Yii::$app->request->post()) && $model->userDelete()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $model->userDelete() of type integer|false is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
169
            Yii::$app->user->logout();
0 ignored issues
show
Bug introduced by
The method logout() 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

169
            Yii::$app->user->/** @scrutinizer ignore-call */ 
170
                             logout();

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...
170
            Yii::$app->session->setFlash('success', Module::t('module', 'Your profile has been successfully deleted!'));
171
            return $this->goHome();
172
        }
173
        return $this->render('delete', [
174
            'model' => $model,
175
        ]);
176
    }
177
178
    /**
179
     * @return User|null
180
     * @throws NotFoundHttpException
181
     */
182
    private function findModel()
183
    {
184
        if (!Yii::$app->user->isGuest) {
185
            /** @var User $identity */
186
            $identity = Yii::$app->user->identity;
187
            if (($model = User::findOne($identity->id)) !== null) {
188
                return $model;
189
            }
190
        }
191
        throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.'));
192
    }
193
}
194