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) |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $relativeUrl |
||
| 185 | * @param string $method |
||
| 186 | * @param array $body |
||
| 187 | * @return Paystack |
||
| 188 | * @throws IsNullException |
||
| 189 | */ |
||
| 190 | private function setHttpResponse($relativeUrl, $method, $body = []) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get the authorization url from the callback response |
||
| 206 | * @return Paystack |
||
| 207 | */ |
||
| 208 | public function getAuthorizationUrl() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get the authorization callback response |
||
| 219 | * In situations where Laravel serves as an backend for a detached UI, the api cannot redirect |
||
| 220 | * and might need to take different actions based on the success or not of the transaction |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public function getAuthorizationResponse($data) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Hit Paystack Gateway to Verify that the transaction is valid |
||
| 234 | */ |
||
| 235 | private function verifyTransactionAtGateway() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * True or false condition whether the transaction is verified |
||
| 246 | * @return boolean |
||
| 247 | */ |
||
| 248 | public function isTransactionVerificationValid() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get Payment details if the transaction was verified successfully |
||
| 271 | * @return json |
||
| 272 | * @throws PaymentVerificationFailedException |
||
| 273 | */ |
||
| 274 | public function getPaymentData() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Fluent method to redirect to Paystack Payment Page |
||
| 285 | */ |
||
| 286 | public function redirectNow() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get Access code from transaction callback respose |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function getAccessCode() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Generate a Unique Transaction Reference |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public function genTranxRef() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get all the customers that have made transactions on your platform |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public function getAllCustomers() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get all the plans that you have on Paystack |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | public function getAllPlans() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get all the transactions that have happened overtime |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | public function getAllTransactions() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get the whole response from a get operation |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | private function getResponse() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get the data response from a get operation |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | private function getData() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Create a plan |
||
| 362 | */ |
||
| 363 | View Code Duplication | public function createPlan() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Fetch any plan based on its plan id or code |
||
| 383 | * @param $plan_code |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | public function fetchPlan($plan_code) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Update any plan's details based on its id or code |
||
| 394 | * @param $plan_code |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function updatePlan($plan_code) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Create a customer |
||
| 415 | */ |
||
| 416 | View Code Duplication | public function createCustomer() |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Fetch a customer based on id or code |
||
| 433 | * @param $customer_id |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | public function fetchCustomer($customer_id) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Update a customer's details based on their id or code |
||
| 444 | * @param $customer_id |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | View Code Duplication | public function updateCustomer($customer_id) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Export transactions in .CSV |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | View Code Duplication | public function exportTransactions() |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Create a subscription to a plan from a customer. |
||
| 480 | */ |
||
| 481 | View Code Duplication | public function createSubscription() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Get all the subscriptions made on Paystack. |
||
| 495 | * |
||
| 496 | * @return array |
||
| 497 | */ |
||
| 498 | public function getAllSubscriptions() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Get customer subscriptions |
||
| 507 | * |
||
| 508 | * @param integer $customer_id |
||
| 509 | * @return array |
||
| 510 | */ |
||
| 511 | public function getCustomerSubscriptions($customer_id) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Get plan subscriptions |
||
| 520 | * |
||
| 521 | * @param integer $plan_id |
||
| 522 | * @return array |
||
| 523 | */ |
||
| 524 | public function getPlanSubscriptions($plan_id) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Enable a subscription using the subscription code and token |
||
| 533 | * @return array |
||
| 534 | */ |
||
| 535 | View Code Duplication | public function enableSubscription() |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Disable a subscription using the subscription code and token |
||
| 548 | * @return array |
||
| 549 | */ |
||
| 550 | View Code Duplication | public function disableSubscription() |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Fetch details about a certain subscription |
||
| 563 | * @param mixed $subscription_id |
||
| 564 | * @return array |
||
| 565 | */ |
||
| 566 | public function fetchSubscription($subscription_id) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Create pages you can share with users using the returned slug |
||
| 574 | */ |
||
| 575 | View Code Duplication | public function createPage() |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Fetches all the pages the merchant has |
||
| 589 | * @return array |
||
| 590 | */ |
||
| 591 | public function getAllPages() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Fetch details about a certain page using its id or slug |
||
| 599 | * @param mixed $page_id |
||
| 600 | * @return array |
||
| 601 | */ |
||
| 602 | public function fetchPage($page_id) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Update the details about a particular page |
||
| 610 | * @param $page_id |
||
| 611 | * @return array |
||
| 612 | */ |
||
| 613 | View Code Duplication | public function updatePage($page_id) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Creates a subaccount to be used for split payments . Required params are business_name , settlement_bank , account_number , percentage_charge |
||
| 627 | * |
||
| 628 | * @return array |
||
| 629 | */ |
||
| 630 | |||
| 631 | View Code Duplication | public function createSubAccount(){ |
|
| 648 | |||
| 649 | /** |
||
| 650 | * Fetches details of a subaccount |
||
| 651 | * @param subaccount code |
||
| 652 | * @return array |
||
| 653 | */ |
||
| 654 | public function fetchSubAccount($subaccount_code){ |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Lists all the subaccounts associated with the account |
||
| 663 | * @param $per_page - Specifies how many records to retrieve per page , $page - SPecifies exactly what page to retrieve |
||
| 664 | * @return array |
||
| 665 | */ |
||
| 666 | public function listSubAccounts($per_page,$page){ |
||
| 672 | |||
| 673 | |||
| 674 | /** |
||
| 675 | * Updates a subaccount to be used for split payments . Required params are business_name , settlement_bank , account_number , percentage_charge |
||
| 676 | * @param subaccount code |
||
| 677 | * @return array |
||
| 678 | */ |
||
| 679 | |||
| 680 | View Code Duplication | public function updateSubAccount($subaccount_code){ |
|
| 698 | } |
||
| 699 |
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: