Completed
Push — master ( c64a40...c6978c )
by Alexey
02:49
created

ProfileController::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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('success', Module::t('module', 'Profile successfully changed.'));
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('success', Module::t('module', 'Profile successfully changed.'));

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