Conditions | 5 |
Paths | 6 |
Total Lines | 64 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
13 | public function handleWebhook() |
||
|
|||
14 | { |
||
15 | $data = $_POST; |
||
16 | $mac_provided = $data['mac']; |
||
17 | unset($data['mac']); |
||
18 | $ver = explode('.', phpversion()); |
||
19 | $major = (int) $ver[0]; |
||
20 | $minor = (int) $ver[1]; |
||
21 | if($major >= 5 and $minor >= 4){ |
||
22 | ksort($data, SORT_STRING | SORT_FLAG_CASE); |
||
23 | } |
||
24 | else{ |
||
25 | uksort($data, 'strcasecmp'); |
||
26 | } |
||
27 | |||
28 | $mac_calculated = hash_hmac("sha1", implode("|", $data), config('laravelmojo.salt')); |
||
29 | if($mac_provided == $mac_calculated){ |
||
30 | if($data['status'] == "Credit"){ |
||
31 | |||
32 | $user = User::where('email',$data['buyer'])->first(); |
||
33 | $user_id = $user->id; |
||
34 | MojoPaymentDetails::create(['user_id' => $user_id, |
||
35 | 'buyer_email' => $data['buyer'], |
||
36 | 'buyer_name' => $data['buyer_name'], |
||
37 | 'buyer_phone' => $data['buyer_phone'], |
||
38 | 'currency' => $data['currency'], |
||
39 | 'amount' => $data['amount'], |
||
40 | 'fees' => $data['fees'], |
||
41 | 'longurl' => $data['longurl'], |
||
42 | 'payment_id' => $data['payment_id'], |
||
43 | 'payment_request_id' => $data['payment_request_id'], |
||
44 | 'purpose' => $data['purpose'], |
||
45 | 'shorturl' => $data['shorturl'], |
||
46 | 'request_status' => 'completed', |
||
47 | 'payment_status' => $data['status'], |
||
48 | ]); |
||
49 | } |
||
50 | |||
51 | else{ |
||
52 | |||
53 | $user = User::where('email',$data['buyer'])->first(); |
||
54 | $user_id = $user->id; |
||
55 | MojoPaymentDetails::create(['user_id' => $user_id, |
||
56 | 'buyer_email' => $data['buyer'], |
||
57 | 'buyer_name' => $data['buyer_name'], |
||
58 | 'buyer_phone' => $data['buyer_phone'], |
||
59 | 'currency' => $data['currency'], |
||
60 | 'amount' => $data['amount'], |
||
61 | 'fees' => $data['fees'], |
||
62 | 'longurl' => $data['longurl'], |
||
63 | 'payment_id' => $data['payment_id'], |
||
64 | 'payment_request_id' => $data['payment_request_id'], |
||
65 | 'purpose' => $data['purpose'], |
||
66 | 'shorturl' => $data['shorturl'], |
||
67 | 'request_status' => 'completed', |
||
68 | 'payment_status' => $data['status'], |
||
69 | ]); |
||
70 | |||
71 | } |
||
72 | } |
||
73 | else{ |
||
74 | echo "MAC mismatch"; |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: