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 | * @return Paystack |
||
| 106 | */ |
||
| 107 | public function makePaymentRequest() |
||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $relativeUrl |
||
| 144 | * @param string $method |
||
| 145 | * @param array $body |
||
| 146 | * @return Paystack |
||
| 147 | * @throws IsNullException |
||
| 148 | */ |
||
| 149 | private function setHttpResponse($relativeUrl, $method, $body = []) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get the authorization url from the callback response |
||
| 165 | * @return Paystack |
||
| 166 | */ |
||
| 167 | public function getAuthorizationUrl() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Hit Paystack Gateway to Verify that the transaction is valid |
||
| 178 | */ |
||
| 179 | private function verifyTransactionAtGateway() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * True or false condition whether the transaction is verified |
||
| 190 | * @return boolean |
||
| 191 | */ |
||
| 192 | public function isTransactionVerificationValid() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get Payment details if the transaction was verified successfully |
||
| 215 | * @return json |
||
| 216 | * @throws PaymentVerificationFailedException |
||
| 217 | */ |
||
| 218 | public function getPaymentData() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Fluent method to redirect to Paystack Payment Page |
||
| 229 | */ |
||
| 230 | public function redirectNow() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get Access code from transaction callback respose |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function getAccessCode() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Generate a Unique Transaction Reference |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function genTranxRef() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get all the customers that have made transactions on your platform |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function getAllCustomers() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get all the plans that you have on Paystack |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | public function getAllPlans() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get all the transactions that have happened overtime |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | public function getAllTransactions() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get the whole response from a get operation |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | private function getResponse() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get the data response from a get operation |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | private function getData() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Create a plan |
||
| 306 | */ |
||
| 307 | View Code Duplication | public function createPlan() |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Fetch any plan based on its plan id or code |
||
| 326 | * @param $plan_code |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public function fetchPlan($plan_code) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Update any plan's details based on its id or code |
||
| 337 | * @param $plan_code |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | View Code Duplication | public function updatePlan($plan_code) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Create a customer |
||
| 358 | */ |
||
| 359 | View Code Duplication | public function createCustomer() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Fetch a customer based on id or code |
||
| 376 | * @param $customer_id |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | public function fetchCustomer($customer_id) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Update a customer's details based on their id or code |
||
| 387 | * @param $customer_id |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | View Code Duplication | public function updateCustomer($customer_id) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Export transactions in .CSV |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | public function exportTransactions() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Create a subscription to a plan from a customer. |
||
| 423 | */ |
||
| 424 | View Code Duplication | public function createSubscription() |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Get all the subscriptions made on Paystack. |
||
| 438 | * |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | public function getAllSubscriptions() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get customer subscriptions |
||
| 450 | * |
||
| 451 | * @param integer $customer_id |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | public function getCustomerSubscriptions($customer_id) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Get plan subscriptions |
||
| 463 | * |
||
| 464 | * @param integer $plan_id |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | public function getPlanSubscriptions($plan_id) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Enable a subscription using the subscription code and token |
||
| 476 | * @return array |
||
| 477 | */ |
||
| 478 | View Code Duplication | public function enableSubscription() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Disable a subscription using the subscription code and token |
||
| 491 | * @return array |
||
| 492 | */ |
||
| 493 | View Code Duplication | public function disableSubscription() |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Fetch details about a certain subscription |
||
| 506 | * @param mixed $subscription_id |
||
| 507 | * @return array |
||
| 508 | */ |
||
| 509 | public function fetchSubscription($subscription_id) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Create pages you can share with users using the returned slug |
||
| 517 | */ |
||
| 518 | View Code Duplication | public function createPage() |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Fetches all the pages the merchant has |
||
| 532 | * @return array |
||
| 533 | */ |
||
| 534 | public function getAllPages() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Fetch details about a certain page using its id or slug |
||
| 542 | * @param mixed $page_id |
||
| 543 | * @return array |
||
| 544 | */ |
||
| 545 | public function fetchPage($page_id) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Update the details about a particular page |
||
| 553 | * @param $page_id |
||
| 554 | * @return array |
||
| 555 | */ |
||
| 556 | View Code Duplication | public function updatePage($page_id) |
|
| 567 | } |
||
| 568 |
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: