Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Paystack 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Paystack, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Paystack |
||
20 | { |
||
21 | /** |
||
22 | * Transaction Verification Successful |
||
23 | */ |
||
24 | const VS = 'Verification successful'; |
||
25 | |||
26 | /** |
||
27 | * Invalid Transaction reference |
||
28 | */ |
||
29 | const ITF = "Invalid transaction reference"; |
||
30 | |||
31 | /** |
||
32 | * Issue Secret Key from your Paystack Dashboard |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $secretKey; |
||
36 | |||
37 | /** |
||
38 | * Instance of Client |
||
39 | * @var Client |
||
40 | */ |
||
41 | protected $client; |
||
42 | |||
43 | /** |
||
44 | * Response from requests made to Paystack |
||
45 | * @var mixed |
||
46 | */ |
||
47 | protected $response; |
||
48 | |||
49 | /** |
||
50 | * Paystack API base Url |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $baseUrl; |
||
54 | |||
55 | /** |
||
56 | * Authorization Url - Paystack payment page |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $authorizationUrl; |
||
60 | |||
61 | public function __construct() |
||
67 | |||
68 | /** |
||
69 | * Get Base Url from Paystack config file |
||
70 | */ |
||
71 | public function setBaseUrl() |
||
75 | |||
76 | /** |
||
77 | * Get secret key from Paystack config file |
||
78 | */ |
||
79 | public function setKey() |
||
83 | |||
84 | /** |
||
85 | * Set options for making the Client request |
||
86 | */ |
||
87 | private function setRequestOptions() |
||
102 | |||
103 | |||
104 | /** |
||
105 | |||
106 | * Initiate a payment request to Paystack |
||
107 | * Included the option to pass the payload to this method for situations |
||
108 | * when the payload is built on the fly (not passed to the controller from a view) |
||
109 | * @return Paystack |
||
110 | */ |
||
111 | |||
112 | public function makePaymentRequest( $data = null) |
||
147 | |||
148 | |||
149 | /** |
||
150 | * @param string $relativeUrl |
||
151 | * @param string $method |
||
152 | * @param array $body |
||
153 | * @return Paystack |
||
154 | * @throws IsNullException |
||
155 | */ |
||
156 | private function setHttpResponse($relativeUrl, $method, $body = []) |
||
169 | |||
170 | /** |
||
171 | * Get the authorization url from the callback response |
||
172 | * @return Paystack |
||
173 | */ |
||
174 | public function getAuthorizationUrl() |
||
182 | |||
183 | /** |
||
184 | * Get the authorization callback response |
||
185 | * In situations where Laravel serves as an backend for a detached UI, the api cannot redirect |
||
186 | * and might need to take different actions based on the success or not of the transaction |
||
187 | * @return array |
||
188 | */ |
||
189 | public function getAuthorizationResponse($data) |
||
197 | |||
198 | /** |
||
199 | * Hit Paystack Gateway to Verify that the transaction is valid |
||
200 | */ |
||
201 | private function verifyTransactionAtGateway() |
||
209 | |||
210 | /** |
||
211 | * True or false condition whether the transaction is verified |
||
212 | * @return boolean |
||
213 | */ |
||
214 | public function isTransactionVerificationValid() |
||
234 | |||
235 | /** |
||
236 | * Get Payment details if the transaction was verified successfully |
||
237 | * @return json |
||
238 | * @throws PaymentVerificationFailedException |
||
239 | */ |
||
240 | public function getPaymentData() |
||
248 | |||
249 | /** |
||
250 | * Fluent method to redirect to Paystack Payment Page |
||
251 | */ |
||
252 | public function redirectNow() |
||
256 | |||
257 | /** |
||
258 | * Get Access code from transaction callback respose |
||
259 | * @return string |
||
260 | */ |
||
261 | public function getAccessCode() |
||
265 | |||
266 | /** |
||
267 | * Generate a Unique Transaction Reference |
||
268 | * @return string |
||
269 | */ |
||
270 | public function genTranxRef() |
||
274 | |||
275 | /** |
||
276 | * Get all the customers that have made transactions on your platform |
||
277 | * @return array |
||
278 | */ |
||
279 | public function getAllCustomers() |
||
285 | |||
286 | /** |
||
287 | * Get all the plans that you have on Paystack |
||
288 | * @return array |
||
289 | */ |
||
290 | public function getAllPlans() |
||
296 | |||
297 | /** |
||
298 | * Get all the transactions that have happened overtime |
||
299 | * @return array |
||
300 | */ |
||
301 | public function getAllTransactions() |
||
307 | |||
308 | /** |
||
309 | * Get the whole response from a get operation |
||
310 | * @return array |
||
311 | */ |
||
312 | private function getResponse() |
||
316 | |||
317 | /** |
||
318 | * Get the data response from a get operation |
||
319 | * @return array |
||
320 | */ |
||
321 | private function getData() |
||
325 | |||
326 | /** |
||
327 | * Create a plan |
||
328 | */ |
||
329 | View Code Duplication | public function createPlan() |
|
346 | |||
347 | /** |
||
348 | * Fetch any plan based on its plan id or code |
||
349 | * @param $plan_code |
||
350 | * @return array |
||
351 | */ |
||
352 | public function fetchPlan($plan_code) |
||
357 | |||
358 | /** |
||
359 | * Update any plan's details based on its id or code |
||
360 | * @param $plan_code |
||
361 | * @return array |
||
362 | */ |
||
363 | View Code Duplication | public function updatePlan($plan_code) |
|
378 | |||
379 | /** |
||
380 | * Create a customer |
||
381 | */ |
||
382 | View Code Duplication | public function createCustomer() |
|
396 | |||
397 | /** |
||
398 | * Fetch a customer based on id or code |
||
399 | * @param $customer_id |
||
400 | * @return array |
||
401 | */ |
||
402 | public function fetchCustomer($customer_id) |
||
407 | |||
408 | /** |
||
409 | * Update a customer's details based on their id or code |
||
410 | * @param $customer_id |
||
411 | * @return array |
||
412 | */ |
||
413 | View Code Duplication | public function updateCustomer($customer_id) |
|
427 | |||
428 | /** |
||
429 | * Export transactions in .CSV |
||
430 | * @return array |
||
431 | */ |
||
432 | public function exportTransactions() |
||
443 | |||
444 | /** |
||
445 | * Create a subscription to a plan from a customer. |
||
446 | */ |
||
447 | View Code Duplication | public function createSubscription() |
|
458 | |||
459 | /** |
||
460 | * Get all the subscriptions made on Paystack. |
||
461 | * |
||
462 | * @return array |
||
463 | */ |
||
464 | public function getAllSubscriptions() |
||
470 | |||
471 | /** |
||
472 | * Get customer subscriptions |
||
473 | * |
||
474 | * @param integer $customer_id |
||
475 | * @return array |
||
476 | */ |
||
477 | public function getCustomerSubscriptions($customer_id) |
||
483 | |||
484 | /** |
||
485 | * Get plan subscriptions |
||
486 | * |
||
487 | * @param integer $plan_id |
||
488 | * @return array |
||
489 | */ |
||
490 | public function getPlanSubscriptions($plan_id) |
||
496 | |||
497 | /** |
||
498 | * Enable a subscription using the subscription code and token |
||
499 | * @return array |
||
500 | */ |
||
501 | View Code Duplication | public function enableSubscription() |
|
511 | |||
512 | /** |
||
513 | * Disable a subscription using the subscription code and token |
||
514 | * @return array |
||
515 | */ |
||
516 | View Code Duplication | public function disableSubscription() |
|
526 | |||
527 | /** |
||
528 | * Fetch details about a certain subscription |
||
529 | * @param mixed $subscription_id |
||
530 | * @return array |
||
531 | */ |
||
532 | public function fetchSubscription($subscription_id) |
||
537 | |||
538 | /** |
||
539 | * Create pages you can share with users using the returned slug |
||
540 | */ |
||
541 | View Code Duplication | public function createPage() |
|
552 | |||
553 | /** |
||
554 | * Fetches all the pages the merchant has |
||
555 | * @return array |
||
556 | */ |
||
557 | public function getAllPages() |
||
562 | |||
563 | /** |
||
564 | * Fetch details about a certain page using its id or slug |
||
565 | * @param mixed $page_id |
||
566 | * @return array |
||
567 | */ |
||
568 | public function fetchPage($page_id) |
||
573 | |||
574 | /** |
||
575 | * Update the details about a particular page |
||
576 | * @param $page_id |
||
577 | * @return array |
||
578 | */ |
||
579 | View Code Duplication | public function updatePage($page_id) |
|
590 | |||
591 | /** |
||
592 | * Creates a subaccount to be used for split payments . Required params are business_name , settlement_bank , account_number , percentage_charge |
||
593 | * |
||
594 | * @return array |
||
595 | */ |
||
596 | |||
597 | View Code Duplication | public function createSubAccount(){ |
|
614 | |||
615 | /** |
||
616 | * Fetches details of a subaccount |
||
617 | * @param subaccount code |
||
618 | * @return array |
||
619 | */ |
||
620 | public function fetchSubAccount($subaccount_code){ |
||
626 | |||
627 | /** |
||
628 | * Lists all the subaccounts associated with the account |
||
629 | * @param $per_page - Specifies how many records to retrieve per page , $page - SPecifies exactly what page to retrieve |
||
630 | * @return array |
||
631 | */ |
||
632 | public function listSubAccounts($per_page,$page){ |
||
638 | |||
639 | |||
640 | /** |
||
641 | * Updates a subaccount to be used for split payments . Required params are business_name , settlement_bank , account_number , percentage_charge |
||
642 | * @param subaccount code |
||
643 | * @return array |
||
644 | */ |
||
645 | |||
646 | View Code Duplication | public function updateSubAccount($subaccount_code){ |
|
664 | } |
||
665 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: