ProfileController   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 18
eloc 59
c 2
b 0
f 1
dl 0
loc 154
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 15 1
A actionIndex() 0 5 1
A actionUpdate() 0 5 1
A actionUpdateProfile() 0 15 3
A processGenerateAuthKey() 0 6 1
A actionUpdatePassword() 0 16 3
A findModel() 0 10 3
A actionAjaxValidatePasswordForm() 0 9 3
A actionGenerateAuthKey() 0 10 2
1
<?php
2
3
namespace modules\users\controllers;
4
5
use Yii;
6
use yii\web\Controller;
7
use modules\users\models\Profile;
8
use yii\filters\AccessControl;
9
use yii\filters\VerbFilter;
10
use yii\web\NotFoundHttpException;
11
use yii\bootstrap\ActiveForm;
12
use yii\web\Response;
13
use modules\users\Module;
14
15
/**
16
 * Class ProfileController
17
 * @package modules\users\controllers
18
 */
19
class ProfileController extends Controller
20
{
21
    /**
22
     * @inheritdoc
23
     * @return array
24
     */
25
    public function behaviors()
26
    {
27
        return [
28
            'verbs' => [
29
                'class' => VerbFilter::class,
30
                'actions' => [
31
                    'delete' => ['post'],
32
                ],
33
            ],
34
            'access' => [
35
                'class' => AccessControl::class,
36
                'rules' => [
37
                    [
38
                        'allow' => true,
39
                        'roles' => ['@']
40
                    ],
41
                ],
42
            ],
43
        ];
44
    }
45
46
    /**
47
     * @return string
48
     * @throws NotFoundHttpException
49
     */
50
    public function actionIndex()
51
    {
52
        $model = $this->findModel();
53
        return $this->render('index', [
54
            'model' => $model
55
        ]);
56
    }
57
58
    /**
59
     * @return string
60
     * @throws NotFoundHttpException
61
     */
62
    public function actionUpdate()
63
    {
64
        $model = $this->findModel();
65
        return $this->render('update', [
66
            'model' => $model,
67
        ]);
68
    }
69
70
    /**
71
     * @return Response
72
     * @throws NotFoundHttpException
73
     */
74
    public function actionUpdateProfile()
75
    {
76
        $model = $this->findModel();
77
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
78
            Yii::$app->session->setFlash(
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

78
            Yii::$app->session->/** @scrutinizer ignore-call */ 
79
                                setFlash(

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...
79
                'success',
80
                Module::t('module', 'Profile successfully changed.')
81
            );
82
        } else {
83
            Yii::$app->session->setFlash(
84
                'error',
85
                Module::t('module', 'Error! Profile not changed.')
86
            );
87
        }
88
        return $this->redirect(['update', 'tab' => 'profile']);
89
    }
90
91
    /**
92
     * @return array|Response
93
     * @throws NotFoundHttpException
94
     */
95
    public function actionUpdatePassword()
96
    {
97
        $model = $this->findModel();
98
        $model->scenario = $model::SCENARIO_PASSWORD_UPDATE;
99
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
100
            Yii::$app->session->setFlash(
101
                'success',
102
                Module::t('module', 'Password changed successfully.')
103
            );
104
        } else {
105
            Yii::$app->session->setFlash(
106
                'error',
107
                Module::t('module', 'Error! Password changed not successfully.')
108
            );
109
        }
110
        return $this->redirect(['update', 'tab' => 'password']);
111
    }
112
113
    /**
114
     * @return array|Response
115
     * @throws NotFoundHttpException
116
     */
117
    public function actionAjaxValidatePasswordForm()
118
    {
119
        $model = $this->findModel();
120
        $model->scenario = $model::SCENARIO_PASSWORD_UPDATE;
121
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
122
            Yii::$app->response->format = Response::FORMAT_JSON;
123
            return ActiveForm::validate($model);
124
        }
125
        return $this->redirect(['index']);
126
    }
127
128
    /**
129
     * Action Generate new auth key
130
     * @throws NotFoundHttpException
131
     */
132
    public function actionGenerateAuthKey()
133
    {
134
        $model = $this->processGenerateAuthKey();
135
        if (Yii::$app->request->isAjax) {
136
            Yii::$app->response->format = Response::FORMAT_JSON;
137
            return [
138
                'success' => $model->auth_key,
139
            ];
140
        }
141
        return $this->redirect(['index']);
142
    }
143
144
    /**
145
     * Generate new auth key
146
     * @return Profile|null
147
     * @throws NotFoundHttpException
148
     */
149
    private function processGenerateAuthKey()
150
    {
151
        $model = $this->findModel();
152
        $model->generateAuthKey();
153
        $model->save();
154
        return $model;
155
    }
156
157
    /**
158
     * Finds the User model based on its primary key value.
159
     * If the model is not found, a 404 HTTP exception will be thrown.
160
     * @return null|Profile the loaded model
161
     * @throws NotFoundHttpException if the model cannot be found
162
     */
163
    private function findModel()
164
    {
165
        if (!Yii::$app->user->isGuest) {
166
            /** @var object $identity */
167
            $identity = Yii::$app->user->identity;
168
            if (($model = Profile::findOne($identity->id)) !== null) {
169
                return $model;
170
            }
171
        }
172
        throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.'));
173
    }
174
}
175