| Total Complexity | 50 |
| Total Lines | 374 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Checkout 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.
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 Checkout, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Checkout implements CheckoutInterface, PropertyHandlerInterface |
||
| 13 | { |
||
| 14 | use PropertyHandler; |
||
| 15 | |||
| 16 | const PENDING = 'PENDING'; |
||
| 17 | const COMPLETED = 'COMPLETED'; |
||
| 18 | const FAILED = 'FAILED'; |
||
| 19 | |||
| 20 | protected $id; |
||
| 21 | protected $status; |
||
| 22 | protected $amount; |
||
| 23 | protected $feeAmount; |
||
| 24 | protected $currency; |
||
| 25 | protected $payToEmail; |
||
| 26 | protected $payFromEmail; |
||
| 27 | protected $description; |
||
| 28 | protected $redirectUrl; |
||
| 29 | protected $validUntil; |
||
| 30 | protected $token; |
||
| 31 | protected $transactionId; |
||
| 32 | protected $transactionCode; |
||
| 33 | protected $type; |
||
| 34 | protected $installments; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Checkout reference |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $reference; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Customer |
||
| 45 | * |
||
| 46 | * @var null|Customer |
||
| 47 | */ |
||
| 48 | protected $customer; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * PaymentInstrument |
||
| 52 | * |
||
| 53 | * |
||
| 54 | * @var null|PaymentInstrument |
||
| 55 | */ |
||
| 56 | protected $card; |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * checkout constructor |
||
| 61 | * |
||
| 62 | * @param array $data |
||
| 63 | */ |
||
| 64 | public function __construct(array $data = null) { |
||
| 65 | if ($data !== null) { |
||
| 66 | $this->setAmount($data['amount']??0); |
||
| 67 | $this->setPayToEmail($data['pay_to_email']??''); |
||
| 68 | $this->setCheckoutReference($data['checkout_reference']??''); |
||
| 69 | $this->setCurrency($data['currency']??''); |
||
| 70 | $this->setDescription($data['description']??null); |
||
| 71 | $this->setFeeAmount($data['fee_amount']??null); |
||
| 72 | $this->setPayFromEmail($data['pay_from_mail']??null); |
||
| 73 | $this->setId($data['id']??null); |
||
| 74 | $this->setRedirectUrl($data['redirect_url']??null); |
||
| 75 | $this->setStatus($data['status']??null); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @inheritDoc |
||
| 81 | */ |
||
| 82 | public function isValid(): bool { |
||
| 83 | return $this->getReference() !== null |
||
| 84 | && $this->getAmount() !== null |
||
| 85 | && $this->getPayToEmail() !== null |
||
| 86 | && $this->getAmount() > 0 |
||
| 87 | && Currency::isValid($this->getCurrency()); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @inheritDoc |
||
| 92 | */ |
||
| 93 | public function getId():? string { |
||
| 94 | return $this->id; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @inheritDoc |
||
| 99 | */ |
||
| 100 | public function setId(?string $id): CheckoutInterface { |
||
| 101 | $this->id = $id; |
||
| 102 | return $this; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @inheritDoc |
||
| 107 | */ |
||
| 108 | public function getStatus():? string { |
||
| 109 | return $this->status; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @inheritDoc |
||
| 114 | */ |
||
| 115 | public function setStatus(?string $status): CheckoutInterface { |
||
| 116 | $this->status = $status; |
||
| 117 | return $this; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @inheritDoc |
||
| 122 | */ |
||
| 123 | public function getAmount():? float { |
||
| 124 | return $this->amount; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @inheritDoc |
||
| 129 | */ |
||
| 130 | public function setAmount(float $amount): CheckoutInterface { |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @inheritDoc |
||
| 137 | */ |
||
| 138 | public function getCurrency():? string { |
||
| 139 | return $this->currency; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @inheritDoc |
||
| 144 | */ |
||
| 145 | public function setCurrency(?string $currency): CheckoutInterface { |
||
| 146 | $this->currency = $currency; |
||
| 147 | return $this; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @inheritDoc |
||
| 152 | */ |
||
| 153 | public function getPayToEmail():? string { |
||
| 154 | return $this->payToEmail; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @inheritDoc |
||
| 159 | */ |
||
| 160 | public function setPayToEmail(string $email): CheckoutInterface { |
||
| 161 | $this->payToEmail = trim($email) === '' ? null : $email; |
||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @inheritDoc |
||
| 167 | */ |
||
| 168 | public function getCheckoutReference():? string { |
||
| 169 | return $this->reference; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @inheritDoc |
||
| 174 | */ |
||
| 175 | public function setCheckoutReference(string $reference): CheckoutInterface { |
||
| 176 | $this->reference = trim($reference) === '' ? null : $reference; |
||
| 177 | return $this; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @inheritDoc |
||
| 182 | */ |
||
| 183 | public function getDescription():? string { |
||
| 184 | return $this->description; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @inheritDoc |
||
| 189 | */ |
||
| 190 | public function setDescription(?string $description): CheckoutInterface { |
||
| 191 | $this->description = $description; |
||
| 192 | return $this; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @inheritDoc |
||
| 197 | */ |
||
| 198 | public function getFeeAmount():? float { |
||
| 199 | return $this->feeAmount; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @inheritDoc |
||
| 204 | */ |
||
| 205 | public function setFeeAmount(?float $fee): CheckoutInterface { |
||
| 206 | $this->feeAmount = $fee; |
||
| 207 | return $this; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @inheritDoc |
||
| 212 | */ |
||
| 213 | public function getPayFromEmail():? string { |
||
| 214 | return $this->payFromEmail; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @inheritDoc |
||
| 219 | */ |
||
| 220 | public function setPayFromEmail(?string $email): CheckoutInterface { |
||
| 221 | $this->payFromEmail = $email; |
||
| 222 | return $this; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @inheritDoc |
||
| 227 | */ |
||
| 228 | public function getRedirectUrl():? string { |
||
| 229 | return $this->redirectUrl; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @inheritDoc |
||
| 234 | */ |
||
| 235 | public function setRedirectUrl(?string $url): CheckoutInterface { |
||
| 236 | $this->redirectUrl = $url; |
||
| 237 | return $this; |
||
| 238 | } |
||
| 239 | |||
| 240 | function getValidUntil():? string { |
||
|
|
|||
| 241 | return $this->validUntil; |
||
| 242 | } |
||
| 243 | |||
| 244 | function setValidUntil(?string $timestamp): CheckoutInterface { |
||
| 245 | $this->validUntil = $timestamp; |
||
| 246 | return $this; |
||
| 247 | } |
||
| 248 | |||
| 249 | function getTransactionCode():? string { |
||
| 250 | return $this->transactionCode; |
||
| 251 | } |
||
| 252 | |||
| 253 | function setTransactionCode(?string $code): CheckoutInterface { |
||
| 254 | $this->transactionCode = $code; |
||
| 255 | return $this; |
||
| 256 | } |
||
| 257 | |||
| 258 | function getTransactionId():? string { |
||
| 259 | return $this->transactionId; |
||
| 260 | } |
||
| 261 | |||
| 262 | function setTransactionId(?string $id): CheckoutInterface { |
||
| 263 | $this->transactionId = $id; |
||
| 264 | return $this; |
||
| 265 | } |
||
| 266 | |||
| 267 | function getTransactions():? array { |
||
| 268 | // return $this->transactions; |
||
| 269 | //TODO remember myself whats is it. |
||
| 270 | return []; |
||
| 271 | } |
||
| 272 | |||
| 273 | function setTransactions(?Array $transactions): CheckoutInterface { |
||
| 274 | // $this->transactions = $transactions; |
||
| 275 | //TODO remember myself whats is it. |
||
| 276 | return $this; |
||
| 277 | } |
||
| 278 | |||
| 279 | function getToken():? string { |
||
| 280 | return $this->token; |
||
| 281 | } |
||
| 282 | |||
| 283 | function setToken(?string $token): CheckoutInterface { |
||
| 284 | $this->token = $token; |
||
| 285 | return $this; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get customer |
||
| 290 | * @return CustomerInterface|null |
||
| 291 | */ |
||
| 292 | public function getCustomer(): ?CustomerInterface |
||
| 293 | { |
||
| 294 | return $this->customer; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Set customer |
||
| 299 | * |
||
| 300 | * @param null|CustomerInterface $customer Customer |
||
| 301 | * |
||
| 302 | * @return CheckoutInterface |
||
| 303 | */ |
||
| 304 | public function setCustomer(?CustomerInterface $customer): CheckoutInterface |
||
| 305 | { |
||
| 306 | $this->customer = $customer; |
||
| 307 | |||
| 308 | return $this; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get card |
||
| 313 | * |
||
| 314 | * @return null|PaymentInstrumentInterface |
||
| 315 | */ |
||
| 316 | public function getCard(): ?PaymentInstrumentInterface |
||
| 317 | { |
||
| 318 | return $this->card; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Set card |
||
| 323 | * |
||
| 324 | * @param null|PaymentInstrumentInterface $card |
||
| 325 | * |
||
| 326 | * @return CheckoutInterface |
||
| 327 | */ |
||
| 328 | public function setCard(?PaymentInstrumentInterface $card): CheckoutInterface |
||
| 329 | { |
||
| 330 | $this->card = $card; |
||
| 331 | |||
| 332 | return $this; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get checkout reference |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function getReference(): ?string |
||
| 341 | { |
||
| 342 | return $this->reference; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Set checkout reference |
||
| 347 | * |
||
| 348 | * @param string $reference Checkout reference |
||
| 349 | * |
||
| 350 | * @return CheckoutInterface |
||
| 351 | */ |
||
| 352 | public function setReference(?string $reference): CheckoutInterface |
||
| 353 | { |
||
| 354 | $this->reference = $reference; |
||
| 355 | |||
| 356 | return $this; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return mixed |
||
| 361 | */ |
||
| 362 | public function getType():? string |
||
| 363 | { |
||
| 364 | return $this->type; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param mixed $type |
||
| 369 | * @return CheckoutInterface |
||
| 370 | */ |
||
| 371 | public function setType(?string $type = null): CheckoutInterface |
||
| 372 | { |
||
| 373 | $this->type = $type; |
||
| 374 | |||
| 375 | return $this; |
||
| 376 | } |
||
| 377 | |||
| 378 | public function setInstallments(?string $installments): CheckoutInterface{ |
||
| 381 | } |
||
| 382 | |||
| 383 | public function getInstallments(): string |
||
| 384 | { |
||
| 385 | return $this->installments; |
||
| 386 | } |
||
| 387 | |||
| 388 | } |
||
| 389 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.