| Total Complexity | 42 |
| Total Lines | 301 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AccountController 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 AccountController, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace App\Http\Controllers\Frontend; |
||
| 10 | class AccountController extends Controller |
||
| 11 | { |
||
| 12 | public function getIndex() |
||
| 13 | { |
||
| 14 | return view('frontend.account.index')->with(array('user' => auth('web')->user())); |
||
| 15 | } |
||
| 16 | |||
| 17 | public function getEditAccount() |
||
| 18 | { |
||
| 19 | return view('frontend.account.edit-account')->with(array('user' => auth('web')->user())); |
||
| 20 | } |
||
| 21 | |||
| 22 | public function getResetAccount($confirmationCode, $email) |
||
| 23 | { |
||
| 24 | $result = ClientService::changeAccountDetails($confirmationCode, $email, config()->get('app.shop_id')); |
||
| 25 | |||
| 26 | if ($result) { |
||
| 27 | Notification::success('Je account gegevens zijn gewijzigd en je dient opnieuw in te loggen met de nieuwe gegevens.'); |
||
| 28 | } |
||
| 29 | |||
| 30 | Notification::error('Wijziging is niet mogelijk.'); |
||
| 31 | auth('web')->logout(); |
||
| 32 | return redirect()->to('account/login'); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function getEditAddress($type) |
||
| 36 | { |
||
| 37 | $shop = app('shop'); |
||
| 38 | return view('frontend.account.edit-account-address-'.$type)->with(array('sendingMethods' => $shop->sendingMethods, 'user' => auth('web')->user())); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function postEditAddress(Request $request, $type) |
||
| 42 | { |
||
| 43 | $userdata = $request->all(); |
||
| 44 | |||
| 45 | // create the validation rules ------------------------ |
||
| 46 | $rules = array( |
||
| 47 | 'firstname' => 'required', |
||
| 48 | 'lastname' => 'required', |
||
| 49 | 'zipcode' => 'required|max:8', |
||
| 50 | 'housenumber' => 'required|numeric', |
||
| 51 | 'street' => 'required', |
||
| 52 | 'city' => 'required', |
||
| 53 | 'country' => 'required' |
||
| 54 | ); |
||
| 55 | |||
| 56 | $validator = Validator::make($userdata, $rules); |
||
| 57 | |||
| 58 | if ($validator->fails()) { |
||
| 59 | // get the error messages from the validator |
||
| 60 | foreach ($validator->errors()->all() as $error) { |
||
| 61 | Notification::error($error); |
||
| 62 | } |
||
| 63 | |||
| 64 | // redirect our user back to the form with the errors from the validator |
||
| 65 | return redirect()->to('account/edit-address/'.$type) |
||
| 66 | ->with(array('type' => $type))->withInput(); |
||
| 67 | } |
||
| 68 | |||
| 69 | $user = auth('web')->user(); |
||
| 70 | |||
| 71 | if ($type == 'bill') { |
||
| 72 | $id = $user->clientBillAddress->id; |
||
|
|
|||
| 73 | |||
| 74 | if ($user->clientDeliveryAddress->id == $user->clientBillAddress->id) { |
||
| 75 | $clientAddress = ClientService::createAddress($userdata, $user->id); |
||
| 76 | ClientService::setBillOrDeliveryAddress(config()->get('app.shop_id'), $user->id, $clientAddress->id, $type); |
||
| 77 | } else { |
||
| 78 | $clientAddress = ClientService::editAddress($user->id, $id, $userdata); |
||
| 79 | } |
||
| 80 | } elseif ($type == 'delivery') { |
||
| 81 | $id = $user->clientDeliveryAddress->id; |
||
| 82 | |||
| 83 | if ($user->clientDeliveryAddress->id == $user->clientBillAddress->id) { |
||
| 84 | $clientAddress = ClientService::createAddress($userdata, $user->id); |
||
| 85 | ClientService::setBillOrDeliveryAddress(config()->get('app.shop_id'), $user->id, $clientAddress->id, $type); |
||
| 86 | } else { |
||
| 87 | $clientAddress = ClientService::editAddress($user->id, $id, $userdata); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | return redirect()->to('account'); |
||
| 92 | |||
| 93 | } |
||
| 94 | |||
| 95 | public function postEditAccount(Request $request) |
||
| 96 | { |
||
| 97 | if (auth('web')->check()) { |
||
| 98 | $requestChange = ClientService::requestChangeAccountDetails($request->all(), config()->get('app.shop_id')); |
||
| 99 | |||
| 100 | if ($requestChange) { |
||
| 101 | $firstname = false; |
||
| 102 | |||
| 103 | if ($requestChange->clientBillAddress->count()) { |
||
| 104 | $firstname = $requestChange->clientBillAddress->firstname; |
||
| 105 | } |
||
| 106 | |||
| 107 | $data = array( |
||
| 108 | 'email' => $requestChange->new_email, |
||
| 109 | 'firstname' => $firstname, |
||
| 110 | 'confirmation_code' => $requestChange->confirmation_code |
||
| 111 | ); |
||
| 112 | |||
| 113 | Mail::send('frontend.email.reset-account-settings-mail', $data, function ($message) use ($data) { |
||
| 114 | |||
| 115 | $message->to($data['email'])->from('[email protected]', 'Hideyo')->subject('confirm changing account details'); |
||
| 116 | }); |
||
| 117 | |||
| 118 | Notification::success('E-mail sent'); |
||
| 119 | } else { |
||
| 120 | Notification::error('error'); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | return redirect()->to('account'); |
||
| 125 | } |
||
| 126 | |||
| 127 | public function getLogin() |
||
| 128 | { |
||
| 129 | return view('frontend.account.login'); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function postLogin(Request $request) |
||
| 133 | { |
||
| 134 | $validateLogin = ClientService::validateLogin($request->all()); |
||
| 135 | |||
| 136 | if ($validateLogin->fails()) { |
||
| 137 | foreach ($validateLogin->errors()->all() as $error) { |
||
| 138 | Notification::error($error); |
||
| 139 | } |
||
| 140 | |||
| 141 | return redirect()->back()->withInput(); |
||
| 142 | } |
||
| 143 | |||
| 144 | $login = ClientService::login($request); |
||
| 145 | |||
| 146 | if($login) { |
||
| 147 | return redirect()->to('/account'); |
||
| 148 | } |
||
| 149 | |||
| 150 | Notification::error('Not correct.'); |
||
| 151 | return redirect()->back()->withInput(); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function getRegister() |
||
| 155 | { |
||
| 156 | return view('frontend.account.register')->with(array('sendingMethods' => app('shop')->sendingMethods)); |
||
| 157 | } |
||
| 158 | |||
| 159 | public function postRegister(Request $request) |
||
| 160 | { |
||
| 161 | $userdata = $request->all(); |
||
| 162 | $validateRegister = ClientService::validateRegister($userdata); |
||
| 163 | |||
| 164 | if($validateRegister->fails()) { |
||
| 165 | foreach ($validator->errors()->all() as $error) { |
||
| 166 | Notification::error($error); |
||
| 167 | } |
||
| 168 | |||
| 169 | return redirect()->back()->withInput(); |
||
| 170 | } |
||
| 171 | |||
| 172 | $register = ClientService::register($userdata, config()->get('app.shop_id')); |
||
| 173 | |||
| 174 | if ($register) { |
||
| 175 | $data = $register->toArray(); |
||
| 176 | Mail::send('frontend.email.register-mail', array('user' => $register->toArray(), 'password' => $request->get('password'), 'billAddress' => $register->clientBillAddress->toArray()), function ($message) use ($data) { |
||
| 177 | $message->to($data['email'])->from('[email protected]', 'Hideyo')->subject(trans('register-completed-subject')); |
||
| 178 | }); |
||
| 179 | Notification::success(trans('you-are-registered-consumer')); |
||
| 180 | return redirect()->to('account/login'); |
||
| 181 | } |
||
| 182 | |||
| 183 | Notification::error('Email already exists.'); |
||
| 184 | return redirect()->back()->withInput(); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function getForgotPassword() |
||
| 188 | { |
||
| 189 | return view('frontend.account.forgot-password'); |
||
| 190 | } |
||
| 191 | |||
| 192 | public function postForgotPassword(Request $request) |
||
| 193 | { |
||
| 194 | $rules = array( |
||
| 195 | 'email' => 'required|email' |
||
| 196 | ); |
||
| 197 | |||
| 198 | $validator = Validator::make($request->all(), $rules); |
||
| 199 | |||
| 200 | if ($validator->fails()) { |
||
| 201 | return redirect()->back() |
||
| 202 | ->withErrors($validator, 'forgot')->withInput(); |
||
| 203 | } |
||
| 204 | |||
| 205 | $userdata = $request->all(); |
||
| 206 | |||
| 207 | $forgotPassword = ClientService::getConfirmationCodeByEmail($userdata['email'], config()->get('app.shop_id')); |
||
| 208 | |||
| 209 | if ($forgotPassword) { |
||
| 210 | $firstname = false; |
||
| 211 | |||
| 212 | if ($forgotPassword->clientBillAddress->count()) { |
||
| 213 | $firstname = $forgotPassword->clientBillAddress->firstname; |
||
| 214 | } |
||
| 215 | |||
| 216 | $data = array( |
||
| 217 | 'email' => $userdata['email'], |
||
| 218 | 'firstname' => $firstname, |
||
| 219 | 'code' => $forgotPassword->confirmation_code |
||
| 220 | ); |
||
| 221 | |||
| 222 | Mail::send('frontend.email.reset-password-mail', $data, function ($message) use ($data) { |
||
| 223 | $message->to($data['email'])->from('[email protected]', 'Phil & Phae')->subject('Wachtwoord vergeten'); |
||
| 224 | }); |
||
| 225 | |||
| 226 | Notification::success('Er is een e-mail gestuurd. Hiermee kan je je wachtwoord resetten.'); |
||
| 227 | |||
| 228 | return redirect()->back(); |
||
| 229 | } |
||
| 230 | |||
| 231 | Notification::error('Account not exist.'); |
||
| 232 | return redirect()->back()->withErrors($forgotPassword['errors'], 'forgot')->withInput(); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function getResetPassword($confirmationCode, $email) |
||
| 236 | { |
||
| 237 | $result = ClientService::validateConfirmationCode($confirmationCode, $email, config()->get('app.shop_id')); |
||
| 238 | |||
| 239 | if ($result) { |
||
| 240 | return view('frontend.account.reset-password')->with(array('confirmationCode' => $confirmationCode, 'email' => $email)); |
||
| 241 | } |
||
| 242 | |||
| 243 | Notification::error('wachtwoord vergeten is mislukt'); |
||
| 244 | return redirect()->to('account/forgot-password')->withErrors(true, 'forgot')->withInput(); |
||
| 245 | } |
||
| 246 | |||
| 247 | public function postResetPassword(Request $request, $confirmationCode, $email) |
||
| 248 | { |
||
| 249 | $rules = array( |
||
| 250 | 'password' => 'required' |
||
| 251 | ); |
||
| 252 | |||
| 253 | $validator = Validator::make($request->all(), $rules); |
||
| 254 | |||
| 255 | if ($validator->fails()) { |
||
| 256 | // get the error messages from the validator |
||
| 257 | $messages = $validator->messages(); |
||
| 258 | |||
| 259 | // redirect our user back to the form with the errors from the validator |
||
| 260 | return redirect()->to('account/reset-password/'.$confirmationCode.'/'.$email) |
||
| 261 | ->withErrors($validator, 'reset')->withInput(); |
||
| 262 | } |
||
| 263 | |||
| 264 | $result = ClientService::validateConfirmationCode($confirmationCode, $email, config()->get('app.shop_id')); |
||
| 265 | |||
| 266 | if ($result) { |
||
| 267 | $result = ClientService::changePassword(array('confirmation_code' => $confirmationCode, 'email' => $email, 'password' => $request->get('password')), config()->get('app.shop_id')); |
||
| 268 | Notification::success('Je wachtwoord is veranderd en je kan nu inloggen.'); |
||
| 269 | return redirect()->to('account/login'); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | public function postSubscriberNewsletter(Request $request) |
||
| 274 | { |
||
| 275 | $userData = $request->all(); |
||
| 276 | $result = ClientService::subscribeNewsletter($userData['email'], config()->get('app.shop_id')); |
||
| 277 | $result = array( |
||
| 278 | "result" => true, |
||
| 279 | "html" => view('frontend.newsletter.completed')->render() |
||
| 280 | ); |
||
| 281 | |||
| 282 | ClientService::registerMailChimp($userData['email']); |
||
| 283 | return response()->json($result); |
||
| 284 | } |
||
| 285 | |||
| 286 | public function getConfirm($code, $email) |
||
| 298 | } |
||
| 299 | |||
| 300 | public function getLogout(Request $request) |
||
| 301 | { |
||
| 302 | auth('web')->logout(); |
||
| 303 | $referrer = $request->headers->get('referer'); |
||
| 304 | if ($referrer) { |
||
| 311 | } |
||
| 312 | } |