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 | * Initiate a payment request to Paystack |
||
| 105 | * Included the option to pass the payload to this method for situations |
||
| 106 | * when the payload is built on the fly (not passed to the controller from a view) |
||
| 107 | * @return Paystack |
||
| 108 | */ |
||
| 109 | public function makePaymentRequest( $data = null) |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $relativeUrl |
||
| 148 | * @param string $method |
||
| 149 | * @param array $body |
||
| 150 | * @return Paystack |
||
| 151 | * @throws IsNullException |
||
| 152 | */ |
||
| 153 | private function setHttpResponse($relativeUrl, $method, $body = []) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get the authorization url from the callback response |
||
| 169 | * @return Paystack |
||
| 170 | */ |
||
| 171 | public function getAuthorizationUrl() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get the authorization callback response |
||
| 182 | * In situations where Laravel serves as an backend for a detached UI, the api cannot redirect |
||
| 183 | * and might need to take different actions based on the success or not of the transaction |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public function getAuthorizationResponse($data) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Hit Paystack Gateway to Verify that the transaction is valid |
||
| 197 | */ |
||
| 198 | private function verifyTransactionAtGateway() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * True or false condition whether the transaction is verified |
||
| 209 | * @return boolean |
||
| 210 | */ |
||
| 211 | public function isTransactionVerificationValid() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get Payment details if the transaction was verified successfully |
||
| 234 | * @return json |
||
| 235 | * @throws PaymentVerificationFailedException |
||
| 236 | */ |
||
| 237 | public function getPaymentData() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Fluent method to redirect to Paystack Payment Page |
||
| 248 | */ |
||
| 249 | public function redirectNow() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get Access code from transaction callback respose |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public function getAccessCode() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Generate a Unique Transaction Reference |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function genTranxRef() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get all the customers that have made transactions on your platform |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function getAllCustomers() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get all the plans that you have on Paystack |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | public function getAllPlans() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get all the transactions that have happened overtime |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | public function getAllTransactions() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Get the whole response from a get operation |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | private function getResponse() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the data response from a get operation |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | private function getData() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Create a plan |
||
| 325 | */ |
||
| 326 | View Code Duplication | public function createPlan() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Fetch any plan based on its plan id or code |
||
| 345 | * @param $plan_code |
||
| 346 | * @return array |
||
| 347 | */ |
||
| 348 | public function fetchPlan($plan_code) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Update any plan's details based on its id or code |
||
| 356 | * @param $plan_code |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | View Code Duplication | public function updatePlan($plan_code) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Create a customer |
||
| 377 | */ |
||
| 378 | View Code Duplication | public function createCustomer() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Fetch a customer based on id or code |
||
| 395 | * @param $customer_id |
||
| 396 | * @return array |
||
| 397 | */ |
||
| 398 | public function fetchCustomer($customer_id) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Update a customer's details based on their id or code |
||
| 406 | * @param $customer_id |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | View Code Duplication | public function updateCustomer($customer_id) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Export transactions in .CSV |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | public function exportTransactions() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Create a subscription to a plan from a customer. |
||
| 442 | */ |
||
| 443 | View Code Duplication | public function createSubscription() |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Get all the subscriptions made on Paystack. |
||
| 457 | * |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | public function getAllSubscriptions() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get customer subscriptions |
||
| 469 | * |
||
| 470 | * @param integer $customer_id |
||
| 471 | * @return array |
||
| 472 | */ |
||
| 473 | public function getCustomerSubscriptions($customer_id) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Get plan subscriptions |
||
| 482 | * |
||
| 483 | * @param integer $plan_id |
||
| 484 | * @return array |
||
| 485 | */ |
||
| 486 | public function getPlanSubscriptions($plan_id) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Enable a subscription using the subscription code and token |
||
| 495 | * @return array |
||
| 496 | */ |
||
| 497 | View Code Duplication | public function enableSubscription() |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Disable a subscription using the subscription code and token |
||
| 510 | * @return array |
||
| 511 | */ |
||
| 512 | View Code Duplication | public function disableSubscription() |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Fetch details about a certain subscription |
||
| 525 | * @param mixed $subscription_id |
||
| 526 | * @return array |
||
| 527 | */ |
||
| 528 | public function fetchSubscription($subscription_id) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Create pages you can share with users using the returned slug |
||
| 536 | */ |
||
| 537 | View Code Duplication | public function createPage() |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Fetches all the pages the merchant has |
||
| 551 | * @return array |
||
| 552 | */ |
||
| 553 | public function getAllPages() |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Fetch details about a certain page using its id or slug |
||
| 561 | * @param mixed $page_id |
||
| 562 | * @return array |
||
| 563 | */ |
||
| 564 | public function fetchPage($page_id) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Update the details about a particular page |
||
| 572 | * @param $page_id |
||
| 573 | * @return array |
||
| 574 | */ |
||
| 575 | View Code Duplication | public function updatePage($page_id) |
|
| 586 | } |
||
| 587 |
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: