Total Complexity | 55 |
Total Lines | 301 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 1 | Features | 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) |
||
70 | { |
||
71 | $validateLogin = ClientService::validateLogin($request->all()); |
||
72 | |||
73 | if ($validateLogin->fails()) { |
||
74 | foreach ($validateLogin->errors()->all() as $error) { |
||
75 | Notification::error($error); |
||
76 | } |
||
77 | |||
78 | return redirect()->to('cart/checkout') |
||
79 | ->withErrors(true, 'login')->withInput(); |
||
80 | } |
||
81 | |||
82 | $userdata = array( |
||
83 | 'email' => $request->get('email'), |
||
84 | 'password' => $request->get('password'), |
||
85 | 'confirmed' => 1, |
||
86 | 'active' => 1, |
||
87 | 'shop_id' => config()->get('app.shop_id') |
||
88 | ); |
||
89 | |||
90 | if (auth('web')->attempt($userdata)) { |
||
91 | return redirect()->to('cart/checkout'); |
||
92 | } |
||
93 | |||
94 | Notification::error(trans('message.error.data-is-incorrect')); |
||
95 | return redirect()->to('cart/checkout')->withErrors(true, 'login')->withInput(); |
||
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 ($request->get('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' => $request->get('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 | $userdata = $request->all(); |
||
162 | unset($userdata['password']); |
||
163 | $registerAttempt = ClientService::validateRegisterNoAccount($userdata, config()->get('app.shop_id')); |
||
164 | |||
165 | if ($registerAttempt) { |
||
166 | $register = ClientService::register($userdata, config()->get('app.shop_id')); |
||
167 | $userdata['client_id'] = $register->id; |
||
168 | } else { |
||
169 | $client = ClientService::findByEmail($userdata['email'], config()->get('app.shop_id')); |
||
170 | if ($client) { |
||
171 | $userdata['client_id'] = $client->id; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | session()->put('noAccountUser', $userdata); |
||
176 | return redirect()->to('cart/checkout'); |
||
177 | } |
||
178 | |||
179 | public function postComplete(Request $request) |
||
180 | { |
||
181 | $noAccountUser = session()->get('noAccountUser'); |
||
182 | if (auth('web')->guest() and !$noAccountUser) { |
||
183 | return view('frontend.checkout.login'); |
||
184 | } |
||
185 | |||
186 | if (!Cart::getContent()->count()) { |
||
187 | return redirect()->to('cart/checkout'); |
||
188 | } |
||
189 | |||
190 | $data = array( |
||
191 | 'products' => Cart::getContent()->toArray(), |
||
192 | 'price_with_tax' => Cart::getTotalWithTax(false), |
||
193 | 'price_without_tax' => Cart::getTotalWithoutTax(false), |
||
194 | 'comments' => $request->get('comments'), |
||
195 | 'browser_detect' => serialize(BrowserDetect::toArray()) |
||
196 | ); |
||
197 | |||
198 | if (auth('web')->check()) { |
||
199 | $data['user_id'] = auth('web')->user()->id; |
||
200 | } |
||
201 | |||
202 | if ($noAccountUser){ |
||
203 | $data['user_id'] = $noAccountUser['client_id']; |
||
204 | } |
||
205 | |||
206 | if(Cart::getConditionsByType('sending_method')->count()) { |
||
207 | $data['sending_method'] = Cart::getConditionsByType('sending_method'); |
||
208 | } |
||
209 | |||
210 | if(Cart::getConditionsByType('sending_method_country_price')->count()) { |
||
211 | $data['sending_method_country_price'] = Cart::getConditionsByType('sending_method_country_price'); |
||
212 | } |
||
213 | |||
214 | if(Cart::getConditionsByType('payment_method')->count()) { |
||
215 | $data['payment_method'] = Cart::getConditionsByType('payment_method'); |
||
216 | } |
||
217 | |||
218 | $orderInsertAttempt = OrderService::createOrderFrontend($data, config()->get('app.shop_id'), $noAccountUser); |
||
219 | |||
220 | if ($orderInsertAttempt AND $orderInsertAttempt->count()) { |
||
221 | if ($orderInsertAttempt->OrderPaymentMethod and $orderInsertAttempt->OrderPaymentMethod->paymentMethod->order_confirmed_order_status_id) { |
||
222 | $orderStatus = OrderService::updateStatus($orderInsertAttempt->id, $orderInsertAttempt->OrderPaymentMethod->paymentMethod->order_confirmed_order_status_id); |
||
223 | if ($orderInsertAttempt->OrderPaymentMethod->paymentMethod->order_confirmed_order_status_id) { |
||
224 | Event::dispatch(new OrderChangeStatus($orderStatus)); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | session()->put('orderData', $orderInsertAttempt); |
||
229 | |||
230 | if ($orderInsertAttempt->OrderPaymentMethod and $orderInsertAttempt->OrderPaymentMethod->paymentMethod->payment_external) { |
||
231 | return redirect()->to('cart/payment'); |
||
232 | } |
||
233 | |||
234 | app('cart')->clear(); |
||
235 | app('cart')->clearCartConditions(); |
||
236 | session()->flush('noAccountUser'); |
||
237 | $body = ""; |
||
238 | return view('frontend.checkout.complete')->with(array('body' => $body)); |
||
239 | } |
||
240 | |||
241 | return redirect()->to('cart/checkout'); |
||
242 | } |
||
243 | |||
244 | public function getEditAddress(Request $request, $type) { |
||
272 | } |
||
273 | |||
274 | public function postEditAddress(Request $request, $type) |
||
275 | { |
||
318 | } |
||
319 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths