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 | public function __construct(string $terminal, string $shopOrderId, float $amount, string $currency) |
||
136 | { |
||
137 | $this->cookieParts = []; |
||
138 | $this->orderLines = []; |
||
139 | $this->terminal = $terminal; |
||
140 | 6 | $this->shopOrderId = $shopOrderId; |
|
141 | $this->amount = $amount; |
||
142 | 6 | $this->currency = $currency; |
|
143 | 6 | } |
|
144 | 6 | ||
145 | 6 | /** |
|
146 | 6 | * @return array |
|
147 | 6 | */ |
|
148 | public function getPayload() : array |
||
149 | { |
||
150 | $cookie = static::parseCookieParts($this->cookieParts); |
||
151 | |||
152 | 6 | $payload = [ |
|
153 | 'terminal' => $this->terminal, |
||
154 | 'shop_orderid' => $this->shopOrderId, |
||
155 | 6 | 'amount' => $this->amount, |
|
156 | 6 | 'currency' => $this->currency, |
|
157 | 6 | 'language' => $this->language, |
|
158 | 6 | 'transaction_info' => $this->transactionInfo, |
|
159 | 6 | 'type' => $this->type, |
|
160 | 6 | 'ccToken' => $this->ccToken, |
|
161 | 6 | 'sale_reconciliation_identifier' => $this->saleReconciliationIdentifier, |
|
162 | 6 | 'sale_invoice_number' => $this->saleInvoiceNumber, |
|
163 | 6 | 'sales_tax' => $this->salesTax, |
|
164 | 6 | 'cookie' => $cookie, |
|
165 | 6 | 'payment_source' => $this->paymentSource, |
|
166 | 6 | 'fraud_service' => $this->fraudService, |
|
167 | 6 | 'shipping_method' => $this->shippingMethod, |
|
168 | 6 | 'customer_created_date' => $this->customerCreatedDate ? $this->customerCreatedDate->format('Y-m-d') : null, |
|
169 | 6 | 'organisation_number' => $this->organisationNumber, |
|
170 | 6 | 'account_offer' => $this->accountOffer, |
|
171 | 6 | 'config' => $this->getConfig(), |
|
172 | 6 | 'customer_info' => $this->getCustomerInfo(), |
|
173 | 4 | 'orderLines' => $this->orderLines, |
|
174 | ]; |
||
175 | |||
176 | 6 | $this->validate(); |
|
177 | 6 | ||
178 | 3 | return static::simplePayload($payload); |
|
179 | 2 | } |
|
180 | |||
181 | public function validate() |
||
182 | 6 | { |
|
183 | 6 | Assert::that($this->terminal)->string(); |
|
184 | 3 | Assert::that($this->shopOrderId)->string(); |
|
185 | 2 | Assert::that($this->amount)->float(); |
|
186 | Assert::that($this->currency)->string(); |
||
187 | Assert::thatNullOr($this->language)->string(); |
||
188 | 6 | Assert::thatNullOr($this->transactionInfo)->isArray(); |
|
189 | 6 | Assert::thatNullOr($this->type)->string(); |
|
190 | 3 | Assert::thatNullOr($this->ccToken)->string(); |
|
191 | 4 | Assert::thatNullOr($this->saleReconciliationIdentifier)->string(); |
|
192 | Assert::thatNullOr($this->saleInvoiceNumber)->string(); |
||
193 | 6 | Assert::thatNullOr($this->salesTax)->float(); |
|
194 | 3 | Assert::thatNullOr($this->paymentSource)->string(); |
|
195 | 2 | Assert::thatNullOr($this->fraudService)->string(); |
|
196 | Assert::thatNullOr($this->shippingMethod)->string(); |
||
197 | 6 | Assert::thatNullOr($this->customerCreatedDate)->isInstanceOf(\DateTimeInterface::class); |
|
198 | Assert::thatNullOr($this->organisationNumber)->string(); |
||
199 | Assert::thatNullOr($this->accountOffer)->string(); |
||
200 | Assert::thatNullOr($this->orderLines)->isArray(); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | 3 | * Takes an array of cookie parts and returns an urlencoded string ready to send |
|
205 | * |
||
206 | 3 | * @param array $cookieParts |
|
207 | 3 | * @return string |
|
208 | */ |
||
209 | public static function parseCookieParts(array $cookieParts) |
||
219 | |||
220 | /** |
||
221 | * @param string $key |
||
222 | 6 | * @return string |
|
223 | */ |
||
224 | 6 | public function getCookiePart(string $key) : string |
|
228 | |||
229 | /** |
||
230 | * @param string $key |
||
231 | 6 | * @param string $value |
|
232 | * @return PaymentRequest |
||
233 | 6 | */ |
|
234 | public function setCookiePart(string $key, string $value) : self |
||
239 | |||
240 | 6 | /** |
|
241 | * @return string |
||
242 | 6 | */ |
|
243 | 6 | public function getTerminal() : string |
|
247 | |||
248 | /** |
||
249 | 6 | * @param string $terminal |
|
250 | * @return PaymentRequest |
||
251 | 6 | */ |
|
252 | public function setTerminal(string $terminal) : self |
||
257 | |||
258 | 6 | /** |
|
259 | * @return string |
||
260 | 6 | */ |
|
261 | 6 | public function getShopOrderId() : string |
|
265 | |||
266 | /** |
||
267 | 6 | * @param string $shopOrderId |
|
268 | * @return PaymentRequest |
||
269 | 6 | */ |
|
270 | public function setShopOrderId(string $shopOrderId) : self |
||
275 | |||
276 | 6 | /** |
|
277 | * @return float |
||
278 | 6 | */ |
|
279 | 6 | public function getAmount() : float |
|
283 | |||
284 | /** |
||
285 | 6 | * @param float $amount |
|
286 | * @return PaymentRequest |
||
287 | 6 | */ |
|
288 | public function setAmount(float $amount) : self |
||
293 | |||
294 | 3 | /** |
|
295 | * @return string |
||
296 | 3 | */ |
|
297 | 3 | public function getCurrency() : string |
|
301 | |||
302 | /** |
||
303 | 6 | * @param string $currency |
|
304 | * @return PaymentRequest |
||
305 | 6 | */ |
|
306 | public function setCurrency(string $currency) : self |
||
311 | |||
312 | 3 | /** |
|
313 | * @return string |
||
314 | 3 | */ |
|
315 | 3 | public function getLanguage() : ?string |
|
319 | |||
320 | /** |
||
321 | 6 | * @param string $language |
|
322 | * @return PaymentRequest |
||
323 | 6 | */ |
|
324 | public function setLanguage(string $language) : self |
||
329 | |||
330 | 3 | /** |
|
331 | * @return array |
||
332 | 3 | */ |
|
333 | 3 | public function getTransactionInfo() : ?array |
|
337 | |||
338 | /** |
||
339 | 6 | * @param array $transactionInfo |
|
340 | * @return PaymentRequest |
||
341 | 6 | */ |
|
342 | public function setTransactionInfo(array $transactionInfo) : self |
||
347 | |||
348 | 3 | /** |
|
349 | * @return string |
||
350 | 3 | */ |
|
351 | 3 | public function getType() : ?string |
|
355 | |||
356 | /** |
||
357 | 6 | * @param string $type |
|
358 | * @return PaymentRequest |
||
359 | 6 | */ |
|
360 | public function setType(string $type) : self |
||
365 | |||
366 | 3 | /** |
|
367 | * @return string |
||
368 | 3 | */ |
|
369 | 3 | public function getCcToken() : ?string |
|
373 | |||
374 | /** |
||
375 | 6 | * @param string $ccToken |
|
376 | * @return PaymentRequest |
||
377 | 6 | */ |
|
378 | public function setCcToken(string $ccToken) : self |
||
383 | |||
384 | 3 | /** |
|
385 | * @return string |
||
386 | 3 | */ |
|
387 | 3 | public function getSaleReconciliationIdentifier() : ?string |
|
391 | |||
392 | /** |
||
393 | 6 | * @param string $saleReconciliationIdentifier |
|
394 | * @return PaymentRequest |
||
395 | 6 | */ |
|
396 | public function setSaleReconciliationIdentifier(string $saleReconciliationIdentifier) : self |
||
401 | |||
402 | 3 | /** |
|
403 | * @return string |
||
404 | 3 | */ |
|
405 | 3 | public function getSaleInvoiceNumber() : ?string |
|
409 | |||
410 | /** |
||
411 | 6 | * @param string $saleInvoiceNumber |
|
412 | * @return PaymentRequest |
||
413 | 6 | */ |
|
414 | public function setSaleInvoiceNumber(string $saleInvoiceNumber) : self |
||
419 | |||
420 | 3 | /** |
|
421 | * @return float |
||
422 | 3 | */ |
|
423 | 3 | public function getSalesTax() : ?float |
|
427 | |||
428 | /** |
||
429 | 6 | * @param float $salesTax |
|
430 | * @return PaymentRequest |
||
431 | 6 | */ |
|
432 | public function setSalesTax(float $salesTax) : self |
||
437 | |||
438 | 3 | /** |
|
439 | * @return array |
||
440 | 3 | */ |
|
441 | 3 | public function getCookieParts(): array |
|
445 | |||
446 | /** |
||
447 | 6 | * @param array $cookieParts |
|
448 | * @return PaymentRequest |
||
449 | 6 | */ |
|
450 | public function setCookieParts(array $cookieParts) : self |
||
455 | |||
456 | 3 | /** |
|
457 | * @return string |
||
458 | 3 | */ |
|
459 | 3 | public function getPaymentSource() : ?string |
|
463 | |||
464 | /** |
||
465 | 6 | * @param string $paymentSource |
|
466 | * @return PaymentRequest |
||
467 | 6 | */ |
|
468 | public function setPaymentSource(string $paymentSource) : self |
||
473 | |||
474 | 3 | /** |
|
475 | * @return string |
||
476 | 3 | */ |
|
477 | 3 | public function getFraudService() : ?string |
|
481 | |||
482 | /** |
||
483 | 6 | * @param string $fraudService |
|
484 | * @return PaymentRequest |
||
485 | 6 | */ |
|
486 | public function setFraudService(string $fraudService) : self |
||
491 | |||
492 | 3 | /** |
|
493 | * @return string |
||
494 | 3 | */ |
|
495 | 3 | public function getShippingMethod() : ?string |
|
499 | |||
500 | /** |
||
501 | 6 | * @param string $shippingMethod |
|
502 | * @return PaymentRequest |
||
503 | 6 | */ |
|
504 | public function setShippingMethod(string $shippingMethod) : self |
||
509 | |||
510 | 3 | /** |
|
511 | * @return \DateTimeInterface |
||
512 | 3 | */ |
|
513 | 3 | public function getCustomerCreatedDate() : ?\DateTimeInterface |
|
517 | |||
518 | /** |
||
519 | 6 | * @param \DateTimeInterface $customerCreatedDate |
|
520 | * @return PaymentRequest |
||
521 | 6 | */ |
|
522 | public function setCustomerCreatedDate(\DateTimeInterface $customerCreatedDate) : self |
||
527 | |||
528 | 3 | /** |
|
529 | * @return string |
||
530 | 3 | */ |
|
531 | 3 | public function getOrganisationNumber() : ?string |
|
535 | |||
536 | /** |
||
537 | 6 | * @param string $organisationNumber |
|
538 | * @return PaymentRequest |
||
539 | 6 | */ |
|
540 | public function setOrganisationNumber(string $organisationNumber) : self |
||
545 | |||
546 | /** |
||
547 | * @return string |
||
548 | */ |
||
549 | public function getAccountOffer() : ?string |
||
553 | |||
554 | /** |
||
555 | 6 | * @param string $accountOffer |
|
556 | * @return PaymentRequest |
||
557 | 6 | */ |
|
558 | 3 | public function setAccountOffer(string $accountOffer) : self |
|
563 | |||
564 | /** |
||
565 | * @return CustomerInfoInterface |
||
566 | */ |
||
567 | 3 | public function getCustomerInfo() : CustomerInfoInterface |
|
574 | |||
575 | /** |
||
576 | 6 | * @param CustomerInfoInterface $customerInfo |
|
577 | * @return PaymentRequest |
||
578 | 6 | */ |
|
579 | 3 | public function setCustomerInfo(CustomerInfoInterface $customerInfo) : self |
|
584 | |||
585 | /** |
||
586 | * @return ConfigInterface |
||
587 | */ |
||
588 | 3 | public function getConfig() : ConfigInterface |
|
595 | |||
596 | /** |
||
597 | * @param ConfigInterface $config |
||
598 | * @return PaymentRequest |
||
599 | */ |
||
600 | public function setConfig(ConfigInterface $config) : self |
||
605 | } |
||
606 |