| Total Complexity | 42 |
| Total Lines | 266 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 3 | Features | 1 |
Complex classes like ProfileController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProfileController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class ProfileController extends Controller |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @return string |
||
| 29 | * @throws NotFoundHttpException |
||
| 30 | */ |
||
| 31 | public function actionIndex() |
||
| 32 | { |
||
| 33 | $model = $this->findModel(); |
||
| 34 | |||
| 35 | $assignModel = new Assignment(); |
||
| 36 | $assignModel->user = $model; |
||
| 37 | |||
| 38 | return $this->render('index', [ |
||
| 39 | 'model' => $model, |
||
| 40 | 'assignModel' => $assignModel |
||
| 41 | ]); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return string |
||
| 46 | * @throws NotFoundHttpException |
||
| 47 | */ |
||
| 48 | public function actionUpdate() |
||
| 62 | ]); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return Response |
||
| 67 | * @throws NotFoundHttpException |
||
| 68 | * @throws Exception |
||
| 69 | */ |
||
| 70 | public function actionUpdateAvatar() |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return array|Response |
||
| 85 | * @throws NotFoundHttpException |
||
| 86 | */ |
||
| 87 | public function actionAjaxValidateAvatarForm() |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Upload file |
||
| 99 | * @return Response |
||
| 100 | * @throws Exception |
||
| 101 | */ |
||
| 102 | public function actionUploadImage() |
||
| 103 | { |
||
| 104 | $model = new UploadForm(); |
||
| 105 | if (Yii::$app->request->isPost) { |
||
| 106 | /** @var yii\web\Session $session */ |
||
| 107 | $session = Yii::$app->session; |
||
| 108 | $model->imageFile = UploadedFile::getInstance($model, 'imageFile'); |
||
| 109 | if (($result = $model->upload()) && !is_string($result)) { |
||
| 110 | if (isset($result['imageFile'][0])) { |
||
| 111 | $session->setFlash('error', $result['imageFile'][0]); |
||
| 112 | } else { |
||
| 113 | $session->setFlash('error', Module::t('module', 'Failed to upload file.')); |
||
| 114 | } |
||
| 115 | return $this->redirect(['update', 'tab' => 'avatar']); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | return $this->redirect(['update', 'tab' => 'avatar', 'modal' => 'show']); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Crop image |
||
| 123 | * @return Response |
||
| 124 | */ |
||
| 125 | public function actionCropAvatar() |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get Avatar |
||
| 138 | * @throws NotFoundHttpException |
||
| 139 | */ |
||
| 140 | public function actionAvatar() |
||
| 141 | { |
||
| 142 | if ($file = Yii::$app->request->get('filename')) { |
||
| 143 | /** @var int|string $id */ |
||
| 144 | $id = Yii::$app->request->get('id') ?: Yii::$app->user->id; |
||
| 145 | $model = new UploadForm(); |
||
| 146 | $storagePath = $model->getPath($id); |
||
| 147 | $response = Yii::$app->getResponse(); |
||
| 148 | $response->headers->set('Content-Type', 'image/jpg'); |
||
| 149 | $response->format = Response::FORMAT_RAW; |
||
| 150 | if ($response->stream = fopen("$storagePath/$file", 'rb')) { |
||
| 151 | return $response->send(); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | throw new NotFoundHttpException('The requested page does not exist.'); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Delete Avatar files |
||
| 159 | * @param int $id |
||
| 160 | * @return Response |
||
| 161 | */ |
||
| 162 | public function actionDeleteAvatar($id) |
||
| 163 | { |
||
| 164 | $model = new UploadForm(); |
||
| 165 | $fileName = $model->getFileName(); |
||
| 166 | $avatar = $model->getPath($id) . DIRECTORY_SEPARATOR . $fileName; |
||
| 167 | $thumb = $model->getPath($id) . DIRECTORY_SEPARATOR . UploadForm::PREFIX_THUMBNAIL . $fileName; |
||
| 168 | $original = $model->getPath($id) . DIRECTORY_SEPARATOR . UploadForm::PREFIX_ORIGINAL . $fileName; |
||
| 169 | $model->delete([$avatar, $thumb, $original]); |
||
| 170 | /** @var yii\web\Session $session */ |
||
| 171 | $session = Yii::$app->session; |
||
| 172 | $session->setFlash('success', Module::t('module', 'Successfully deleted.')); |
||
| 173 | return $this->redirect(['update', 'tab' => 'avatar']); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return Response |
||
| 178 | * @throws NotFoundHttpException |
||
| 179 | * @throws Exception |
||
| 180 | */ |
||
| 181 | public function actionUpdatePassword() |
||
| 182 | { |
||
| 183 | $model = new UpdatePasswordForm($this->findModel()); |
||
| 184 | /** @var yii\web\Session $session */ |
||
| 185 | $session = Yii::$app->session; |
||
| 186 | if ($model->load(Yii::$app->request->post()) && $model->resetPassword()) { |
||
| 187 | $session->setFlash('success', Module::t('module', 'Password changed successfully.')); |
||
| 188 | } else { |
||
| 189 | $session->setFlash('error', Module::t('module', 'Error! Password changed not successfully.')); |
||
| 190 | } |
||
| 191 | return $this->redirect(['update', 'tab' => 'password']); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return array|Response |
||
| 196 | * @throws NotFoundHttpException |
||
| 197 | */ |
||
| 198 | public function actionAjaxValidatePasswordForm() |
||
| 199 | { |
||
| 200 | $model = new UpdatePasswordForm($this->findModel()); |
||
| 201 | if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { |
||
| 202 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 203 | return ActiveForm::validate($model); |
||
| 204 | } |
||
| 205 | return $this->redirect(['index']); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return array|Response |
||
| 210 | * @throws NotFoundHttpException |
||
| 211 | */ |
||
| 212 | public function actionAjaxValidatePasswordDeleteForm() |
||
| 213 | { |
||
| 214 | $model = new UserDeleteForm($this->findModel()); |
||
| 215 | if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { |
||
| 216 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 217 | return ActiveForm::validate($model); |
||
| 218 | } |
||
| 219 | return $this->redirect(['delete']); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return array|Response |
||
| 224 | * @throws Exception |
||
| 225 | * @throws NotFoundHttpException |
||
| 226 | */ |
||
| 227 | public function actionGenerateAuthKey() |
||
| 228 | { |
||
| 229 | $model = $this->processGenerateAuthKey(); |
||
| 230 | if (Yii::$app->request->isAjax) { |
||
| 231 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 232 | return [ |
||
| 233 | 'success' => $model->auth_key |
||
| 234 | ]; |
||
| 235 | } |
||
| 236 | return $this->redirect(['index']); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return User |
||
| 241 | * @throws Exception |
||
| 242 | * @throws NotFoundHttpException |
||
| 243 | */ |
||
| 244 | private function processGenerateAuthKey() |
||
| 245 | { |
||
| 246 | $model = $this->findModel(); |
||
| 247 | $model->generateAuthKey(); |
||
| 248 | $model->save(); |
||
| 249 | return $model; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return string|Response |
||
| 254 | * @throws NotFoundHttpException |
||
| 255 | * @throws Throwable |
||
| 256 | * @throws StaleObjectException |
||
| 257 | */ |
||
| 258 | public function actionDelete() |
||
| 259 | { |
||
| 260 | $model = new UserDeleteForm($this->findModel()); |
||
| 261 | if ($model->load(Yii::$app->request->post()) && $model->userDelete()) { |
||
| 262 | /** @var \yii\web\User $user */ |
||
| 263 | $user = Yii::$app->user; |
||
| 264 | $user->logout(); |
||
| 265 | /** @var yii\web\Session $session */ |
||
| 266 | $session = Yii::$app->session; |
||
| 267 | $session->setFlash('success', Module::t('module', 'Your profile has been successfully deleted!')); |
||
| 268 | return $this->goHome(); |
||
| 269 | } |
||
| 270 | return $this->render('delete', [ |
||
| 271 | 'model' => $model |
||
| 272 | ]); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return User |
||
| 277 | * @throws NotFoundHttpException |
||
| 278 | */ |
||
| 279 | private function findModel() |
||
| 291 | } |
||
| 292 | } |
||
| 293 |