| Total Complexity | 56 |
| Total Lines | 355 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CheckoutController 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 CheckoutController, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace App\Http\Controllers\Frontend; |
||
| 14 | class CheckoutController extends Controller |
||
| 15 | { |
||
| 16 | public function checkout() |
||
| 17 | { |
||
| 18 | $sendingMethodsList = SendingMethodService::selectAllActiveByShopId(config()->get('app.shop_id')); |
||
| 19 | |||
| 20 | if (Cart::getContent()->count()) { |
||
| 21 | |||
| 22 | $paymentMethodsList = Cart::getConditionsByType('sending_method')->first()->getAttributes()['data']['related_payment_methods_list']; |
||
| 23 | |||
| 24 | if(!Cart::getConditionsByType('sending_method')->count()) { |
||
| 25 | Notification::error('Selecteer een verzendwijze'); |
||
| 26 | return redirect()->to('cart'); |
||
| 27 | } |
||
| 28 | |||
| 29 | if(!Cart::getConditionsByType('payment_method')->count()) { |
||
| 30 | |||
| 31 | Notification::error('Selecteer een betaalwijze'); |
||
| 32 | return redirect()->to('cart'); |
||
| 33 | } |
||
| 34 | |||
| 35 | } else { |
||
| 36 | return redirect()->to('cart'); |
||
| 37 | } |
||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | if (auth('web')->guest()) { |
||
| 42 | $noAccountUser = session()->get('noAccountUser'); |
||
| 43 | if ($noAccountUser) { |
||
| 44 | if (!isset($noAccountUser['delivery'])) { |
||
| 45 | $noAccountUser['delivery'] = $noAccountUser; |
||
| 46 | session()->put('noAccountUser', $noAccountUser); |
||
| 47 | } |
||
| 48 | |||
| 49 | return view('frontend.checkout.no-account')->with(array( |
||
| 50 | 'noAccountUser' => $noAccountUser, |
||
| 51 | 'sendingMethodsList' => $sendingMethodsList, |
||
| 52 | 'paymentMethodsList' => $paymentMethodsList)); |
||
| 53 | } |
||
| 54 | |||
| 55 | return view('frontend.checkout.login')->with(array( 'sendingMethodsList' => $sendingMethodsList, 'paymentMethodsList' => $paymentMethodsList)); |
||
| 56 | } |
||
| 57 | |||
| 58 | $user = auth('web')->user(); |
||
| 59 | self::checkCountryPrice($user->clientDeliveryAddress->country); |
||
| 60 | |||
| 61 | if (!$user->clientDeliveryAddress()->count()) { |
||
| 62 | ClientService::setBillOrDeliveryAddress(config()->get('app.shop_id'), $user->id, $user->clientBillAddress->id, 'delivery'); |
||
| 63 | return redirect()->to('cart/checkout'); |
||
| 64 | } |
||
| 65 | |||
| 66 | return view('frontend.checkout.index')->with(array( |
||
| 67 | 'user' => $user, |
||
| 68 | 'sendingMethodsList' => $sendingMethodsList, |
||
| 69 | 'paymentMethodsList' => $paymentMethodsList)); |
||
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | public function postCheckoutLogin(Request $request) |
||
| 74 | { |
||
| 75 | // create the validation rules ------------------------ |
||
| 76 | $rules = array( |
||
| 77 | 'email' => 'required|email', // required and must be unique in the ducks table |
||
| 78 | 'password' => 'required' |
||
| 79 | ); |
||
| 80 | |||
| 81 | $validator = Validator::make($request->all(), $rules); |
||
| 82 | |||
| 83 | if ($validator->fails()) { |
||
| 84 | |||
| 85 | foreach ($validator->errors()->all() as $error) { |
||
| 86 | Notification::error($error); |
||
| 87 | } |
||
| 88 | |||
| 89 | return redirect()->to('cart/checkout') |
||
| 90 | ->withErrors(true, 'login')->withInput(); |
||
| 91 | } |
||
| 92 | |||
| 93 | $userdata = array( |
||
| 94 | 'email' => $request->get('email'), |
||
| 95 | 'password' => $request->get('password'), |
||
| 96 | 'confirmed' => 1, |
||
| 97 | 'active' => 1, |
||
| 98 | 'shop_id' => config()->get('app.shop_id') |
||
| 99 | ); |
||
| 100 | |||
| 101 | /* Try to authenticate the credentials */ |
||
| 102 | if (auth('web')->attempt($userdata)) { |
||
| 103 | // we are now logged in, go to admin |
||
| 104 | return redirect()->to('cart/checkout'); |
||
| 105 | } |
||
| 106 | |||
| 107 | Notification::error(trans('message.error.data-is-incorrect')); |
||
| 108 | return redirect()->to('cart/checkout')->withErrors(true, 'login')->withInput(); |
||
| 109 | } |
||
| 110 | |||
| 111 | //to-do: transfer logic to repo |
||
| 112 | public function postCheckoutRegister(Request $request) |
||
| 113 | { |
||
| 114 | if (!Cart::getContent()->count()) { |
||
| 115 | return redirect()->to('cart/checkout'); |
||
| 116 | } |
||
| 117 | |||
| 118 | $userdata = $request->all(); |
||
| 119 | |||
| 120 | $rules = array( |
||
| 121 | 'email' => 'required|email', // required and must be unique in the ducks table |
||
| 122 | 'password' => 'required', |
||
| 123 | 'firstname' => 'required', |
||
| 124 | 'lastname' => 'required', |
||
| 125 | 'zipcode' => 'required', |
||
| 126 | 'housenumber' => 'required|numeric', |
||
| 127 | 'street' => 'required', |
||
| 128 | 'city' => 'required' |
||
| 129 | ); |
||
| 130 | |||
| 131 | if (!$userdata['password']) { |
||
| 132 | unset($rules['email']); |
||
| 133 | unset($rules['password']); |
||
| 134 | } |
||
| 135 | |||
| 136 | $validator = Validator::make($request->all(), $rules); |
||
| 137 | |||
| 138 | if ($validator->fails()) { |
||
| 139 | // get the error messages from the validator |
||
| 140 | foreach ($validator->errors()->all() as $error) { |
||
| 141 | Notification::error($error); |
||
| 142 | } |
||
| 143 | // redirect our user back to the form with the errors from the validator |
||
| 144 | return redirect()->to('cart/checkout') |
||
| 145 | ->withErrors(true, 'register')->withInput(); |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($userdata['password']) { |
||
| 149 | $registerAttempt = ClientService::validateRegister($userdata, config()->get('app.shop_id')); |
||
| 150 | |||
| 151 | if ($registerAttempt) { |
||
| 152 | $register = ClientService::register($userdata, config()->get('app.shop_id'), true); |
||
| 153 | } else { |
||
| 154 | $client = ClientService::findByEmail($userdata['email'], config()->get('app.shop_id')); |
||
| 155 | |||
| 156 | if ($client->account_created) { |
||
| 157 | Notification::error('Je hebt al een account. Login aan de linkerkant of vraag een nieuw wachtwoord aan.'); |
||
| 158 | return redirect()->to('cart/checkout')->withInput()->withErrors('Dit emailadres is al in gebruik. Je kan links inloggen.', 'register'); |
||
| 159 | } else { |
||
| 160 | $register = ClientService::createAccount($userdata, config()->get('app.shop_id')); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($register) { |
||
| 165 | $data = $register; |
||
| 166 | $data['shop'] = app('shop'); |
||
| 167 | |||
| 168 | Mail::send('frontend.email.register-mail', array('password' => $userdata['password'], 'user' => $data->toArray(), 'billAddress' => $data->clientBillAddress->toArray()), function ($message) use ($data) { |
||
| 169 | |||
| 170 | $message->to($data['email'])->from($data['shop']->email, $data['shop']->title)->subject('Je bent geregistreerd.'); |
||
| 171 | }); |
||
| 172 | |||
| 173 | $userdata = array( |
||
| 174 | 'email' => $request->get('email'), |
||
| 175 | 'password' => $request->get('password'), |
||
| 176 | 'confirmed' => 1, |
||
| 177 | 'active' => 1 |
||
| 178 | ); |
||
| 179 | |||
| 180 | auth('web')->attempt($userdata); |
||
| 181 | |||
| 182 | return redirect()->to('cart/checkout')->withErrors('Je bent geregistreerd. Er is een bevestigingsmail gestuurd.', 'login'); |
||
| 183 | } else { |
||
| 184 | Notification::error('Je hebt al een account'); |
||
| 185 | return redirect()->to('cart/checkout')->withErrors(true, 'register')->withInput(); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | unset($userdata['password']); |
||
| 190 | $registerAttempt = ClientService::validateRegisterNoAccount($userdata, config()->get('app.shop_id')); |
||
| 191 | |||
| 192 | if ($registerAttempt) { |
||
| 193 | $register = ClientService::register($userdata, config()->get('app.shop_id')); |
||
| 194 | $userdata['client_id'] = $register->id; |
||
| 195 | } else { |
||
| 196 | $client = ClientService::findByEmail($userdata['email'], config()->get('app.shop_id')); |
||
| 197 | if ($client) { |
||
| 198 | $userdata['client_id'] = $client->id; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | session()->put('noAccountUser', $userdata); |
||
| 203 | return redirect()->to('cart/checkout'); |
||
| 204 | |||
| 205 | |||
| 206 | } |
||
| 207 | |||
| 208 | public function postComplete(Request $request) |
||
| 209 | { |
||
| 210 | $noAccountUser = session()->get('noAccountUser'); |
||
| 211 | if (auth('web')->guest() and !$noAccountUser) { |
||
| 212 | return view('frontend.checkout.login'); |
||
| 213 | } |
||
| 214 | |||
| 215 | if (!Cart::getContent()->count()) { |
||
| 216 | return redirect()->to('cart/checkout'); |
||
| 217 | } |
||
| 218 | |||
| 219 | $data = array( |
||
| 220 | 'products' => Cart::getContent()->toArray(), |
||
| 221 | 'price_with_tax' => Cart::getTotalWithTax(false), |
||
| 222 | 'price_without_tax' => Cart::getTotalWithoutTax(false), |
||
| 223 | 'comments' => $request->get('comments'), |
||
| 224 | 'browser_detect' => serialize(BrowserDetect::toArray()) |
||
| 225 | ); |
||
| 226 | |||
| 227 | |||
| 228 | if (auth('web')->check()) { |
||
| 229 | $data['user_id'] = auth('web')->user()->id; |
||
| 230 | } else { |
||
| 231 | $data['user_id'] = $noAccountUser['client_id']; |
||
| 232 | } |
||
| 233 | |||
| 234 | if(Cart::getConditionsByType('sending_method')->count()) { |
||
| 235 | $data['sending_method'] = Cart::getConditionsByType('sending_method'); |
||
| 236 | } |
||
| 237 | |||
| 238 | if(Cart::getConditionsByType('sending_method_country_price')->count()) { |
||
| 239 | $data['sending_method_country_price'] = Cart::getConditionsByType('sending_method_country_price'); |
||
| 240 | } |
||
| 241 | |||
| 242 | if(Cart::getConditionsByType('payment_method')->count()) { |
||
| 243 | $data['payment_method'] = Cart::getConditionsByType('payment_method'); |
||
| 244 | } |
||
| 245 | |||
| 246 | $orderInsertAttempt = OrderService::createByUserAndShopId($data, config()->get('app.shop_id'), $noAccountUser); |
||
| 247 | |||
| 248 | if ($orderInsertAttempt AND $orderInsertAttempt->count()) { |
||
| 249 | if ($orderInsertAttempt->OrderPaymentMethod and $orderInsertAttempt->OrderPaymentMethod->paymentMethod->order_confirmed_order_status_id) { |
||
| 250 | $orderStatus = OrderService::updateStatus($orderInsertAttempt->id, $orderInsertAttempt->OrderPaymentMethod->paymentMethod->order_confirmed_order_status_id); |
||
| 251 | if ($orderInsertAttempt->OrderPaymentMethod->paymentMethod->order_confirmed_order_status_id) { |
||
| 252 | Event::fire(new OrderChangeStatus($orderStatus)); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | session()->put('orderData', $orderInsertAttempt); |
||
| 257 | |||
| 258 | if ($orderInsertAttempt->OrderPaymentMethod and $orderInsertAttempt->OrderPaymentMethod->paymentMethod->payment_external) { |
||
| 259 | return redirect()->to('cart/payment'); |
||
| 260 | } |
||
| 261 | |||
| 262 | app('cart')->clear(); |
||
| 263 | app('cart')->clearCartConditions(); |
||
| 264 | session()->flush('noAccountUser'); |
||
| 265 | $body = ""; |
||
| 266 | return view('frontend.checkout.complete')->with(array('body' => $body)); |
||
| 267 | } |
||
| 268 | |||
| 269 | return redirect()->to('cart/checkout'); |
||
| 270 | } |
||
| 271 | |||
| 272 | public function getEditAddress(Request $request, $type) { |
||
| 300 | } |
||
| 301 | |||
| 302 | public function postEditAddress(Request $request, $type) |
||
| 303 | { |
||
| 369 | } |
||
| 370 | } |
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.