| Total Complexity | 42 |
| Total Lines | 272 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 5 | 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() |
||
| 41 | ]); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return string|Response |
||
| 46 | * @throws NotFoundHttpException |
||
| 47 | */ |
||
| 48 | public function actionUpdate() |
||
| 63 | ]); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return Response |
||
| 68 | * @throws NotFoundHttpException |
||
| 69 | */ |
||
| 70 | public function actionUpdateAvatar() |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return array|Response |
||
| 86 | * @throws NotFoundHttpException |
||
| 87 | */ |
||
| 88 | public function actionAjaxValidateAvatarForm() |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Upload file |
||
| 100 | * @return Response |
||
| 101 | * @throws Exception |
||
| 102 | */ |
||
| 103 | public function actionUploadImage() |
||
| 104 | { |
||
| 105 | $model = new UploadForm(); |
||
| 106 | if (Yii::$app->request->isPost) { |
||
| 107 | /** @var yii\web\Session $session */ |
||
| 108 | $session = Yii::$app->session; |
||
| 109 | $model->imageFile = UploadedFile::getInstance($model, 'imageFile'); |
||
| 110 | if (($result = $model->upload()) && !is_string($result)) { |
||
| 111 | if (isset($result['imageFile'][0])) { |
||
| 112 | $session->setFlash('error', $result['imageFile'][0]); |
||
| 113 | } else { |
||
| 114 | $session->setFlash('error', Module::translate('module', 'Failed to upload file.')); |
||
| 115 | } |
||
| 116 | return $this->redirect(['update', 'tab' => 'avatar']); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | return $this->redirect(['update', 'tab' => 'avatar', 'modal' => 'show']); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Crop image |
||
| 124 | * @return Response |
||
| 125 | */ |
||
| 126 | public function actionCropAvatar() |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get Avatar |
||
| 139 | * @return bool |
||
| 140 | * @throws NotFoundHttpException |
||
| 141 | */ |
||
| 142 | public function actionAvatar() |
||
| 143 | { |
||
| 144 | if ($file = Yii::$app->request->get('filename')) { |
||
| 145 | /** @var int|string $id */ |
||
| 146 | $id = Yii::$app->request->get('id') ?: Yii::$app->user->id; |
||
| 147 | $model = new UploadForm(); |
||
| 148 | $storagePath = $model->getPath($id); |
||
| 149 | $response = Yii::$app->getResponse(); |
||
| 150 | if (($steam = fopen("$storagePath/$file", 'rb')) !== false) { |
||
| 151 | $response->headers->set('Content-Type', 'image/jpg'); |
||
| 152 | $response->format = Response::FORMAT_RAW; |
||
| 153 | $response->stream = $steam; |
||
| 154 | $response->send(); |
||
| 155 | return true; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | throw new NotFoundHttpException('The requested page does not exist.'); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Delete Avatar files |
||
| 163 | * @param int|string $id |
||
| 164 | * @return Response |
||
| 165 | */ |
||
| 166 | public function actionDeleteAvatar($id) |
||
| 167 | { |
||
| 168 | $model = new UploadForm(); |
||
| 169 | $fileName = $model->getFileName(); |
||
| 170 | $avatar = $model->getPath($id) . DIRECTORY_SEPARATOR . $fileName; |
||
| 171 | $thumb = $model->getPath($id) . DIRECTORY_SEPARATOR . UploadForm::PREFIX_THUMBNAIL . $fileName; |
||
| 172 | $original = $model->getPath($id) . DIRECTORY_SEPARATOR . UploadForm::PREFIX_ORIGINAL . $fileName; |
||
| 173 | $model->delete([$avatar, $thumb, $original]); |
||
| 174 | /** @var yii\web\Session $session */ |
||
| 175 | $session = Yii::$app->session; |
||
| 176 | $session->setFlash('success', Module::translate('module', 'Successfully deleted.')); |
||
| 177 | return $this->redirect(['update', 'tab' => 'avatar']); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return Response |
||
| 182 | * @throws NotFoundHttpException |
||
| 183 | * @throws Exception |
||
| 184 | */ |
||
| 185 | public function actionUpdatePassword() |
||
| 186 | { |
||
| 187 | $model = new UpdatePasswordForm($this->findModel()); |
||
| 188 | /** @var yii\web\Session $session */ |
||
| 189 | $session = Yii::$app->session; |
||
| 190 | $load = $model->load(Yii::$app->request->post()); |
||
| 191 | if ($load && $model->resetPassword()) { |
||
| 192 | $session->setFlash('success', Module::translate('module', 'Password changed successfully.')); |
||
| 193 | } else { |
||
| 194 | $session->setFlash('error', Module::translate('module', 'Error! Password changed not successfully.')); |
||
| 195 | } |
||
| 196 | return $this->redirect(['update', 'tab' => 'password']); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return array|Response |
||
| 201 | * @throws NotFoundHttpException |
||
| 202 | */ |
||
| 203 | public function actionAjaxValidatePasswordForm() |
||
| 204 | { |
||
| 205 | $model = new UpdatePasswordForm($this->findModel()); |
||
| 206 | if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { |
||
| 207 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 208 | return ActiveForm::validate($model); |
||
| 209 | } |
||
| 210 | return $this->redirect(['index']); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return array|Response |
||
| 215 | * @throws NotFoundHttpException |
||
| 216 | */ |
||
| 217 | public function actionAjaxValidatePasswordDeleteForm() |
||
| 218 | { |
||
| 219 | $model = new UserDeleteForm($this->findModel()); |
||
| 220 | if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { |
||
| 221 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 222 | return ActiveForm::validate($model); |
||
| 223 | } |
||
| 224 | return $this->redirect(['delete']); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return array|Response |
||
| 229 | * @throws Exception |
||
| 230 | * @throws NotFoundHttpException |
||
| 231 | */ |
||
| 232 | public function actionGenerateAuthKey() |
||
| 233 | { |
||
| 234 | $model = $this->processGenerateAuthKey(); |
||
| 235 | if (Yii::$app->request->isAjax) { |
||
| 236 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 237 | return [ |
||
| 238 | 'success' => $model->auth_key |
||
| 239 | ]; |
||
| 240 | } |
||
| 241 | return $this->redirect(['index']); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return User |
||
| 246 | * @throws Exception |
||
| 247 | * @throws NotFoundHttpException |
||
| 248 | */ |
||
| 249 | private function processGenerateAuthKey() |
||
| 250 | { |
||
| 251 | $model = $this->findModel(); |
||
| 252 | $model->generateAuthKey(); |
||
| 253 | $model->save(); |
||
| 254 | return $model; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return string|Response |
||
| 259 | * @throws NotFoundHttpException |
||
| 260 | * @throws Throwable |
||
| 261 | * @throws StaleObjectException |
||
| 262 | */ |
||
| 263 | public function actionDelete() |
||
| 264 | { |
||
| 265 | $model = new UserDeleteForm($this->findModel()); |
||
| 266 | $load = $model->load(Yii::$app->request->post()); |
||
| 267 | if ($load && $model->userDelete() !== false) { |
||
| 268 | /** @var \yii\web\User $user */ |
||
| 269 | $user = Yii::$app->user; |
||
| 270 | $user->logout(); |
||
| 271 | /** @var yii\web\Session $session */ |
||
| 272 | $session = Yii::$app->session; |
||
| 273 | $session->setFlash('success', Module::translate('module', 'Your profile has been successfully deleted!')); |
||
| 274 | return $this->goHome(); |
||
| 275 | } |
||
| 276 | return $this->render('delete', [ |
||
| 277 | 'model' => $model |
||
| 278 | ]); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return User |
||
| 283 | * @throws NotFoundHttpException |
||
| 284 | */ |
||
| 285 | private function findModel() |
||
| 297 | } |
||
| 298 | } |
||
| 299 |