| Total Complexity | 43 |
| Total Lines | 267 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| 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 | $id = Yii::$app->request->get('id') ?: Yii::$app->user->id; |
||
| 144 | if (!is_array($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 | } |
||
| 155 | throw new NotFoundHttpException('The requested page does not exist.'); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Delete Avatar files |
||
| 160 | * @param int $id |
||
| 161 | * @return Response |
||
| 162 | */ |
||
| 163 | public function actionDeleteAvatar($id) |
||
| 164 | { |
||
| 165 | $model = new UploadForm(); |
||
| 166 | $fileName = $model->getFileName(); |
||
| 167 | $avatar = $model->getPath($id) . DIRECTORY_SEPARATOR . $fileName; |
||
| 168 | $thumb = $model->getPath($id) . DIRECTORY_SEPARATOR . UploadForm::PREFIX_THUMBNAIL . $fileName; |
||
| 169 | $original = $model->getPath($id) . DIRECTORY_SEPARATOR . UploadForm::PREFIX_ORIGINAL . $fileName; |
||
| 170 | $model->delete([$avatar, $thumb, $original]); |
||
| 171 | /** @var yii\web\Session $session */ |
||
| 172 | $session = Yii::$app->session; |
||
| 173 | $session->setFlash('success', Module::t('module', 'Successfully deleted.')); |
||
| 174 | return $this->redirect(['update', 'tab' => 'avatar']); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return Response |
||
| 179 | * @throws NotFoundHttpException |
||
| 180 | * @throws Exception |
||
| 181 | */ |
||
| 182 | public function actionUpdatePassword() |
||
| 183 | { |
||
| 184 | $model = new UpdatePasswordForm($this->findModel()); |
||
| 185 | /** @var yii\web\Session $session */ |
||
| 186 | $session = Yii::$app->session; |
||
| 187 | if ($model->load(Yii::$app->request->post()) && $model->resetPassword()) { |
||
| 188 | $session->setFlash('success', Module::t('module', 'Password changed successfully.')); |
||
| 189 | } else { |
||
| 190 | $session->setFlash('error', Module::t('module', 'Error! Password changed not successfully.')); |
||
| 191 | } |
||
| 192 | return $this->redirect(['update', 'tab' => 'password']); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return array|Response |
||
| 197 | * @throws NotFoundHttpException |
||
| 198 | */ |
||
| 199 | public function actionAjaxValidatePasswordForm() |
||
| 200 | { |
||
| 201 | $model = new UpdatePasswordForm($this->findModel()); |
||
| 202 | if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { |
||
| 203 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 204 | return ActiveForm::validate($model); |
||
| 205 | } |
||
| 206 | return $this->redirect(['index']); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return array|Response |
||
| 211 | * @throws NotFoundHttpException |
||
| 212 | */ |
||
| 213 | public function actionAjaxValidatePasswordDeleteForm() |
||
| 214 | { |
||
| 215 | $model = new UserDeleteForm($this->findModel()); |
||
| 216 | if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { |
||
| 217 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 218 | return ActiveForm::validate($model); |
||
| 219 | } |
||
| 220 | return $this->redirect(['delete']); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return array|Response |
||
| 225 | * @throws Exception |
||
| 226 | * @throws NotFoundHttpException |
||
| 227 | */ |
||
| 228 | public function actionGenerateAuthKey() |
||
| 229 | { |
||
| 230 | $model = $this->processGenerateAuthKey(); |
||
| 231 | if (Yii::$app->request->isAjax) { |
||
| 232 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 233 | return [ |
||
| 234 | 'success' => $model->auth_key |
||
| 235 | ]; |
||
| 236 | } |
||
| 237 | return $this->redirect(['index']); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return User |
||
| 242 | * @throws Exception |
||
| 243 | * @throws NotFoundHttpException |
||
| 244 | */ |
||
| 245 | private function processGenerateAuthKey() |
||
| 246 | { |
||
| 247 | $model = $this->findModel(); |
||
| 248 | $model->generateAuthKey(); |
||
| 249 | $model->save(); |
||
| 250 | return $model; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return string|Response |
||
| 255 | * @throws NotFoundHttpException |
||
| 256 | * @throws Throwable |
||
| 257 | * @throws StaleObjectException |
||
| 258 | */ |
||
| 259 | public function actionDelete() |
||
| 260 | { |
||
| 261 | $model = new UserDeleteForm($this->findModel()); |
||
| 262 | if ($model->load(Yii::$app->request->post()) && $model->userDelete()) { |
||
| 263 | /** @var \yii\web\User $user */ |
||
| 264 | $user = Yii::$app->user; |
||
| 265 | $user->logout(); |
||
| 266 | /** @var yii\web\Session $session */ |
||
| 267 | $session = Yii::$app->session; |
||
| 268 | $session->setFlash('success', Module::t('module', 'Your profile has been successfully deleted!')); |
||
| 269 | return $this->goHome(); |
||
| 270 | } |
||
| 271 | return $this->render('delete', [ |
||
| 272 | 'model' => $model |
||
| 273 | ]); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @return User |
||
| 278 | * @throws NotFoundHttpException |
||
| 279 | */ |
||
| 280 | private function findModel() |
||
| 292 | } |
||
| 293 | } |
||
| 294 |