Total Complexity | 44 |
Total Lines | 340 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BaseOrderController 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 BaseOrderController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class BaseOrderController extends ExtendedOrderController |
||
16 | { |
||
17 | protected $sendMail; |
||
18 | |||
19 | public function __construct() |
||
20 | { |
||
21 | $this->middleware('auth'); |
||
22 | $this->middleware('admin'); |
||
23 | } |
||
24 | |||
25 | use UpdateDates; |
||
|
|||
26 | |||
27 | public function getUrl($model, $status, $subscriptionId) |
||
28 | { |
||
29 | $url = ''; |
||
30 | if ($status == 'success') { |
||
31 | if ($subscriptionId) { |
||
32 | $url = '<a href='.url('renew/'.$subscriptionId)." |
||
33 | class='btn btn-sm btn-secondary btn-xs'".tooltip('Renew')."<i class='fas fa-credit-card' |
||
34 | style='color:white;'> </i></a>"; |
||
35 | } |
||
36 | } |
||
37 | |||
38 | return '<p><a href='.url('orders/'.$model->id)." |
||
39 | class='btn btn-sm btn-secondary btn-xs'".tooltip('View')."<i class='fas fa-eye' |
||
40 | style='color:white;'> </i></a> $url</p>"; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * inserting the values to orders table. |
||
45 | * |
||
46 | * @param type $invoiceid |
||
47 | * @param type $order_status |
||
48 | * |
||
49 | * @throws \Exception |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | public function executeOrder($invoiceid, $order_status = 'executed') |
||
54 | { |
||
55 | try { |
||
56 | $invoice_items = $this->invoice_items->where('invoice_id', $invoiceid)->get(); |
||
57 | $user_id = $this->invoice->find($invoiceid)->user_id; |
||
58 | if (count($invoice_items) > 0) { |
||
59 | foreach ($invoice_items as $item) { |
||
60 | if ($item) { |
||
61 | $items = $this->getIfItemPresent($item, $invoiceid, $user_id, $order_status); |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | |||
66 | return 'success'; |
||
67 | } catch (\Exception $ex) { |
||
68 | app('log')->error($ex->getMessage()); |
||
69 | Bugsnag::notifyException($ex); |
||
70 | |||
71 | throw new \Exception($ex->getMessage()); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | public function getIfItemPresent($item, $invoiceid, $user_id, $order_status) |
||
76 | { |
||
77 | try { |
||
78 | $product = $this->product->where('name', $item->product_name)->first()->id; |
||
79 | $version = $this->product->where('name', $item->product_name)->first()->version; |
||
80 | if ($version == null) { |
||
81 | //Get Version from Product Upload Table |
||
82 | $version = $this->product_upload->where('product_id', $product)->pluck('version')->first(); |
||
83 | } |
||
84 | $serial_key = $this->generateSerialKey($product, $item->agents); //Send Product Id and Agents to generate Serial Key |
||
85 | $domain = $item->domain; |
||
86 | $plan_id = $this->plan($item->id); |
||
87 | $order = $this->order->create([ |
||
88 | |||
89 | 'invoice_id' => $invoiceid, |
||
90 | 'invoice_item_id' => $item->id, |
||
91 | 'client' => $user_id, |
||
92 | 'order_status' => $order_status, |
||
93 | 'serial_key' => Crypt::encrypt($serial_key), |
||
94 | 'product' => $product, |
||
95 | 'price_override' => $item->subtotal, |
||
96 | 'qty' => $item->quantity, |
||
97 | 'domain' => $domain, |
||
98 | 'number' => $this->generateNumber(), |
||
99 | ]); |
||
100 | $this->addOrderInvoiceRelation($invoiceid, $order->id); |
||
101 | |||
102 | if ($plan_id != 0) { |
||
103 | $this->addSubscription($order->id, $plan_id, $version, $product, $serial_key); |
||
104 | } |
||
105 | |||
106 | if (emailSendingStatus()) { |
||
107 | $this->sendOrderMail($user_id, $order->id, $item->id); |
||
108 | } |
||
109 | //Update Subscriber To Mailchimp |
||
110 | $mailchimpStatus = StatusSetting::pluck('mailchimp_status')->first(); |
||
111 | if ($mailchimpStatus) { |
||
112 | $this->addtoMailchimp($product, $user_id, $item); |
||
113 | } |
||
114 | } catch (\Exception $ex) { |
||
115 | Bugsnag::notifyException($ex); |
||
116 | app('log')->error($ex->getMessage()); |
||
117 | |||
118 | throw new \Exception($ex->getMessage()); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | public function addToMailchimp($product, $user_id, $item) |
||
123 | { |
||
124 | try { |
||
125 | $mailchimp = new \App\Http\Controllers\Common\MailChimpController(); |
||
126 | $email = User::where('id', $user_id)->pluck('email')->first(); |
||
127 | if ($item->subtotal > 0) { |
||
128 | $r = $mailchimp->updateSubscriberForPaidProduct($email, $product); |
||
129 | } else { |
||
130 | $r = $mailchimp->updateSubscriberForFreeProduct($email, $product); |
||
131 | } |
||
132 | } catch (\Exception $ex) { |
||
133 | return; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * inserting the values to subscription table. |
||
139 | * |
||
140 | * @param int $orderid |
||
141 | * @param int $planid |
||
142 | * @param string $version |
||
143 | * @param int $product |
||
144 | * @param string $serial_key |
||
145 | * |
||
146 | * @throws \Exception |
||
147 | * |
||
148 | * @author Ashutosh Pathak <[email protected]> |
||
149 | */ |
||
150 | public function addSubscription($orderid, $planid, $version, $product, $serial_key) |
||
151 | { |
||
152 | try { |
||
153 | $permissions = LicensePermissionsController::getPermissionsForProduct($product); |
||
154 | if ($version == null) { |
||
155 | $version = ''; |
||
156 | } |
||
157 | $days = $this->plan->where('id', $planid)->first()->days; |
||
158 | $licenseExpiry = $this->getLicenseExpiryDate($permissions['generateLicenseExpiryDate'], $days); |
||
159 | $updatesExpiry = $this->getUpdatesExpiryDate($permissions['generateUpdatesxpiryDate'], $days); |
||
160 | $supportExpiry = $this->getSupportExpiryDate($permissions['generateSupportExpiryDate'], $days); |
||
161 | $user_id = $this->order->find($orderid)->client; |
||
162 | $this->subscription->create(['user_id' => $user_id, |
||
163 | 'plan_id' => $planid, 'order_id' => $orderid, 'update_ends_at' =>$updatesExpiry, 'ends_at' => $licenseExpiry, 'support_ends_at'=>$supportExpiry, 'version'=> $version, 'product_id' =>$product, ]); |
||
164 | |||
165 | $licenseStatus = StatusSetting::pluck('license_status')->first(); |
||
166 | if ($licenseStatus == 1) { |
||
167 | $cont = new \App\Http\Controllers\License\LicenseController(); |
||
168 | $createNewLicense = $cont->createNewLicene($orderid, $product, $user_id, $licenseExpiry, $updatesExpiry, $supportExpiry, $serial_key); |
||
169 | } |
||
170 | } catch (\Exception $ex) { |
||
171 | Bugsnag::notifyException($ex); |
||
172 | app('log')->error($ex->getMessage()); |
||
173 | |||
174 | throw new \Exception('Can not Generate Subscription'); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Get the Expiry Date for License. |
||
180 | * |
||
181 | * @param bool $permissions [Whether Permissons for generating License Expiry Date are there or not] |
||
182 | * @param int $days [No of days that would get addeed to the current date ] |
||
183 | * |
||
184 | * @return string [The final License Expiry date that is generated] |
||
185 | */ |
||
186 | protected function getLicenseExpiryDate(bool $permissions, $days) |
||
187 | { |
||
188 | $ends_at = ''; |
||
189 | if ($days > 0 && $permissions == 1) { |
||
190 | $dt = \Carbon\Carbon::now(); |
||
191 | $ends_at = $dt->addDays($days); |
||
192 | } |
||
193 | |||
194 | return $ends_at; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Get the Expiry Date for Updates. |
||
199 | * |
||
200 | * @param bool $permissions [Whether Permissons for generating Updates Expiry Date are there or not] |
||
201 | * @param int $days [No of days that would get added to the current date ] |
||
202 | * |
||
203 | * @return string [The final Updates Expiry date that is generated] |
||
204 | */ |
||
205 | protected function getUpdatesExpiryDate(bool $permissions, $days) |
||
206 | { |
||
207 | $update_ends_at = ''; |
||
208 | if ($days > 0 && $permissions == 1) { |
||
209 | $dt = \Carbon\Carbon::now(); |
||
210 | $update_ends_at = $dt->addDays($days); |
||
211 | } |
||
212 | |||
213 | return $update_ends_at; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Get the Expiry Date for Support. |
||
218 | * |
||
219 | * @param bool $permissions [Whether Permissons for generating Updates Expiry Date are there or not] |
||
220 | * @param int $days [No of days that would get added to the current date ] |
||
221 | * |
||
222 | * @return string [The final Suport Expiry date that is generated] |
||
223 | */ |
||
224 | protected function getSupportExpiryDate(bool $permissions, $days) |
||
225 | { |
||
226 | $support_ends_at = ''; |
||
227 | if ($days > 0 && $permissions == 1) { |
||
228 | $dt = \Carbon\Carbon::now(); |
||
229 | $support_ends_at = $dt->addDays($days); |
||
230 | } |
||
231 | |||
232 | return $support_ends_at; |
||
233 | } |
||
234 | |||
235 | public function addOrderInvoiceRelation($invoiceid, $orderid) |
||
244 | } |
||
245 | } |
||
246 | |||
247 | public function sendOrderMail($userid, $orderid, $itemid) |
||
273 | } |
||
274 | |||
275 | public function getMail($setting, $user, $downloadurl, $invoiceurl, $order, $product, $orderid, $myaccounturl) |
||
276 | { |
||
277 | try { |
||
278 | $templates = new \App\Model\Common\Template(); |
||
279 | $temp_id = $setting->order_mail; |
||
280 | $template = $templates->where('id', $temp_id)->first(); |
||
281 | $knowledgeBaseUrl = $setting->company_url; |
||
282 | $from = $setting->email; |
||
283 | $to = $user->email; |
||
284 | $adminEmail = $setting->company_email; |
||
285 | $subject = $template->name; |
||
286 | $data = $template->data; |
||
287 | $replace = [ |
||
288 | 'name' => $user->first_name.' '.$user->last_name, |
||
289 | 'serialkeyurl' => $myaccounturl, |
||
290 | 'downloadurl' => $downloadurl, |
||
291 | 'invoiceurl' => $invoiceurl, |
||
292 | 'product' => $product, |
||
293 | 'number' => $order->number, |
||
294 | 'expiry' => $this->expiry($orderid), |
||
295 | 'url' => $this->renew($orderid), |
||
296 | 'knowledge_base'=> $knowledgeBaseUrl, |
||
297 | |||
298 | ]; |
||
299 | $type = ''; |
||
300 | if ($template) { |
||
301 | $type_id = $template->type; |
||
302 | $temp_type = new \App\Model\Common\TemplateType(); |
||
303 | $type = $temp_type->where('id', $type_id)->first()->name; |
||
304 | } |
||
305 | $mail = new \App\Http\Controllers\Common\PhpMailController(); |
||
306 | $mail->sendEmail($from, $to, $data, $subject, $replace, $type); |
||
307 | |||
308 | if ($order->invoice->grand_total) { |
||
309 | SettingsController::sendPaymentSuccessMailtoAdmin($order->invoice->currency, $order->invoice->grand_total, $user, $product); |
||
310 | } |
||
311 | } catch (\Exception $ex) { |
||
312 | throw new \Exception($ex->getMessage()); |
||
313 | } |
||
314 | } |
||
315 | |||
316 | public function invoiceUrl($orderid) |
||
317 | { |
||
318 | $orders = new Order(); |
||
319 | $order = $orders->where('id', $orderid)->first(); |
||
320 | $invoiceid = $order->invoice_id; |
||
321 | $url = url('my-invoice/'.$invoiceid); |
||
322 | |||
323 | return $url; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * get the price of a product by id. |
||
328 | * |
||
329 | * @param type $product_id |
||
330 | * |
||
331 | * @throws \Exception |
||
332 | * |
||
333 | * @return type collection |
||
334 | */ |
||
335 | public function getPrice($product_id) |
||
336 | { |
||
337 | try { |
||
338 | return $this->price->where('product_id', $product_id)->first(); |
||
339 | } catch (\Exception $ex) { |
||
340 | Bugsnag::notifyException($ex); |
||
341 | |||
342 | throw new \Exception($ex->getMessage()); |
||
343 | } |
||
344 | } |
||
345 | |||
346 | public function downloadUrl($userid, $orderid) |
||
355 | } |
||
356 | } |
||
357 |