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 EbayEnterprise_GiftCard_Model_Giftcard 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 EbayEnterprise_GiftCard_Model_Giftcard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class EbayEnterprise_GiftCard_Model_Giftcard implements EbayEnterprise_GiftCard_Model_IGiftcard |
||
29 | { |
||
30 | const BALANCE_REQUST_ID_PREFIX = 'GCB-'; |
||
31 | const REDEEM_REQUST_ID_PREFIX = 'GCR-'; |
||
32 | const VOID_REQUST_ID_PREFIX = 'GCV-'; |
||
33 | const REQUEST_FAILED_MESSAGE = 'EbayEnterprise_GiftCard_Request_Failed'; |
||
34 | const BALANCE_REQUEST_FAILED_MESSAGE = 'EbayEnterprise_GiftCard_Request_Failed_Balance'; |
||
35 | const REDEEM_REQUEST_FAILED_MESSAGE = 'EbayEnterprise_GiftCard_Request_Failed_Redeem'; |
||
36 | const VOID_REQUEST_FAILED_MESSAGE = 'EbayEnterprise_GiftCard_Request_Failed_Void'; |
||
37 | |||
38 | /** |
||
39 | * Externalization of gift card data - number, pin, tender type, etc. When |
||
40 | * persisting gift card data in the session, only the memo object will be |
||
41 | * put into the session and gift card models will be restored or reconstructed |
||
42 | * using memo data from the session. |
||
43 | * |
||
44 | * @var EbayEnterprise_GiftCard_Model_Giftcard_Memo |
||
45 | */ |
||
46 | protected $memo; |
||
47 | /** @var EbayEnterprise_GiftCard_Helper_Data **/ |
||
48 | protected $helper; |
||
49 | /** @var EbayEnterprise_Eb2cCore_Helper_Data **/ |
||
50 | protected $coreHelper; |
||
51 | /** @var EbayEnterprise_MageLog_Helper_Data **/ |
||
52 | protected $logger; |
||
53 | /** @var LoggerInterface **/ |
||
54 | protected $apiLogger; |
||
55 | /** @var EbayEnterprise_MageLog_Helper_Context */ |
||
56 | protected $context; |
||
57 | /** @var EbayEnterprise_GiftCard_Model_Mask */ |
||
58 | protected $mask; |
||
59 | /** @var string */ |
||
60 | protected $tenderType; |
||
61 | /** @var EbayEnterprise_GiftCard_Helper_Tendertype */ |
||
62 | protected $tenderTypeHelper; |
||
63 | /** @var EbayEnterprise_Eb2cCore_Model_Config_Registry */ |
||
64 | protected $config; |
||
65 | |||
66 | /** |
||
67 | * @param array $initParams May contain: |
||
68 | * - 'helper' => EbayEnterprise_GiftCard_Helper_Data |
||
69 | * - 'core_helper' => EbayEnterprise_Eb2cCore_Helper_Data |
||
70 | * - 'logger' => EbayEnterprise_MageLog_Helper_Data |
||
71 | * - 'context' => EbayEnterprise_MageLog_Helper_Context |
||
72 | * - 'api_logger' => LoggerInterface |
||
73 | * - 'mask' => EbayEnterprise_GiftCard_Model_Mask |
||
74 | * - 'config' => EbayEnterprise_Eb2cCore_Model_Config_Registry |
||
75 | */ |
||
76 | View Code Duplication | public function __construct(array $initParams = []) |
|
|
|||
77 | { |
||
78 | list( |
||
79 | $this->tenderTypeHelper, |
||
80 | $this->helper, |
||
81 | $this->coreHelper, |
||
82 | $this->logger, |
||
83 | $this->context, |
||
84 | $this->apiLogger, |
||
85 | $this->mask, |
||
86 | $this->memo, |
||
87 | $this->config |
||
88 | ) = $this->checkTypes( |
||
89 | $this->nullCoalesce($initParams, 'tender_type_helper', Mage::helper('ebayenterprise_giftcard/tendertype')), |
||
90 | $this->nullCoalesce($initParams, 'helper', Mage::helper('ebayenterprise_giftcard')), |
||
91 | $this->nullCoalesce($initParams, 'core_helper', Mage::helper('eb2ccore')), |
||
92 | $this->nullCoalesce($initParams, 'logger', Mage::helper('ebayenterprise_magelog')), |
||
93 | $this->nullCoalesce($initParams, 'context', Mage::helper('ebayenterprise_magelog/context')), |
||
94 | $this->nullCoalesce($initParams, 'api_logger', new NullLogger), |
||
95 | $this->nullCoalesce($initParams, 'mask', Mage::getModel('ebayenterprise_giftcard/mask')), |
||
96 | $this->nullCoalesce($initParams, 'memo', Mage::getModel('ebayenterprise_giftcard/giftcard_memo')), |
||
97 | $this->nullCoalesce($initParams, 'config', Mage::helper('ebayenterprise_giftcard')->getConfigModel()) |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Type checks for self::__construct $initParams. |
||
103 | * @param EbayEnterprise_Giftcard_Helper_Tendertype |
||
104 | * @param EbayEnterprise_Giftcard_Helper_Data |
||
105 | * @param EbayEnterprise_Eb2cCore_Helper_Data |
||
106 | * @param EbayEnterprise_MageLog_Helper_Data |
||
107 | * @param EbayEnterprise_MageLog_Helper_Context |
||
108 | * @param LoggerInterface |
||
109 | * @param EbayEnterprise_GiftCard_Model_Mask |
||
110 | * @param EbayEnterprise_GiftCard_Model_Giftcard_Memo |
||
111 | * @param EbayEnterprise_Eb2cCore_Model_Config_Registry |
||
112 | * @return mixed[] |
||
113 | */ |
||
114 | protected function checkTypes( |
||
115 | EbayEnterprise_Giftcard_Helper_Tendertype $tenderTypeHelper, |
||
116 | EbayEnterprise_Giftcard_Helper_Data $helper, |
||
117 | EbayEnterprise_Eb2cCore_Helper_Data $coreHelper, |
||
118 | EbayEnterprise_MageLog_Helper_Data $logger, |
||
119 | EbayEnterprise_MageLog_Helper_Context $context, |
||
120 | LoggerInterface $apiLogger, |
||
121 | EbayEnterprise_GiftCard_Model_Mask $mask, |
||
122 | EbayEnterprise_GiftCard_Model_Giftcard_Memo $memo, |
||
123 | EbayEnterprise_Eb2cCore_Model_Config_Registry $config |
||
124 | ) { |
||
125 | return func_get_args(); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Return the value at field in array if it exists. Otherwise, use the |
||
130 | * default value. |
||
131 | * @param array $arr |
||
132 | * @param string|int $field Valid array key |
||
133 | * @param mixed $default |
||
134 | * @return mixed |
||
135 | */ |
||
136 | protected function nullCoalesce(array $arr, $field, $default) |
||
140 | |||
141 | public function setOrderId($orderId) |
||
142 | { |
||
143 | $this->memo->setOrderId($orderId); |
||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | public function getOrderId() |
||
151 | |||
152 | /** |
||
153 | * Set the card number of the gift card. If the card number is changed after |
||
154 | * receiving a tokenized card number, the tokenized card number will be |
||
155 | * cleared out as it is not expected to be relevant to the new card number. |
||
156 | * @param string $cardNumber |
||
157 | * @return self |
||
158 | */ |
||
159 | public function setCardNumber($cardNumber) |
||
160 | { |
||
161 | // Tokenized number is no longer valid after changing the non-tokenized number. |
||
162 | if ($this->memo->getCardNumber() !== $cardNumber) { |
||
163 | $this->memo->setTokenizedCardNumber(null); |
||
164 | } |
||
165 | $this->memo->setCardNumber($cardNumber); |
||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | public function getCardNumber() |
||
173 | |||
174 | /** |
||
175 | * get the tender type for the giftcard. perform a lookup |
||
176 | * using the tendertype service if not yet set. |
||
177 | * |
||
178 | * @see EbayEnterprise_GiftCard_Helper_Tendertype::lookupTenderType |
||
179 | * @return string |
||
180 | */ |
||
181 | public function getTenderType() |
||
182 | { |
||
183 | if (!$this->memo->getTenderType()) { |
||
184 | $this->memo->setTenderType($this->tenderTypeHelper->lookupTenderType( |
||
185 | $this->getCardNumber(), |
||
186 | $this->getBalanceCurrencyCode(), |
||
187 | $this->getPanIsToken() |
||
188 | )); |
||
189 | } |
||
190 | return $this->memo->getTenderType(); |
||
191 | } |
||
192 | |||
193 | public function setTokenizedCardNumber($tokenizedCardNumber) |
||
194 | { |
||
195 | $this->memo->setTokenizedCardNumber($tokenizedCardNumber); |
||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | public function getTokenizedCardNumber() |
||
203 | |||
204 | public function setPin($pin) |
||
205 | { |
||
206 | $this->memo->setPin($pin); |
||
207 | return $this; |
||
208 | } |
||
209 | |||
210 | public function getPin() |
||
214 | |||
215 | public function setPanIsToken($isToken) |
||
216 | { |
||
217 | $this->memo->setPanIsToken($isToken); |
||
218 | return $this; |
||
219 | } |
||
220 | |||
221 | public function getPanIsToken() |
||
225 | |||
226 | public function setBalanceRequestId($requestId) |
||
227 | { |
||
228 | $this->memo->setBalanceRequestId($requestId); |
||
229 | return $this; |
||
230 | } |
||
231 | |||
232 | public function getBalanceRequestId() |
||
236 | |||
237 | public function setRedeemRequestId($requestId) |
||
238 | { |
||
239 | $this->memo->setRedeemRequestId($requestId); |
||
240 | return $this; |
||
241 | } |
||
242 | |||
243 | public function getRedeemRequestId() |
||
247 | |||
248 | public function setRedeemVoidRequestId($requestId) |
||
249 | { |
||
250 | $this->memo->setRedeemVoidRequestId($requestId); |
||
251 | return $this; |
||
252 | } |
||
253 | |||
254 | public function getRedeemVoidRequestId() |
||
258 | |||
259 | public function setAmountToRedeem($amount) |
||
260 | { |
||
261 | $this->memo->setAmountToRedeem($amount); |
||
262 | return $this; |
||
263 | } |
||
264 | |||
265 | public function getAmountToRedeem() |
||
269 | |||
270 | public function setAmountRedeemed($amount) |
||
271 | { |
||
272 | $this->memo->setAmountRedeemed($amount); |
||
273 | return $this; |
||
274 | } |
||
275 | |||
276 | public function getAmountRedeemed() |
||
280 | |||
281 | public function setRedeemCurrencyCode($currencyCode) |
||
282 | { |
||
283 | $this->memo->setRedeemCurrencyCode($currencyCode); |
||
284 | return $this; |
||
285 | } |
||
286 | |||
287 | public function getRedeemCurrencyCode() |
||
288 | { |
||
289 | // if no currency code has been set, default to the current store's currency code |
||
290 | if (is_null($this->memo->getRedeemCurrencyCode())) { |
||
291 | $this->memo->setRedeemCurrencyCode(Mage::app()->getStore()->getCurrentCurrencyCode()); |
||
292 | } |
||
293 | return $this->memo->getRedeemCurrencyCode(); |
||
294 | } |
||
295 | |||
296 | public function setBalanceAmount($amount) |
||
297 | { |
||
298 | $this->memo->setBalanceAmount($amount); |
||
299 | return $this; |
||
300 | } |
||
301 | |||
302 | public function getBalanceAmount() |
||
306 | |||
307 | public function setBalanceCurrencyCode($currencyCode) |
||
308 | { |
||
309 | $this->memo->setBalanceCurrencyCode($currencyCode); |
||
310 | return $this; |
||
311 | } |
||
312 | |||
313 | public function getBalanceCurrencyCode() |
||
314 | { |
||
315 | // if no currency code has been set, default to the current store's currency code |
||
316 | if (is_null($this->memo->getBalanceCurrencyCode())) { |
||
317 | $this->memo->setBalanceCurrencyCode(Mage::app()->getStore()->getCurrentCurrencyCode()); |
||
318 | } |
||
319 | return $this->memo->getBalanceCurrencyCode(); |
||
320 | } |
||
321 | |||
322 | public function setIsRedeemed($isRedeemed) |
||
323 | { |
||
324 | $this->memo->setIsRedeemed($isRedeemed); |
||
325 | return $this; |
||
326 | } |
||
327 | |||
328 | public function getIsRedeemed() |
||
332 | |||
333 | public function setRedeemedAt(DateTime $redeemedAt) |
||
334 | { |
||
335 | $this->memo->setRedeemedAt($redeemedAt); |
||
336 | return $this; |
||
337 | } |
||
338 | |||
339 | public function getRedeemedAt() |
||
343 | |||
344 | /** |
||
345 | * Restore data from an external memo to the gift card. |
||
346 | * |
||
347 | * Intended as a means of restoring state to the gift card from data |
||
348 | * in the session or otherwise externalized. |
||
349 | * |
||
350 | * @param EbayEnterprise_GiftCard_Model_Giftcard_Memo |
||
351 | * @return self |
||
352 | */ |
||
353 | public function restoreFromMemo(EbayEnterprise_GiftCard_Model_Giftcard_Memo $memo) |
||
354 | { |
||
355 | $this->memo = $memo; |
||
356 | return $this; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Get the memo used to encapsulate gift card data for storage. |
||
361 | * |
||
362 | * @return EbayEnterprise_GiftCard_Model_Giftcard_Memo |
||
363 | */ |
||
364 | public function getMemo() |
||
368 | |||
369 | /** |
||
370 | * Log the different requests consistently. |
||
371 | * |
||
372 | * @param string $type 'balance', 'redeem', 'void' |
||
373 | * @param string $body the serialized xml body |
||
374 | * @param string $direction 'request' or 'response' |
||
375 | */ |
||
376 | protected function logApiCall($type, $direction) |
||
377 | { |
||
378 | $logData = ['type' => $type, 'direction' => $direction]; |
||
379 | $logMessage = 'Processing gift card {type} {direction}.'; |
||
380 | $this->logger->info($logMessage, $this->context->getMetaData(__CLASS__, $logData)); |
||
381 | } |
||
382 | |||
383 | |||
384 | View Code Duplication | public function checkBalance() |
|
385 | { |
||
386 | $api = $this->getApi($this->config->apiOperationBalance); |
||
387 | $this->prepareApiForBalanceCheck($api); |
||
388 | $this->logApiCall('balance', 'request'); |
||
389 | $this->logStoredValuePayload($api, true, 'Sending StoredValueBalanceRequest.'); |
||
390 | $this->sendRequest($api); |
||
391 | $this->logApiCall('balance', 'response'); |
||
392 | $this->logStoredValuePayload($api, false, 'Received StoredValueBalanceReply response.'); |
||
393 | $this->handleBalanceResponse($api); |
||
394 | return $this; |
||
395 | } |
||
396 | |||
397 | View Code Duplication | public function redeem() |
|
398 | { |
||
399 | $api = $this->getApi($this->config->apiOperationRedeem); |
||
400 | $this->prepareApiForRedeem($api); |
||
401 | $this->logApiCall('redeem', 'request'); |
||
402 | $this->logStoredValuePayload($api, true, 'Sending StoredValueRedeemRequest.'); |
||
403 | $this->sendRequest($api); |
||
404 | $this->logApiCall('redeem', 'response'); |
||
405 | $this->logStoredValuePayload($api, false, 'Received StoredValueRedeemReply response.'); |
||
406 | $this->handleRedeemResponse($api); |
||
407 | return $this; |
||
408 | } |
||
409 | |||
410 | View Code Duplication | public function void() |
|
411 | { |
||
412 | $api = $this->getApi($this->config->apiOperationVoid); |
||
413 | $this->prepareApiForVoid($api); |
||
414 | $this->logApiCall('void', 'request'); |
||
415 | $this->logStoredValuePayload($api, true, 'Sending StoredValueRedeemVoidRequest.'); |
||
416 | $this->sendRequest($api); |
||
417 | $this->logApiCall('void', 'response'); |
||
418 | $this->logStoredValuePayload($api, false, 'Received StoredValueRedeemVoidReply response.'); |
||
419 | $this->handleVoidResponse($api); |
||
420 | return $this; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Get a new SDK Api instance for an API call. |
||
425 | * @param string $operation |
||
426 | * @return IBidirectionalApi |
||
427 | */ |
||
428 | protected function getApi($operation) |
||
429 | { |
||
430 | return $this->coreHelper->getSdkApi( |
||
431 | $this->config->apiService, |
||
432 | $operation, |
||
433 | [$this->getTenderType()], |
||
434 | // Use a special logger just for the SDK logging, prevents |
||
435 | // the SDK from logging any PII. |
||
436 | $this->apiLogger |
||
437 | ); |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Prepare an API instance for a balance request - fill out and set the |
||
442 | * request payload with gift card data. |
||
443 | * @param IBidirectionalApi $api |
||
444 | * @return self |
||
445 | */ |
||
446 | protected function prepareApiForBalanceCheck(IBidirectionalApi $api) |
||
447 | { |
||
448 | $this->setBalanceRequestId($this->coreHelper->generateRequestId(self::BALANCE_REQUST_ID_PREFIX)); |
||
449 | $payload = $api->getRequestBody(); |
||
450 | $payload |
||
451 | ->setRequestId($this->getBalanceRequestId()) |
||
452 | ->setPin($this->getPin()) |
||
453 | ->setCurrencyCode($this->getBalanceCurrencyCode()); |
||
454 | $this->setPayloadAccountUniqueId($payload); |
||
455 | $api->setRequestBody($payload); |
||
456 | return $this; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Prepare an API instance for a balance request - fill out and set the |
||
461 | * request payload with gift card data. |
||
462 | * @param IBidirectionalApi $api |
||
463 | * @return self |
||
464 | */ |
||
465 | View Code Duplication | protected function prepareApiForRedeem(IBidirectionalApi $api) |
|
466 | { |
||
467 | $this->setRedeemRequestId($this->coreHelper->generateRequestId(self::REDEEM_REQUST_ID_PREFIX)); |
||
468 | $payload = $api->getRequestBody(); |
||
469 | $payload |
||
470 | ->setRequestId($this->getRedeemRequestId()) |
||
471 | ->setPin($this->getPin()) |
||
472 | ->setAmount($this->getAmountToRedeem()) |
||
473 | ->setCurrencyCode($this->getRedeemCurrencyCode()); |
||
474 | $this->setPayloadPaymentContext($payload); |
||
475 | $api->setRequestBody($payload); |
||
476 | return $this; |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Prepare an API instance for a balance request - fill out and set the |
||
481 | * request payload with gift card data. |
||
482 | * @param IBidirectionalApi $api |
||
483 | * @return self |
||
484 | */ |
||
485 | View Code Duplication | protected function prepareApiForVoid(IBidirectionalApi $api) |
|
486 | { |
||
487 | $this->setRedeemVoidRequestId($this->coreHelper->generateRequestId(self::VOID_REQUST_ID_PREFIX)); |
||
488 | $payload = $api->getRequestBody(); |
||
489 | $payload |
||
490 | ->setRequestId($this->getRedeemVoidRequestId()) |
||
491 | ->setPin($this->getPin()) |
||
492 | ->setAmount($this->getAmountRedeemed()) |
||
493 | ->setCurrencyCode($this->getRedeemCurrencyCode()); |
||
494 | $this->setPayloadPaymentContext($payload); |
||
495 | $api->setRequestBody($payload); |
||
496 | return $this; |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Set the payment context on the payload - consists of an order id and |
||
501 | * a paymend account unique id. |
||
502 | * @param Payload\Payment\IPaymentContext $payload |
||
503 | * @return self |
||
504 | */ |
||
505 | protected function setPayloadPaymentContext(Payload\Payment\IPaymentContext $payload) |
||
506 | { |
||
507 | $payload->setOrderId($this->getOrderId()); |
||
508 | return $this->setPayloadAccountUniqueId($payload); |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * Set payment account unique id fields on the payload. |
||
513 | * @param Payload\Payment\IPaymentAccountUniqueId $payload |
||
514 | * @return self |
||
515 | */ |
||
516 | protected function setPayloadAccountUniqueId(Payload\Payment\IPaymentAccountUniqueId $payload) |
||
517 | { |
||
518 | $tokenizedCardNumber = $this->getTokenizedCardNumber(); |
||
519 | $hasTokenizedNumber = !is_null($tokenizedCardNumber); |
||
520 | $payload |
||
521 | ->setCardNumber($hasTokenizedNumber ? $tokenizedCardNumber : $this->getCardNumber()) |
||
522 | ->setPanIsToken($hasTokenizedNumber); |
||
523 | return $this; |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * Send the request via the SDK |
||
528 | * @param IBidirectionalApi $api |
||
529 | * @return self |
||
530 | * @throws EbayEnterprise_GiftCard_Exception If request cannot be made successfully |
||
531 | */ |
||
532 | protected function sendRequest(IBidirectionalApi $api) |
||
533 | { |
||
534 | $logger = $this->logger; |
||
535 | $logContext = $this->context; |
||
536 | try { |
||
537 | $api->send(); |
||
538 | } catch (InvalidPayload $e) { |
||
539 | $logMessage = 'Invalid payload for stored value request. See exception log for more details.'; |
||
540 | $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])); |
||
541 | $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e)); |
||
542 | throw Mage::exception('EbayEnterprise_GiftCard', $this->helper->__(self::REQUEST_FAILED_MESSAGE)); |
||
543 | } catch (NetworkError $e) { |
||
544 | $logMessage = 'Caught a network error sending stored value request. See exception log for more details.'; |
||
545 | $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])); |
||
546 | $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e)); |
||
547 | throw Mage::exception('EbayEnterprise_GiftCard_Exception_Network', $this->helper->__(self::REQUEST_FAILED_MESSAGE)); |
||
548 | } catch (UnsupportedOperation $e) { |
||
549 | $logMessage = 'The stored value card operation is unsupported in the current configuration. See exception log for more details.'; |
||
550 | $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])); |
||
551 | $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e)); |
||
552 | throw $e; |
||
553 | } catch (UnsupportedHttpAction $e) { |
||
554 | $logMessage = 'The stored value card operation is configured with an unsupported HTTP action. See exception log for more details.'; |
||
555 | $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])); |
||
556 | $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e)); |
||
557 | throw $e; |
||
558 | } catch (Exception $e) { |
||
559 | $logMessage = 'Encountered unexpected exception from stored value card operation. See exception log for more details.'; |
||
560 | $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])); |
||
561 | $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e)); |
||
562 | throw $e; |
||
563 | } |
||
564 | return $this; |
||
565 | } |
||
566 | |||
567 | /** |
||
568 | * Check for the balance response to be successful. If it was, update the |
||
569 | * gift card with response data. If not, thrown an exception, indicating the |
||
570 | * request failed. |
||
571 | * @param IBidirectionalApi $api |
||
572 | * @return self |
||
573 | * @throws EbayEnterprise_GiftCard_Exception If the reply is not successful. |
||
574 | */ |
||
575 | protected function handleBalanceResponse(IBidirectionalApi $api) |
||
576 | { |
||
577 | $response = $api->getResponseBody(); |
||
578 | if (!$response->isSuccessful()) { |
||
579 | throw Mage::exception('EbayEnterprise_GiftCard', self::BALANCE_REQUEST_FAILED_MESSAGE); |
||
580 | } |
||
581 | return $this->extractPayloadAccountUniqueId($response) |
||
582 | ->setBalanceAmount($response->getBalanceAmount()) |
||
583 | ->setBalanceCurrencyCode($response->getCurrencyCode()); |
||
584 | } |
||
585 | |||
586 | /** |
||
587 | * Check for the gift card to have been redeemed. If it was, update the |
||
588 | * gift card with response data. If not, thrown an exception, indicating the |
||
589 | * request failed. |
||
590 | * @param IBidirectionalApi $api |
||
591 | * @return self |
||
592 | * @throws EbayEnterprise_GiftCard_Exception If the card was not redeemed |
||
593 | */ |
||
594 | protected function handleRedeemResponse(IBidirectionalApi $api) |
||
595 | { |
||
596 | $response = $api->getResponseBody(); |
||
597 | if (!$response->wasRedeemed()) { |
||
598 | // redeem failed so empty out any redeem amount |
||
599 | $this->setAmountRedeemed(0.00); |
||
600 | throw Mage::exception('EbayEnterprise_GiftCard', self::REDEEM_REQUEST_FAILED_MESSAGE); |
||
601 | } |
||
602 | return $this->extractPayloadAccountUniqueId($response) |
||
603 | ->setBalanceAmount($response->getBalanceAmount()) |
||
604 | ->setBalanceCurrencyCode($response->getBalanceCurrencyCode()) |
||
605 | ->setAmountRedeemed($response->getAmountRedeemed()) |
||
606 | ->setRedeemCurrencyCode($response->getCurrencyCodeRedeemed()) |
||
607 | ->setRedeemedAt(new DateTime) |
||
608 | ->setIsRedeemed(true); |
||
609 | } |
||
610 | |||
611 | /** |
||
612 | * Check for the gift card redeem to have been voided. If it was, update the |
||
613 | * gift card with response data. If not, thrown an exception, indicating the |
||
614 | * request failed. |
||
615 | * @param IBidirectionalApi $api |
||
616 | * @return self |
||
617 | * @throws EbayEnterprise_GiftCard_Exception If the redeem was not voided |
||
618 | */ |
||
619 | protected function handleVoidResponse(IBidirectionalApi $api) |
||
620 | { |
||
621 | $response = $api->getResponseBody(); |
||
622 | if (!$response->wasVoided()) { |
||
623 | throw Mage::exception('EbayEnterprise_GiftCard', self::VOID_REQUEST_FAILED_MESSAGE); |
||
624 | } |
||
625 | return $this->extractPayloadAccountUniqueId($response) |
||
626 | // after voiding a redemption, new balance will be the current |
||
627 | // balance plus the amount voided (which is the same as the |
||
628 | // amount originally redeemed) |
||
629 | ->setBalanceAmount($this->getBalanceAmount() + $this->getAmountRedeemed()) |
||
630 | // redeem has been voided, so amount redeemed drops back to 0 and |
||
631 | // card should no longer be considered to have been redeemed |
||
632 | ->setAmountRedeemed(0.00) |
||
633 | ->setIsRedeemed(false); |
||
634 | } |
||
635 | |||
636 | /** |
||
637 | * Extract payment context form the payload - ignoring order id in the reply |
||
638 | * as Magento is currently the master source for order ids when creating orders. |
||
639 | * @param Payload\Payment\IPaymentContext $payload |
||
640 | * @return self |
||
641 | */ |
||
642 | protected function extractPayloadPaymentContext(Payload\Payment\IPaymentContext $payload) |
||
646 | |||
647 | /** |
||
648 | * Update the gift card with account unique id data from the payload. |
||
649 | * @param Payload\Payment\IPaymentAccountUniqueId $payload |
||
650 | * @return self |
||
651 | */ |
||
652 | protected function extractPayloadAccountUniqueId(Payload\Payment\IPaymentAccountUniqueId $payload) |
||
653 | { |
||
654 | if ($payload->getPanIsToken()) { |
||
655 | $this->setTokenizedCardNumber($payload->getCardNumber()); |
||
656 | } else { |
||
657 | $this->setCardNumber($payload->getCardNumber()); |
||
658 | } |
||
659 | return $this; |
||
660 | } |
||
661 | |||
662 | /** |
||
663 | * Log request and response of stored value various service API calls. |
||
664 | * |
||
665 | * @param IBidirectionalApi |
||
666 | * @param bool |
||
667 | * @param string |
||
668 | */ |
||
669 | protected function logStoredValuePayload(IBidirectionalApi $api, $isRequest, $logMessage) |
||
670 | { |
||
671 | /** @var string */ |
||
672 | $method = 'getRequestBody'; |
||
673 | /** @var string */ |
||
674 | $metaDataKey = 'rom_request_body'; |
||
675 | if (!$isRequest) { |
||
676 | $method = 'getResponseBody'; |
||
677 | $metaDataKey = 'rom_response_body'; |
||
678 | } |
||
679 | /** @var string */ |
||
680 | $cleanedXml = $this->mask->maskXmlNodes($api->$method()->serialize()); |
||
681 | $this->logger->debug($logMessage, $this->context->getMetaData(__CLASS__, [$metaDataKey => $cleanedXml])); |
||
682 | return $this; |
||
684 | } |
||
685 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.