| Total Complexity | 55 |
| Total Lines | 300 |
| 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; |
||
| 17 | class CheckoutController extends Controller |
||
| 18 | { |
||
| 19 | public function checkout() |
||
| 20 | { |
||
| 21 | $sendingMethodsList = SendingMethodService::selectAllActiveByShopId(config()->get('app.shop_id')); |
||
| 22 | |||
| 23 | if (!Cart::getContent()->count()) { |
||
| 24 | return redirect()->to('cart'); |
||
| 25 | } |
||
| 26 | |||
| 27 | $paymentMethodsList = Cart::getConditionsByType('sending_method')->first()->getAttributes()['data']['related_payment_methods_list']; |
||
| 28 | |||
| 29 | if(!Cart::getConditionsByType('sending_method')->count()) { |
||
| 30 | Notification::error('Selecteer een verzendwijze'); |
||
| 31 | return redirect()->to('cart'); |
||
| 32 | } |
||
| 33 | |||
| 34 | if(!Cart::getConditionsByType('payment_method')->count()) { |
||
| 35 | Notification::error('Selecteer een betaalwijze'); |
||
| 36 | return redirect()->to('cart'); |
||
| 37 | } |
||
| 38 | |||
| 39 | if (auth('web')->guest()) { |
||
| 40 | $noAccountUser = session()->get('noAccountUser'); |
||
| 41 | if ($noAccountUser) { |
||
| 42 | if (!isset($noAccountUser['delivery'])) { |
||
| 43 | $noAccountUser['delivery'] = $noAccountUser; |
||
| 44 | session()->put('noAccountUser', $noAccountUser); |
||
| 45 | } |
||
| 46 | |||
| 47 | return view('frontend.checkout.no-account')->with(array( |
||
| 48 | 'noAccountUser' => $noAccountUser, |
||
| 49 | 'sendingMethodsList' => $sendingMethodsList, |
||
| 50 | 'paymentMethodsList' => $paymentMethodsList)); |
||
| 51 | } |
||
| 52 | |||
| 53 | return view('frontend.checkout.login')->with(array( 'sendingMethodsList' => $sendingMethodsList, 'paymentMethodsList' => $paymentMethodsList)); |
||
| 54 | } |
||
| 55 | |||
| 56 | $user = auth('web')->user(); |
||
| 57 | |||
| 58 | if (!$user->clientDeliveryAddress()->count()) { |
||
|
|
|||
| 59 | ClientService::setBillOrDeliveryAddress(config()->get('app.shop_id'), $user->id, $user->clientBillAddress->id, 'delivery'); |
||
| 60 | return redirect()->to('cart/checkout'); |
||
| 61 | } |
||
| 62 | |||
| 63 | return view('frontend.checkout.index')->with(array( |
||
| 64 | 'user' => $user, |
||
| 65 | 'sendingMethodsList' => $sendingMethodsList, |
||
| 66 | 'paymentMethodsList' => $paymentMethodsList)); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function postCheckoutLogin(Request $request) |
||
| 96 | } |
||
| 97 | |||
| 98 | public function postCheckoutRegister(Request $request) |
||
| 99 | { |
||
| 100 | if (!Cart::getContent()->count()) { |
||
| 101 | return redirect()->to('cart/checkout'); |
||
| 102 | } |
||
| 103 | |||
| 104 | $noAccount = true; |
||
| 105 | if ($userdata['password']) { |
||
| 106 | $noAccount = false; |
||
| 107 | } |
||
| 108 | |||
| 109 | $validateRegister = ClientService::validateRegister($request->all(), $noAccount); |
||
| 110 | |||
| 111 | if($validateRegister->fails()) { |
||
| 112 | foreach ($validator->errors()->all() as $error) { |
||
| 113 | Notification::error($error); |
||
| 114 | } |
||
| 115 | |||
| 116 | return redirect()->to('cart/checkout') |
||
| 117 | ->withErrors(true, 'register')->withInput();; |
||
| 118 | } |
||
| 119 | |||
| 120 | if ($request->get('password')) { |
||
| 121 | $registerAttempt = ClientService::validateRegister($request->all(), config()->get('app.shop_id')); |
||
| 122 | |||
| 123 | if ($registerAttempt) { |
||
| 124 | $register = ClientService::register($request->all(), config()->get('app.shop_id'), true); |
||
| 125 | } else { |
||
| 126 | $client = ClientService::findByEmail($request->get('email'), config()->get('app.shop_id')); |
||
| 127 | |||
| 128 | if ($client->account_created) { |
||
| 129 | Notification::error('Je hebt al een account. Login aan de linkerkant of vraag een nieuw wachtwoord aan.'); |
||
| 130 | return redirect()->to('cart/checkout')->withInput()->withErrors('Dit emailadres is al in gebruik. Je kan links inloggen.', 'register'); |
||
| 131 | } |
||
| 132 | |||
| 133 | $register = ClientService::createAccount($request->all(), config()->get('app.shop_id')); |
||
| 134 | } |
||
| 135 | |||
| 136 | if ($register) { |
||
| 137 | $data = $register; |
||
| 138 | $data['shop'] = app('shop'); |
||
| 139 | |||
| 140 | Mail::send('frontend.email.register-mail', array('password' => $userdata['password'], 'user' => $data->toArray(), 'billAddress' => $data->clientBillAddress->toArray()), function ($message) use ($data) { |
||
| 141 | |||
| 142 | $message->to($data['email'])->from($data['shop']->email, $data['shop']->title)->subject('Je bent geregistreerd.'); |
||
| 143 | }); |
||
| 144 | |||
| 145 | $userdata = array( |
||
| 146 | 'email' => $request->get('email'), |
||
| 147 | 'password' => $request->get('password'), |
||
| 148 | 'confirmed' => 1, |
||
| 149 | 'active' => 1 |
||
| 150 | ); |
||
| 151 | |||
| 152 | auth('web')->attempt($userdata); |
||
| 153 | |||
| 154 | return redirect()->to('cart/checkout')->withErrors('Je bent geregistreerd. Er is een bevestigingsmail gestuurd.', 'login'); |
||
| 155 | } |
||
| 156 | |||
| 157 | Notification::error('Je hebt al een account'); |
||
| 158 | return redirect()->to('cart/checkout')->withErrors(true, 'register')->withInput(); |
||
| 159 | } |
||
| 160 | |||
| 161 | unset($userdata['password']); |
||
| 162 | $registerAttempt = ClientService::validateRegisterNoAccount($userdata, config()->get('app.shop_id')); |
||
| 163 | |||
| 164 | if ($registerAttempt) { |
||
| 165 | $register = ClientService::register($userdata, config()->get('app.shop_id')); |
||
| 166 | $userdata['client_id'] = $register->id; |
||
| 167 | } else { |
||
| 168 | $client = ClientService::findByEmail($userdata['email'], config()->get('app.shop_id')); |
||
| 169 | if ($client) { |
||
| 170 | $userdata['client_id'] = $client->id; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | session()->put('noAccountUser', $userdata); |
||
| 175 | return redirect()->to('cart/checkout'); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function postComplete(Request $request) |
||
| 179 | { |
||
| 180 | $noAccountUser = session()->get('noAccountUser'); |
||
| 181 | if (auth('web')->guest() and !$noAccountUser) { |
||
| 182 | return view('frontend.checkout.login'); |
||
| 183 | } |
||
| 184 | |||
| 185 | if (!Cart::getContent()->count()) { |
||
| 186 | return redirect()->to('cart/checkout'); |
||
| 187 | } |
||
| 188 | |||
| 189 | $data = array( |
||
| 190 | 'products' => Cart::getContent()->toArray(), |
||
| 191 | 'price_with_tax' => Cart::getTotalWithTax(false), |
||
| 192 | 'price_without_tax' => Cart::getTotalWithoutTax(false), |
||
| 193 | 'comments' => $request->get('comments'), |
||
| 194 | 'browser_detect' => serialize(BrowserDetect::toArray()) |
||
| 195 | ); |
||
| 196 | |||
| 197 | if (auth('web')->check()) { |
||
| 198 | $data['user_id'] = auth('web')->user()->id; |
||
| 199 | } |
||
| 200 | |||
| 201 | if ($noAccountUser){ |
||
| 202 | $data['user_id'] = $noAccountUser['client_id']; |
||
| 203 | } |
||
| 204 | |||
| 205 | if(Cart::getConditionsByType('sending_method')->count()) { |
||
| 206 | $data['sending_method'] = Cart::getConditionsByType('sending_method'); |
||
| 207 | } |
||
| 208 | |||
| 209 | if(Cart::getConditionsByType('sending_method_country_price')->count()) { |
||
| 210 | $data['sending_method_country_price'] = Cart::getConditionsByType('sending_method_country_price'); |
||
| 211 | } |
||
| 212 | |||
| 213 | if(Cart::getConditionsByType('payment_method')->count()) { |
||
| 214 | $data['payment_method'] = Cart::getConditionsByType('payment_method'); |
||
| 215 | } |
||
| 216 | |||
| 217 | $orderInsertAttempt = OrderService::createOrderFrontend($data, config()->get('app.shop_id'), $noAccountUser); |
||
| 218 | |||
| 219 | if ($orderInsertAttempt AND $orderInsertAttempt->count()) { |
||
| 220 | if ($orderInsertAttempt->OrderPaymentMethod and $orderInsertAttempt->OrderPaymentMethod->paymentMethod->order_confirmed_order_status_id) { |
||
| 221 | $orderStatus = OrderService::updateStatus($orderInsertAttempt->id, $orderInsertAttempt->OrderPaymentMethod->paymentMethod->order_confirmed_order_status_id); |
||
| 222 | if ($orderInsertAttempt->OrderPaymentMethod->paymentMethod->order_confirmed_order_status_id) { |
||
| 223 | Event::fire(new OrderChangeStatus($orderStatus)); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | session()->put('orderData', $orderInsertAttempt); |
||
| 228 | |||
| 229 | if ($orderInsertAttempt->OrderPaymentMethod and $orderInsertAttempt->OrderPaymentMethod->paymentMethod->payment_external) { |
||
| 230 | return redirect()->to('cart/payment'); |
||
| 231 | } |
||
| 232 | |||
| 233 | app('cart')->clear(); |
||
| 234 | app('cart')->clearCartConditions(); |
||
| 235 | session()->flush('noAccountUser'); |
||
| 236 | $body = ""; |
||
| 237 | return view('frontend.checkout.complete')->with(array('body' => $body)); |
||
| 238 | } |
||
| 239 | |||
| 240 | return redirect()->to('cart/checkout'); |
||
| 241 | } |
||
| 242 | |||
| 243 | public function getEditAddress(Request $request, $type) { |
||
| 271 | } |
||
| 272 | |||
| 273 | public function postEditAddress(Request $request, $type) |
||
| 317 | } |
||
| 318 | } |