Total Complexity | 120 |
Total Lines | 760 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Utilities 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 Utilities, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Utilities { |
||
13 | |||
14 | static function setActiveMenu($uri,$isParent = false) |
||
15 | { |
||
16 | $class = ($isParent) ? 'active open' : 'active'; |
||
17 | return \Request::is($uri) ? $class : ''; |
||
18 | //return \Request::is($uri); |
||
19 | } |
||
20 | |||
21 | // Get Setting |
||
22 | static function getSetting($key) |
||
23 | { |
||
24 | $settingValue = Setting::where('key','=',$key)->pluck('value'); |
||
25 | return $settingValue; |
||
26 | } |
||
27 | |||
28 | //get Settings |
||
29 | static function getSettings() |
||
30 | { |
||
31 | $settings = Setting::all(); |
||
32 | $settings_array = array(); |
||
33 | |||
34 | foreach ($settings as $setting) { |
||
35 | $settings_array[$setting->key] = $setting->value; |
||
36 | } |
||
37 | |||
38 | return $settings_array; |
||
39 | } |
||
40 | |||
41 | |||
42 | //Follow up Status |
||
43 | static function getFollowUpStatus($status) |
||
44 | { |
||
45 | switch ($status) { |
||
46 | case '1': |
||
47 | return "Done"; |
||
48 | break; |
||
49 | |||
50 | default : |
||
51 | return "Pending"; |
||
52 | break; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | //Follow up by |
||
57 | static function getFollowupBy($followUpBy) |
||
58 | { |
||
59 | switch ($followUpBy) { |
||
60 | case '1': |
||
61 | return "SMS"; |
||
62 | break; |
||
63 | |||
64 | case '2': |
||
65 | return "Personal"; |
||
66 | break; |
||
67 | |||
68 | default : |
||
69 | return "Call"; |
||
70 | break; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | //FollowUp Status Icon bg |
||
75 | static function getIconBg($status) |
||
85 | } |
||
86 | } |
||
87 | |||
88 | |||
89 | |||
90 | //Followup Status Icon |
||
91 | static function getStatusIcon($status) |
||
92 | { |
||
93 | switch ($status) { |
||
94 | case '1': |
||
95 | return "fa fa-thumbs-up"; |
||
96 | break; |
||
97 | |||
98 | default : |
||
99 | return "fa fa-refresh"; |
||
100 | break; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | |||
105 | // Aim for member & enquiry creation |
||
106 | static function getAim($aim) |
||
107 | { |
||
108 | switch ($aim) { |
||
109 | case '1': |
||
110 | return "Networking"; |
||
111 | break; |
||
112 | |||
113 | case '2': |
||
114 | return "Body Building"; |
||
115 | break; |
||
116 | |||
117 | case '3': |
||
118 | return "Fatloss"; |
||
119 | break; |
||
120 | |||
121 | case '4': |
||
122 | return "Weightgain"; |
||
123 | break; |
||
124 | |||
125 | case '5': |
||
126 | return "Others"; |
||
127 | break; |
||
128 | |||
129 | default : |
||
130 | return "Fitness"; |
||
131 | break; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | |||
136 | // Invoice Labels |
||
137 | static function getInvoiceLabel($status) |
||
138 | { |
||
139 | switch ($status) { |
||
140 | case '0': |
||
141 | return "label label-danger"; |
||
142 | break; |
||
143 | |||
144 | case '1': |
||
145 | return "label label-success"; |
||
146 | break; |
||
147 | |||
148 | case '3': |
||
149 | return "label label-default"; |
||
150 | break; |
||
151 | |||
152 | default : |
||
153 | return "label label-primary"; |
||
154 | break; |
||
155 | } |
||
156 | } |
||
157 | |||
158 | // Expense alert repeat |
||
159 | static function expenseRepeatIntervel($repeat) |
||
160 | { |
||
161 | switch ($repeat) { |
||
162 | case '0': |
||
163 | return "Never Repeat"; |
||
164 | break; |
||
165 | |||
166 | case '1': |
||
167 | return "Every Day"; |
||
168 | break; |
||
169 | |||
170 | case '2': |
||
171 | return "Every Week"; |
||
172 | break; |
||
173 | |||
174 | case '3': |
||
175 | return "Every Month"; |
||
176 | break; |
||
177 | |||
178 | default : |
||
179 | return "Every Year"; |
||
180 | break; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | //Paid Unpaid Labels |
||
185 | static function getPaidUnpaid($status) |
||
186 | { |
||
187 | switch ($status) { |
||
188 | case '0': |
||
189 | return "label label-danger"; |
||
190 | break; |
||
191 | |||
192 | default : |
||
193 | return "label label-primary"; |
||
194 | break; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | //Active-Inactive Labels |
||
199 | static function getActiveInactive($status) |
||
209 | } |
||
210 | } |
||
211 | |||
212 | |||
213 | |||
214 | // Occupation of members |
||
215 | static function getOccupation($occupation) |
||
216 | { |
||
217 | switch ($occupation) { |
||
218 | case '1': |
||
219 | return "Housewife"; |
||
220 | break; |
||
221 | |||
222 | case '2': |
||
223 | return "Self Employed"; |
||
224 | break; |
||
225 | |||
226 | case '3': |
||
227 | return "Professional"; |
||
228 | break; |
||
229 | |||
230 | case '4': |
||
231 | return "Freelancer"; |
||
232 | break; |
||
233 | |||
234 | case '5': |
||
235 | return "Others"; |
||
236 | break; |
||
237 | |||
238 | default : |
||
239 | return "Student"; |
||
240 | break; |
||
241 | } |
||
242 | } |
||
243 | |||
244 | |||
245 | |||
246 | // Source for member & enquiry creation |
||
247 | static function getSource($source) |
||
261 | } |
||
262 | } |
||
263 | |||
264 | |||
265 | // Member Status |
||
266 | static function getStatusValue($status) |
||
267 | { |
||
268 | switch ($status) { |
||
269 | case '0': |
||
270 | return "Inactive"; |
||
271 | break; |
||
272 | |||
273 | case '2': |
||
274 | return "Archived"; |
||
275 | break; |
||
276 | |||
277 | default : |
||
278 | return "Active"; |
||
279 | break; |
||
280 | } |
||
281 | } |
||
282 | |||
283 | // Enquiry Status |
||
284 | static function getEnquiryStatus($status) |
||
285 | { |
||
286 | switch ($status) { |
||
287 | case '0': |
||
288 | return "Lost"; |
||
289 | break; |
||
290 | |||
291 | case '2': |
||
292 | return "Member"; |
||
293 | break; |
||
294 | |||
295 | default : |
||
296 | return "Lead"; |
||
297 | break; |
||
298 | } |
||
299 | } |
||
300 | |||
301 | // Enquiry Label |
||
302 | static function getEnquiryLabel($status) |
||
303 | { |
||
304 | switch ($status) { |
||
305 | case '0': |
||
306 | return "label label-danger"; |
||
307 | break; |
||
308 | |||
309 | case '2': |
||
310 | return "label label-success"; |
||
311 | break; |
||
312 | |||
313 | default : |
||
314 | return "label label-primary"; |
||
315 | break; |
||
316 | } |
||
317 | } |
||
318 | |||
319 | // Set invoice status |
||
320 | static function setInvoiceStatus($amount_due,$invoice_total) |
||
321 | { |
||
322 | if($amount_due == 0) |
||
323 | { |
||
324 | $paymentStatus = \constPaymentStatus::Paid; |
||
325 | } |
||
326 | elseif($amount_due > 0 && $amount_due < $invoice_total) |
||
327 | { |
||
328 | $paymentStatus = \constPaymentStatus::Partial; |
||
329 | } |
||
330 | elseif($amount_due == $invoice_total) |
||
331 | { |
||
332 | $paymentStatus = \constPaymentStatus::Unpaid; |
||
333 | } |
||
334 | else |
||
335 | { |
||
336 | $paymentStatus = \constPaymentStatus::Overpaid; |
||
337 | } |
||
338 | return $paymentStatus; |
||
339 | } |
||
340 | |||
341 | |||
342 | // Invoice Status |
||
343 | static function getInvoiceStatus($status) |
||
344 | { |
||
345 | switch ($status) { |
||
346 | case '1': |
||
347 | return "Paid"; |
||
348 | break; |
||
349 | |||
350 | case '2': |
||
351 | return "Partial"; |
||
352 | break; |
||
353 | |||
354 | case '3': |
||
355 | return "Overpaid"; |
||
356 | break; |
||
357 | |||
358 | default : |
||
359 | return "Unpaid"; |
||
360 | break; |
||
361 | } |
||
362 | } |
||
363 | |||
364 | |||
365 | // Subcription Status |
||
366 | static function getSubscriptionStatus($status) |
||
367 | { |
||
368 | switch ($status) { |
||
369 | case '0': |
||
370 | return "Expired"; |
||
371 | break; |
||
372 | |||
373 | case '2': |
||
374 | return "Renewed"; |
||
375 | break; |
||
376 | |||
377 | case '3': |
||
378 | return "Cancelled"; |
||
379 | break; |
||
380 | |||
381 | default : |
||
382 | return "OnGoing"; |
||
383 | break; |
||
384 | } |
||
385 | } |
||
386 | |||
387 | |||
388 | // Subcription Label |
||
389 | static function getSubscriptionLabel($status) |
||
390 | { |
||
391 | switch ($status) { |
||
392 | case '0': |
||
393 | return "label label-danger"; |
||
394 | break; |
||
395 | |||
396 | case '2': |
||
397 | return "label label-success"; |
||
398 | break; |
||
399 | |||
400 | case '3': |
||
401 | return "label label-default"; |
||
402 | break; |
||
403 | |||
404 | default : |
||
405 | return "label label-primary"; |
||
406 | break; |
||
407 | } |
||
408 | } |
||
409 | |||
410 | |||
411 | // Payment Mode |
||
412 | static function getPaymentMode($status) |
||
413 | { |
||
414 | switch ($status) { |
||
415 | case '0': |
||
416 | return "Cheque"; |
||
417 | break; |
||
418 | |||
419 | default : |
||
420 | return "Cash"; |
||
421 | break; |
||
422 | } |
||
423 | } |
||
424 | |||
425 | // Cheque status |
||
426 | static function getChequeStatus($status) |
||
427 | { |
||
428 | switch ($status) { |
||
429 | case '1': |
||
430 | return "Deposited"; |
||
431 | break; |
||
432 | |||
433 | case '2': |
||
434 | return "Cleared"; |
||
435 | break; |
||
436 | |||
437 | case '3': |
||
438 | return "Bounced"; |
||
439 | break; |
||
440 | |||
441 | case '4': |
||
442 | return "Reissued"; |
||
443 | break; |
||
444 | |||
445 | default : |
||
446 | return "Recieved"; |
||
447 | break; |
||
448 | } |
||
449 | } |
||
450 | |||
451 | // Get Gender |
||
452 | static function getGender($gender) |
||
453 | { |
||
454 | switch ($gender) |
||
455 | { |
||
456 | case 'm': |
||
457 | return "Male"; |
||
458 | break; |
||
459 | |||
460 | case 'f': |
||
461 | return "Female"; |
||
462 | break; |
||
463 | } |
||
464 | } |
||
465 | |||
466 | |||
467 | //Get invoice display name type |
||
468 | static function getDisplay($display) |
||
469 | { |
||
470 | switch ($display) |
||
471 | { |
||
472 | case 'gym_logo': |
||
473 | return "Gym Logo"; |
||
474 | break; |
||
475 | |||
476 | default: |
||
477 | return "Gym Name"; |
||
478 | break; |
||
479 | } |
||
480 | } |
||
481 | |||
482 | |||
483 | // Get Numbering mode |
||
484 | static function getMode($mode) |
||
485 | { |
||
486 | switch ($mode) |
||
487 | { |
||
488 | case '0': |
||
489 | return "Manual"; |
||
490 | break; |
||
491 | |||
492 | default : |
||
493 | return "Automatic"; |
||
494 | break; |
||
495 | } |
||
496 | } |
||
497 | |||
498 | static function getGreeting(){ |
||
499 | //$time = date("H"); |
||
500 | $time = Carbon::now()->hour; |
||
501 | /* If the time is less than 1200 hours, show good morning */ |
||
502 | if ($time < "12") { |
||
503 | echo "Good morning"; |
||
504 | } else |
||
505 | /* If the time is grater than or equal to 1200 hours, but less than 1700 hours, so good afternoon */ |
||
506 | if ($time >= "12" && $time < "17") { |
||
507 | echo "Good afternoon"; |
||
508 | } else |
||
509 | /* Should the time be between or equal to 1700 and 1900 hours, show good evening */ |
||
510 | if ($time >= "17" && $time < "22") { |
||
511 | echo "Good evening"; |
||
512 | } else |
||
513 | /* Finally, show good night if the time is greater than or equal to 2200 hours */ |
||
514 | if ($time >= "22") { |
||
515 | echo "Good night"; |
||
516 | } |
||
517 | } |
||
518 | |||
519 | |||
520 | /** |
||
521 | *File Upload |
||
522 | **/ |
||
523 | |||
524 | static function uploadFile(Request $request, $prefix, $recordId, $upload_field, $upload_path) |
||
539 | } |
||
540 | } |
||
541 | } |
||
542 | |||
543 | static function registrationsTrend() |
||
544 | { |
||
545 | // Get Financial date |
||
546 | $startDate = new Carbon(Setting::where('key','=','financial_start')->pluck('value')); |
||
547 | $data = array(); |
||
548 | |||
549 | for ($i=1; $i <= 12 ; $i++) { |
||
550 | //$members = member::registrations($startDate->month,$startDate->year); // Laravel Scoped Query Issue: Workaroud Needed |
||
551 | $members = Member::whereMonth('created_at', '=', $startDate->month)->whereYear('created_at', '=',$startDate->year)->count(); |
||
552 | $data[] = array("month" => $startDate->format('Y-m'), "registrations" => $members); |
||
553 | $startDate->addMonth(); |
||
554 | } |
||
555 | |||
556 | return json_encode($data); |
||
557 | } |
||
558 | |||
559 | |||
560 | static function membersPerPlan() |
||
561 | { |
||
562 | $data = array(); |
||
563 | |||
564 | $plans = Plan::onlyActive()->get(); |
||
565 | |||
566 | foreach ($plans as $plan) |
||
567 | { |
||
568 | $subscriptions = Subscription::where('status','=',\constSubscription::onGoing)->where('plan_id','=',$plan->id)->count(); |
||
569 | $data[] = array('label' =>$plan->plan_name ,'value'=>$subscriptions); |
||
570 | } |
||
571 | return json_encode($data); |
||
572 | } |
||
573 | |||
574 | // Checking logonutility gateway status |
||
575 | static function smsGatewayStatus() |
||
576 | { |
||
577 | try |
||
578 | { |
||
579 | $api_key = Utilities::getSetting('sms_api_key'); |
||
580 | |||
581 | $api_url = "http://logonutility.in/app/miscapi/".$api_key."/getBalance/true/"; |
||
582 | |||
583 | if(Utilities::isDomainAvailible($api_url)) |
||
584 | { |
||
585 | return true; |
||
586 | } |
||
587 | else |
||
588 | { |
||
589 | return false; |
||
590 | } |
||
591 | |||
592 | } |
||
593 | catch(Exception $e) |
||
594 | { |
||
595 | return false; |
||
596 | } |
||
597 | } |
||
598 | |||
599 | |||
600 | // returns true, if domain is availible, false if not |
||
601 | static function isDomainAvailible($domain) |
||
602 | { |
||
603 | //check, if a valid url is provided |
||
604 | if(!filter_var($domain, FILTER_VALIDATE_URL)) |
||
605 | { |
||
606 | return false; |
||
607 | } |
||
608 | |||
609 | //initialize curl |
||
610 | $curlInit = curl_init($domain); |
||
611 | curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10); |
||
612 | curl_setopt($curlInit,CURLOPT_HEADER,true); |
||
613 | curl_setopt($curlInit,CURLOPT_NOBODY,true); |
||
614 | curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true); |
||
615 | |||
616 | //get answer |
||
617 | $response = curl_exec($curlInit); |
||
618 | |||
619 | curl_close($curlInit); |
||
620 | |||
621 | if ($response) return true; |
||
622 | |||
623 | return false; |
||
624 | } |
||
625 | |||
626 | |||
627 | static function Sms($sender_id,$member_contact,$sms_text,$sms_status) |
||
674 | } |
||
675 | |||
676 | } |
||
677 | } |
||
678 | |||
679 | static function retrySms($sender_id,$member_contact,$sms_text,$log) |
||
680 | { |
||
681 | $gatewayStatus = Utilities::smsGatewayStatus(); |
||
682 | |||
683 | if($gatewayStatus) |
||
684 | { |
||
685 | $api_key = Utilities::getSetting('sms_api_key'); |
||
686 | $contacts = $member_contact; |
||
687 | $from = $sender_id; |
||
688 | $send_sms_text = urlencode($sms_text); |
||
689 | |||
690 | //Transactional = 20 , Promo with sender id = 21 , only promotional = 18 |
||
691 | $api_url = "http://www.logonutility.in/app/smsapi/index.php?key=".$api_key."&campaign=1&routeid=21&type=text&contacts=".$contacts."&senderid=".$from."&msg=".$send_sms_text; |
||
692 | |||
693 | //Submit to server |
||
694 | $response = file_get_contents($api_url); |
||
695 | |||
696 | if (str_contains($response,'SMS-SHOOT-ID')) |
||
697 | { |
||
698 | //Log entry for SMS_log table |
||
699 | $log->update(['shoot_id' => substr($response, strpos($response, "SMS-SHOOT-ID/") + 13), |
||
700 | 'number' => $member_contact, |
||
701 | 'message' => $sms_text, |
||
702 | 'sender_id' => $sender_id, |
||
703 | 'send_time' => Carbon::now(), |
||
704 | 'status' => 'NA']); |
||
705 | $log->save(); |
||
706 | } |
||
707 | //Update SMS balance |
||
708 | Utilities::smsBalance(); |
||
709 | } |
||
710 | } |
||
711 | |||
712 | static function smsBalance() |
||
733 | } |
||
734 | } |
||
735 | } |
||
736 | |||
737 | static function smsStatusUpdate() |
||
772 | } |
||
773 | } |
||
779 | ?> |
||
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: