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