Complex classes like PaymentRequest 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 PaymentRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class PaymentRequest extends Payload implements PaymentRequestInterface |
||
11 | { |
||
12 | use OrderLineArrayTrait; |
||
13 | |||
14 | const ACCOUNT_OFFER_REQUIRED = 'required'; |
||
15 | const ACCOUNT_OFFER_DISABLED = 'disabled'; |
||
16 | |||
17 | const PAYMENT_SOURCE_ECOMMERCE = 'eCommerce'; |
||
18 | const PAYMENT_SOURCE_MOBI = 'mobi'; |
||
19 | const PAYMENT_SOURCE_MOTO = 'moto'; |
||
20 | const PAYMENT_SOURCE_MAIL_ORDER = 'mail_order'; |
||
21 | const PAYMENT_SOURCE_TELEPHONE_ORDER = 'telephone_order'; |
||
22 | |||
23 | const SHIPPING_METHOD_LOW_COST = 'LowCost'; |
||
24 | const SHIPPING_METHOD_DESIGNATED_BY_CUSTOMER = 'DesignatedByCustomer'; |
||
25 | const SHIPPING_METHOD_INTERNATIONAL = 'International'; |
||
26 | const SHIPPING_METHOD_MILITARY = 'Military'; |
||
27 | const SHIPPING_METHOD_NEXT_DAY = 'NextDay'; |
||
28 | const SHIPPING_METHOD_OTHER = 'Other'; |
||
29 | const SHIPPING_METHOD_STORE_PICKUP = 'StorePickup'; |
||
30 | const SHIPPING_METHOD_TWO_DAY_SERVICE = 'TwoDayService'; |
||
31 | const SHIPPING_METHOD_THREE_DAY_SERVICE = 'ThreeDayService'; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $terminal; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $shopOrderId; |
||
42 | |||
43 | /** |
||
44 | * @var float |
||
45 | */ |
||
46 | private $amount; |
||
47 | |||
48 | /** |
||
49 | * Currency in ISO-4217 format |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | private $currency; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | private $language; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | private $transactionInfo; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | private $type; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | private $ccToken; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | private $saleReconciliationIdentifier; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | private $saleInvoiceNumber; |
||
84 | |||
85 | /** |
||
86 | * @var float |
||
87 | */ |
||
88 | private $salesTax; |
||
89 | |||
90 | /** |
||
91 | * @var array |
||
92 | */ |
||
93 | private $cookieParts; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | */ |
||
98 | private $paymentSource; |
||
99 | |||
100 | /** |
||
101 | * @var string |
||
102 | */ |
||
103 | private $fraudService; |
||
104 | |||
105 | /** |
||
106 | * @var string |
||
107 | */ |
||
108 | private $shippingMethod; |
||
109 | |||
110 | /** |
||
111 | * @var \DateTimeInterface |
||
112 | */ |
||
113 | private $customerCreatedDate; |
||
114 | |||
115 | /** |
||
116 | * @var string |
||
117 | */ |
||
118 | private $organisationNumber; |
||
119 | |||
120 | /** |
||
121 | * @var string |
||
122 | */ |
||
123 | private $accountOffer; |
||
124 | |||
125 | /** |
||
126 | * @var CustomerInfoInterface |
||
127 | */ |
||
128 | private $customerInfo; |
||
129 | |||
130 | /** |
||
131 | * @var ConfigInterface |
||
132 | */ |
||
133 | private $config; |
||
134 | |||
135 | 12 | public function __construct(string $terminal, string $shopOrderId, float $amount, string $currency) |
|
136 | { |
||
137 | 12 | $this->cookieParts = []; |
|
138 | 12 | $this->orderLines = []; |
|
139 | 12 | $this->terminal = $terminal; |
|
140 | 12 | $this->shopOrderId = $shopOrderId; |
|
141 | 12 | $this->amount = $amount; |
|
142 | 12 | $this->currency = $currency; |
|
143 | 12 | } |
|
144 | |||
145 | /** |
||
146 | * @return array |
||
147 | */ |
||
148 | 9 | public function getPayload() : array |
|
149 | { |
||
150 | 9 | $cookie = static::parseCookieParts($this->cookieParts); |
|
151 | |||
152 | $payload = [ |
||
153 | 9 | 'terminal' => $this->terminal, |
|
154 | 9 | 'shop_orderid' => $this->shopOrderId, |
|
155 | 9 | 'amount' => $this->amount, |
|
156 | 9 | 'currency' => $this->currency, |
|
157 | 9 | 'language' => $this->language, |
|
158 | 9 | 'transaction_info' => $this->transactionInfo, |
|
159 | 9 | 'type' => $this->type, |
|
160 | 9 | 'ccToken' => $this->ccToken, |
|
161 | 9 | 'sale_reconciliation_identifier' => $this->saleReconciliationIdentifier, |
|
162 | 9 | 'sale_invoice_number' => $this->saleInvoiceNumber, |
|
163 | 9 | 'sales_tax' => $this->salesTax, |
|
164 | 9 | 'cookie' => $cookie, |
|
165 | 9 | 'payment_source' => $this->paymentSource, |
|
166 | 9 | 'fraud_service' => $this->fraudService, |
|
167 | 9 | 'shipping_method' => $this->shippingMethod, |
|
168 | 9 | 'customer_created_date' => $this->customerCreatedDate ? $this->customerCreatedDate->format('Y-m-d') : null, |
|
169 | 9 | 'organisation_number' => $this->organisationNumber, |
|
170 | 9 | 'account_offer' => $this->accountOffer, |
|
171 | 9 | 'config' => $this->getConfig(), |
|
172 | 9 | 'customer_info' => $this->getCustomerInfo(), |
|
173 | 9 | 'orderLines' => $this->orderLines, |
|
174 | ]; |
||
175 | |||
176 | 9 | $this->validate(); |
|
177 | |||
178 | 9 | return static::simplePayload($payload); |
|
179 | } |
||
180 | |||
181 | 9 | public function validate() |
|
182 | { |
||
183 | 9 | Assert::that($this->terminal)->string(); |
|
184 | 9 | Assert::that($this->shopOrderId)->string(); |
|
185 | 9 | Assert::that($this->amount)->float(); |
|
186 | 9 | Assert::that($this->currency)->string(); |
|
187 | 9 | Assert::thatNullOr($this->language)->string(); |
|
188 | 9 | Assert::thatNullOr($this->transactionInfo)->isArray(); |
|
189 | 9 | Assert::thatNullOr($this->type)->string(); |
|
190 | 9 | Assert::thatNullOr($this->ccToken)->string(); |
|
191 | 9 | Assert::thatNullOr($this->saleReconciliationIdentifier)->string(); |
|
192 | 9 | Assert::thatNullOr($this->saleInvoiceNumber)->string(); |
|
193 | 9 | Assert::thatNullOr($this->salesTax)->float(); |
|
194 | 9 | Assert::thatNullOr($this->paymentSource)->string(); |
|
195 | 9 | Assert::thatNullOr($this->fraudService)->string(); |
|
196 | 9 | Assert::thatNullOr($this->shippingMethod)->string(); |
|
197 | 9 | Assert::thatNullOr($this->customerCreatedDate)->isInstanceOf(\DateTimeInterface::class); |
|
198 | 9 | Assert::thatNullOr($this->organisationNumber)->string(); |
|
199 | 9 | Assert::thatNullOr($this->accountOffer)->string(); |
|
200 | 9 | Assert::thatNullOr($this->orderLines)->isArray(); |
|
201 | 9 | } |
|
202 | |||
203 | /** |
||
204 | * Takes an array of cookie parts and returns an urlencoded string ready to send |
||
205 | * |
||
206 | * @param array $cookieParts |
||
207 | * @return string |
||
208 | */ |
||
209 | 12 | public static function parseCookieParts(array $cookieParts) |
|
210 | { |
||
211 | 12 | $cookie = ''; |
|
212 | 12 | foreach ($cookieParts as $key => $val) { |
|
213 | 6 | $cookie .= $key.'='.rawurlencode($val).';'; |
|
214 | } |
||
215 | 12 | $cookie = trim($cookie, ';'); |
|
216 | |||
217 | 12 | return $cookie; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param string $key |
||
222 | * @return string |
||
223 | */ |
||
224 | 3 | public function getCookiePart(string $key) : string |
|
228 | |||
229 | /** |
||
230 | * @param string $key |
||
231 | * @param string $value |
||
232 | * @return PaymentRequest |
||
233 | */ |
||
234 | 6 | public function setCookiePart(string $key, string $value) : self |
|
235 | { |
||
236 | 6 | $this->cookieParts[$key] = $value; |
|
237 | 6 | return $this; |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * @return string |
||
242 | */ |
||
243 | 3 | public function getTerminal() : string |
|
244 | { |
||
245 | 3 | return $this->terminal; |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param string $terminal |
||
250 | * @return PaymentRequest |
||
251 | */ |
||
252 | 3 | public function setTerminal(string $terminal) : self |
|
253 | { |
||
254 | 3 | $this->terminal = $terminal; |
|
255 | 3 | return $this; |
|
256 | } |
||
257 | |||
258 | /** |
||
259 | * @return string |
||
260 | */ |
||
261 | 3 | public function getShopOrderId() : string |
|
262 | { |
||
263 | 3 | return $this->shopOrderId; |
|
264 | } |
||
265 | |||
266 | /** |
||
267 | * @param string $shopOrderId |
||
268 | * @return PaymentRequest |
||
269 | */ |
||
270 | 3 | public function setShopOrderId(string $shopOrderId) : self |
|
271 | { |
||
272 | 3 | $this->shopOrderId = $shopOrderId; |
|
273 | 3 | return $this; |
|
274 | } |
||
275 | |||
276 | /** |
||
277 | * @return float |
||
278 | */ |
||
279 | 3 | public function getAmount() : float |
|
280 | { |
||
281 | 3 | return $this->amount; |
|
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param float $amount |
||
286 | * @return PaymentRequest |
||
287 | */ |
||
288 | 3 | public function setAmount(float $amount) : self |
|
289 | { |
||
290 | 3 | $this->amount = $amount; |
|
291 | 3 | return $this; |
|
292 | } |
||
293 | |||
294 | /** |
||
295 | * @return string |
||
296 | */ |
||
297 | 3 | public function getCurrency() : string |
|
298 | { |
||
299 | 3 | return $this->currency; |
|
300 | } |
||
301 | |||
302 | /** |
||
303 | * @param string $currency |
||
304 | * @return PaymentRequest |
||
305 | */ |
||
306 | 3 | public function setCurrency(string $currency) : self |
|
307 | { |
||
308 | 3 | $this->currency = $currency; |
|
309 | 3 | return $this; |
|
310 | } |
||
311 | |||
312 | /** |
||
313 | * @return string |
||
314 | */ |
||
315 | 3 | public function getLanguage() : ?string |
|
316 | { |
||
317 | 3 | return $this->language; |
|
318 | } |
||
319 | |||
320 | /** |
||
321 | * @param string $language |
||
322 | * @return PaymentRequest |
||
323 | */ |
||
324 | 6 | public function setLanguage(string $language) : self |
|
325 | { |
||
326 | 6 | $this->language = $language; |
|
327 | 6 | return $this; |
|
328 | } |
||
329 | |||
330 | /** |
||
331 | * @return array |
||
332 | */ |
||
333 | 3 | public function getTransactionInfo() : ?array |
|
334 | { |
||
335 | 3 | return $this->transactionInfo; |
|
336 | } |
||
337 | |||
338 | /** |
||
339 | * @param array $transactionInfo |
||
340 | * @return PaymentRequest |
||
341 | */ |
||
342 | 6 | public function setTransactionInfo(array $transactionInfo) : self |
|
343 | { |
||
344 | 6 | $this->transactionInfo = $transactionInfo; |
|
345 | 6 | return $this; |
|
346 | } |
||
347 | |||
348 | /** |
||
349 | * @return string |
||
350 | */ |
||
351 | 3 | public function getType() : ?string |
|
352 | { |
||
353 | 3 | return $this->type; |
|
354 | } |
||
355 | |||
356 | /** |
||
357 | * @param string $type |
||
358 | * @return PaymentRequest |
||
359 | */ |
||
360 | 6 | public function setType(string $type) : self |
|
361 | { |
||
362 | 6 | $this->type = $type; |
|
363 | 6 | return $this; |
|
364 | } |
||
365 | |||
366 | /** |
||
367 | * @return string |
||
368 | */ |
||
369 | 3 | public function getCcToken() : ?string |
|
370 | { |
||
371 | 3 | return $this->ccToken; |
|
372 | } |
||
373 | |||
374 | /** |
||
375 | * @param string $ccToken |
||
376 | * @return PaymentRequest |
||
377 | */ |
||
378 | 6 | public function setCcToken(string $ccToken) : self |
|
379 | { |
||
380 | 6 | $this->ccToken = $ccToken; |
|
381 | 6 | return $this; |
|
382 | } |
||
383 | |||
384 | /** |
||
385 | * @return string |
||
386 | */ |
||
387 | 3 | public function getSaleReconciliationIdentifier() : ?string |
|
388 | { |
||
389 | 3 | return $this->saleReconciliationIdentifier; |
|
390 | } |
||
391 | |||
392 | /** |
||
393 | * @param string $saleReconciliationIdentifier |
||
394 | * @return PaymentRequest |
||
395 | */ |
||
396 | 6 | public function setSaleReconciliationIdentifier(string $saleReconciliationIdentifier) : self |
|
397 | { |
||
398 | 6 | $this->saleReconciliationIdentifier = $saleReconciliationIdentifier; |
|
399 | 6 | return $this; |
|
400 | } |
||
401 | |||
402 | /** |
||
403 | * @return string |
||
404 | */ |
||
405 | 3 | public function getSaleInvoiceNumber() : ?string |
|
406 | { |
||
407 | 3 | return $this->saleInvoiceNumber; |
|
408 | } |
||
409 | |||
410 | /** |
||
411 | * @param string $saleInvoiceNumber |
||
412 | * @return PaymentRequest |
||
413 | */ |
||
414 | 6 | public function setSaleInvoiceNumber(string $saleInvoiceNumber) : self |
|
415 | { |
||
416 | 6 | $this->saleInvoiceNumber = $saleInvoiceNumber; |
|
417 | 6 | return $this; |
|
418 | } |
||
419 | |||
420 | /** |
||
421 | * @return float |
||
422 | */ |
||
423 | 3 | public function getSalesTax() : ?float |
|
424 | { |
||
425 | 3 | return $this->salesTax; |
|
426 | } |
||
427 | |||
428 | /** |
||
429 | * @param float $salesTax |
||
430 | * @return PaymentRequest |
||
431 | */ |
||
432 | 6 | public function setSalesTax(float $salesTax) : self |
|
433 | { |
||
434 | 6 | $this->salesTax = $salesTax; |
|
435 | 6 | return $this; |
|
436 | } |
||
437 | |||
438 | /** |
||
439 | * @return array |
||
440 | */ |
||
441 | 3 | public function getCookieParts(): array |
|
442 | { |
||
443 | 3 | return $this->cookieParts; |
|
444 | } |
||
445 | |||
446 | /** |
||
447 | * @param array $cookieParts |
||
448 | * @return PaymentRequest |
||
449 | */ |
||
450 | 3 | public function setCookieParts(array $cookieParts) : self |
|
451 | { |
||
452 | 3 | $this->cookieParts = $cookieParts; |
|
453 | 3 | return $this; |
|
454 | } |
||
455 | |||
456 | /** |
||
457 | * @return string |
||
458 | */ |
||
459 | 3 | public function getPaymentSource() : ?string |
|
460 | { |
||
461 | 3 | return $this->paymentSource; |
|
462 | } |
||
463 | |||
464 | /** |
||
465 | * @param string $paymentSource |
||
466 | * @return PaymentRequest |
||
467 | */ |
||
468 | 6 | public function setPaymentSource(string $paymentSource) : self |
|
469 | { |
||
470 | 6 | $this->paymentSource = $paymentSource; |
|
471 | 6 | return $this; |
|
472 | } |
||
473 | |||
474 | /** |
||
475 | * @return string |
||
476 | */ |
||
477 | 3 | public function getFraudService() : ?string |
|
478 | { |
||
479 | 3 | return $this->fraudService; |
|
480 | } |
||
481 | |||
482 | /** |
||
483 | * @param string $fraudService |
||
484 | * @return PaymentRequest |
||
485 | */ |
||
486 | 6 | public function setFraudService(string $fraudService) : self |
|
487 | { |
||
488 | 6 | $this->fraudService = $fraudService; |
|
489 | 6 | return $this; |
|
490 | } |
||
491 | |||
492 | /** |
||
493 | * @return string |
||
494 | */ |
||
495 | 3 | public function getShippingMethod() : ?string |
|
496 | { |
||
497 | 3 | return $this->shippingMethod; |
|
498 | } |
||
499 | |||
500 | /** |
||
501 | * @param string $shippingMethod |
||
502 | * @return PaymentRequest |
||
503 | */ |
||
504 | 6 | public function setShippingMethod(string $shippingMethod) : self |
|
505 | { |
||
506 | 6 | $this->shippingMethod = $shippingMethod; |
|
507 | 6 | return $this; |
|
508 | } |
||
509 | |||
510 | /** |
||
511 | * @return \DateTimeInterface |
||
512 | */ |
||
513 | 3 | public function getCustomerCreatedDate() : ?\DateTimeInterface |
|
514 | { |
||
515 | 3 | return $this->customerCreatedDate; |
|
516 | } |
||
517 | |||
518 | /** |
||
519 | * @param \DateTimeInterface $customerCreatedDate |
||
520 | * @return PaymentRequest |
||
521 | */ |
||
522 | 6 | public function setCustomerCreatedDate(\DateTimeInterface $customerCreatedDate) : self |
|
523 | { |
||
524 | 6 | $this->customerCreatedDate = $customerCreatedDate; |
|
525 | 6 | return $this; |
|
526 | } |
||
527 | |||
528 | /** |
||
529 | * @return string |
||
530 | */ |
||
531 | 3 | public function getOrganisationNumber() : ?string |
|
532 | { |
||
533 | 3 | return $this->organisationNumber; |
|
534 | } |
||
535 | |||
536 | /** |
||
537 | * @param string $organisationNumber |
||
538 | * @return PaymentRequest |
||
539 | */ |
||
540 | 6 | public function setOrganisationNumber(string $organisationNumber) : self |
|
541 | { |
||
542 | 6 | $this->organisationNumber = $organisationNumber; |
|
543 | 6 | return $this; |
|
544 | } |
||
545 | |||
546 | /** |
||
547 | * @return string |
||
548 | */ |
||
549 | 3 | public function getAccountOffer() : ?string |
|
550 | { |
||
551 | 3 | return $this->accountOffer; |
|
552 | } |
||
553 | |||
554 | /** |
||
555 | * @param string $accountOffer |
||
556 | * @return PaymentRequest |
||
557 | */ |
||
558 | 6 | public function setAccountOffer(string $accountOffer) : self |
|
563 | |||
564 | /** |
||
565 | * @return CustomerInfoInterface |
||
566 | */ |
||
567 | 9 | public function getCustomerInfo() : CustomerInfoInterface |
|
568 | { |
||
569 | 9 | if (!$this->customerInfo) { |
|
570 | 3 | $this->customerInfo = new CustomerInfo(); |
|
571 | } |
||
572 | 9 | return $this->customerInfo; |
|
573 | } |
||
574 | |||
575 | /** |
||
576 | * @param CustomerInfoInterface $customerInfo |
||
577 | * @return PaymentRequest |
||
578 | */ |
||
579 | 6 | public function setCustomerInfo(CustomerInfoInterface $customerInfo) : self |
|
584 | |||
585 | /** |
||
586 | * @return ConfigInterface |
||
587 | */ |
||
588 | 9 | public function getConfig() : ConfigInterface |
|
589 | { |
||
590 | 9 | if (!$this->config) { |
|
591 | 6 | $this->config = new Config(); |
|
592 | } |
||
593 | 9 | return $this->config; |
|
594 | } |
||
595 | |||
596 | /** |
||
597 | * @param ConfigInterface $config |
||
598 | * @return PaymentRequest |
||
599 | */ |
||
600 | 3 | public function setConfig(ConfigInterface $config) : self |
|
601 | { |
||
602 | 3 | $this->config = $config; |
|
603 | 3 | return $this; |
|
604 | } |
||
605 | } |
||
606 |