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( $data = null ) |
|
348 | |||
349 | /** |
||
350 | * Fetch any plan based on its plan id or code |
||
351 | * @param $plan_code |
||
352 | * @return array |
||
353 | */ |
||
354 | public function fetchPlan($plan_code) |
||
359 | |||
360 | /** |
||
361 | * Update any plan's details based on its id or code |
||
362 | * @param $plan_code |
||
363 | * @return array |
||
364 | */ |
||
365 | View Code Duplication | public function updatePlan($plan_code, $data = null) |
|
382 | |||
383 | /** |
||
384 | * Create a customer |
||
385 | */ |
||
386 | View Code Duplication | public function createCustomer( $data = null ) |
|
401 | |||
402 | /** |
||
403 | * Fetch a customer based on id or code |
||
404 | * @param $customer_id |
||
405 | * @return array |
||
406 | */ |
||
407 | public function fetchCustomer($customer_id) |
||
412 | |||
413 | /** |
||
414 | * Update a customer's details based on their id or code |
||
415 | * @param $customer_id |
||
416 | * @return array |
||
417 | */ |
||
418 | View Code Duplication | public function updateCustomer($customer_id, $data = null) |
|
434 | |||
435 | /** |
||
436 | * Export transactions in .CSV |
||
437 | * @return array |
||
438 | */ |
||
439 | View Code Duplication | public function exportTransactions( $data = null ) |
|
452 | |||
453 | /** |
||
454 | * Create a subscription to a plan from a customer. |
||
455 | */ |
||
456 | View Code Duplication | public function createSubscription() |
|
467 | |||
468 | /** |
||
469 | * Get all the subscriptions made on Paystack. |
||
470 | * |
||
471 | * @return array |
||
472 | */ |
||
473 | public function getAllSubscriptions() |
||
479 | |||
480 | /** |
||
481 | * Get customer subscriptions |
||
482 | * |
||
483 | * @param integer $customer_id |
||
484 | * @return array |
||
485 | */ |
||
486 | public function getCustomerSubscriptions($customer_id) |
||
492 | |||
493 | /** |
||
494 | * Get plan subscriptions |
||
495 | * |
||
496 | * @param integer $plan_id |
||
497 | * @return array |
||
498 | */ |
||
499 | public function getPlanSubscriptions($plan_id) |
||
505 | |||
506 | /** |
||
507 | * Enable a subscription using the subscription code and token |
||
508 | * @return array |
||
509 | */ |
||
510 | View Code Duplication | public function enableSubscription( $data = null ) |
|
522 | |||
523 | /** |
||
524 | * Disable a subscription using the subscription code and token |
||
525 | * @return array |
||
526 | */ |
||
527 | View Code Duplication | public function disableSubscription() |
|
537 | |||
538 | /** |
||
539 | * Fetch details about a certain subscription |
||
540 | * @param mixed $subscription_id |
||
541 | * @return array |
||
542 | */ |
||
543 | public function fetchSubscription($subscription_id) |
||
548 | |||
549 | /** |
||
550 | * Create pages you can share with users using the returned slug |
||
551 | */ |
||
552 | View Code Duplication | public function createPage() |
|
563 | |||
564 | /** |
||
565 | * Fetches all the pages the merchant has |
||
566 | * @return array |
||
567 | */ |
||
568 | public function getAllPages() |
||
573 | |||
574 | /** |
||
575 | * Fetch details about a certain page using its id or slug |
||
576 | * @param mixed $page_id |
||
577 | * @return array |
||
578 | */ |
||
579 | public function fetchPage($page_id) |
||
584 | |||
585 | /** |
||
586 | * Update the details about a particular page |
||
587 | * @param $page_id |
||
588 | * @return array |
||
589 | */ |
||
590 | View Code Duplication | public function updatePage($page_id) |
|
601 | |||
602 | /** |
||
603 | * Creates a subaccount to be used for split payments . Required params are business_name , settlement_bank , account_number , percentage_charge |
||
604 | * |
||
605 | * @return array |
||
606 | */ |
||
607 | |||
608 | View Code Duplication | public function createSubAccount( $data = null ){ |
|
629 | |||
630 | /** |
||
631 | * Fetches details of a subaccount |
||
632 | * @param subaccount code |
||
633 | * @return array |
||
634 | */ |
||
635 | public function fetchSubAccount($subaccount_code){ |
||
641 | |||
642 | /** |
||
643 | * Lists all the subaccounts associated with the account |
||
644 | * @param $per_page - Specifies how many records to retrieve per page , $page - SPecifies exactly what page to retrieve |
||
645 | * @return array |
||
646 | */ |
||
647 | public function listSubAccounts($per_page,$page){ |
||
653 | |||
654 | |||
655 | /** |
||
656 | * Updates a subaccount to be used for split payments . Required params are business_name , settlement_bank , account_number , percentage_charge |
||
657 | * @param subaccount code |
||
658 | * @return array |
||
659 | */ |
||
660 | |||
661 | View Code Duplication | public function updateSubAccount($subaccount_code, $data = null){ |
|
682 | } |
||
683 |
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: