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 | * Instance of Client |
||
| 33 | * @var Client |
||
| 34 | */ |
||
| 35 | protected $client; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Response from requests made to Paystack |
||
| 39 | * @var mixed |
||
| 40 | */ |
||
| 41 | protected $response; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Paystack API base Url |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $baseUrl; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Authorization Url - Paystack payment page |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $authorizationUrl; |
||
| 54 | |||
| 55 | public function __construct(Client $client = null) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get Base Url from Paystack config file |
||
| 63 | */ |
||
| 64 | public function setBaseUrl() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Initiate a payment request to Paystack |
||
| 71 | * Included the option to pass the payload to this method for situations |
||
| 72 | * when the payload is built on the fly (not passed to the controller from a view) |
||
| 73 | * @return Paystack |
||
| 74 | */ |
||
| 75 | public function makePaymentRequest( $data = null) |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $relativeUrl |
||
| 114 | * @param string $method |
||
| 115 | * @param array $body |
||
| 116 | * @return Paystack |
||
| 117 | * @throws IsNullException |
||
| 118 | */ |
||
| 119 | private function setHttpResponse($relativeUrl, $method, $body = []) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get the authorization url from the callback response |
||
| 135 | * @return Paystack |
||
| 136 | */ |
||
| 137 | public function getAuthorizationUrl() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the authorization callback response |
||
| 148 | * In situations where Laravel serves as an backend for a detached UI, the api cannot redirect |
||
| 149 | * and might need to take different actions based on the success (or not) of the transaction |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | public function getAuthorizationResponse($data = null) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Hit Paystack Gateway to Verify that the transaction is valid |
||
| 163 | */ |
||
| 164 | private function verifyTransactionAtGateway() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * True or false condition whether the transaction is verified |
||
| 175 | * @return boolean |
||
| 176 | */ |
||
| 177 | public function isTransactionVerificationValid() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get Payment details if the transaction was verified successfully |
||
| 200 | * @return json |
||
| 201 | * @throws PaymentVerificationFailedException |
||
| 202 | */ |
||
| 203 | public function getPaymentData() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Fluent method to redirect to Paystack Payment Page |
||
| 214 | */ |
||
| 215 | public function redirectNow() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get Access code from transaction callback respose |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function getAccessCode() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Generate a Unique Transaction Reference |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function genTranxRef() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get all the customers that have made transactions on your platform |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | public function getAllCustomers() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get all the plans that you have on Paystack |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | public function getAllPlans() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get all the transactions that have happened overtime |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function getAllTransactions() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get the whole response from a get operation |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | private function getResponse() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the data response from a get operation |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | private function getData() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Create a plan |
||
| 291 | */ |
||
| 292 | View Code Duplication | public function createPlan() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Fetch any plan based on its plan id or code |
||
| 312 | * @param $plan_code |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | public function fetchPlan($plan_code) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Update any plan's details based on its id or code |
||
| 323 | * @param $plan_code |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | View Code Duplication | public function updatePlan($plan_code) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Create a customer |
||
| 344 | */ |
||
| 345 | View Code Duplication | public function createCustomer() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Fetch a customer based on id or code |
||
| 362 | * @param $customer_id |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | public function fetchCustomer($customer_id) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Update a customer's details based on their id or code |
||
| 373 | * @param $customer_id |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | View Code Duplication | public function updateCustomer($customer_id) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Export transactions in .CSV |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | public function exportTransactions() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Create a subscription to a plan from a customer. |
||
| 409 | */ |
||
| 410 | View Code Duplication | public function createSubscription() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Get all the subscriptions made on Paystack. |
||
| 424 | * |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | public function getAllSubscriptions() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get customer subscriptions |
||
| 436 | * |
||
| 437 | * @param integer $customer_id |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | public function getCustomerSubscriptions($customer_id) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get plan subscriptions |
||
| 449 | * |
||
| 450 | * @param integer $plan_id |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | public function getPlanSubscriptions($plan_id) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Enable a subscription using the subscription code and token |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | View Code Duplication | public function enableSubscription() |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Disable a subscription using the subscription code and token |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | View Code Duplication | public function disableSubscription() |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Fetch details about a certain subscription |
||
| 492 | * @param mixed $subscription_id |
||
| 493 | * @return array |
||
| 494 | */ |
||
| 495 | public function fetchSubscription($subscription_id) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Create pages you can share with users using the returned slug |
||
| 503 | */ |
||
| 504 | View Code Duplication | public function createPage() |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Fetches all the pages the merchant has |
||
| 518 | * @return array |
||
| 519 | */ |
||
| 520 | public function getAllPages() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Fetch details about a certain page using its id or slug |
||
| 528 | * @param mixed $page_id |
||
| 529 | * @return array |
||
| 530 | */ |
||
| 531 | public function fetchPage($page_id) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Update the details about a particular page |
||
| 539 | * @param $page_id |
||
| 540 | * @return array |
||
| 541 | */ |
||
| 542 | View Code Duplication | public function updatePage($page_id) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Creates a subaccount to be used for split payments . Required params are business_name , settlement_bank , account_number , percentage_charge |
||
| 556 | * |
||
| 557 | * @return array |
||
| 558 | */ |
||
| 559 | |||
| 560 | View Code Duplication | public function createSubAccount(){ |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Fetches details of a subaccount |
||
| 580 | * @param subaccount code |
||
| 581 | * @return array |
||
| 582 | */ |
||
| 583 | public function fetchSubAccount($subaccount_code){ |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Lists all the subaccounts associated with the account |
||
| 592 | * @param $per_page - Specifies how many records to retrieve per page , $page - SPecifies exactly what page to retrieve |
||
| 593 | * @return array |
||
| 594 | */ |
||
| 595 | public function listSubAccounts($per_page,$page){ |
||
| 601 | |||
| 602 | |||
| 603 | /** |
||
| 604 | * Updates a subaccount to be used for split payments . Required params are business_name , settlement_bank , account_number , percentage_charge |
||
| 605 | * @param subaccount code |
||
| 606 | * @return array |
||
| 607 | */ |
||
| 608 | |||
| 609 | View Code Duplication | public function updateSubAccount($subaccount_code){ |
|
| 627 | } |
||
| 628 |
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: