From 3740f6546b13d641449331c0238647f3714735eb Mon Sep 17 00:00:00 2001 From: Lucian Turiac Date: Wed, 30 Aug 2023 11:24:36 +0300 Subject: [PATCH] BP-2433 - Remove helper addDebug function --- Helper/Data.php | 37 ------------------- Observer/RestoreQuote.php | 57 ++++++++++++++++------------- Plugin/ShippingMethodManagement.php | 36 +++++++++++------- 3 files changed, 55 insertions(+), 75 deletions(-) diff --git a/Helper/Data.php b/Helper/Data.php index f8638db72..4a928a1eb 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -134,11 +134,6 @@ class Data extends AbstractHelper */ protected $json; - /** - * @var array - */ - private $staticCache = []; - /** * @var StoreManagerInterface */ @@ -407,17 +402,6 @@ public function getStore(): ?StoreInterface } } - /** - * Add debug message - * - * @param string $messages - * @return void - */ - public function addDebug(string $messages) - { - $this->logger->addDebug($messages); - } - /** * Retrieve the sort order for the available gift card types * @@ -451,27 +435,6 @@ public function areEqualAmounts($amount1, $amount2): bool } } - /** - * Retrieve the value of the restore quote last order flag from the checkout session - * - * @return int|string|bool - */ - public function getRestoreQuoteLastOrder() - { - return $this->checkoutSession->getRestoreQuoteLastOrder(); - } - - /** - * Sets the value of the 'restore quote last order' flag in the checkout session. - * - * @param int|string|bool $value - * @return int|string|bool - */ - public function setRestoreQuoteLastOrder($value) - { - return $this->checkoutSession->setRestoreQuoteLastOrder($value); - } - /** * Returns the current checkout session object. * diff --git a/Observer/RestoreQuote.php b/Observer/RestoreQuote.php index 31ab9f676..f4a2714e0 100644 --- a/Observer/RestoreQuote.php +++ b/Observer/RestoreQuote.php @@ -22,8 +22,8 @@ use Buckaroo\Magento2\Helper\Data; use Buckaroo\Magento2\Helper\PaymentGroupTransaction; +use Buckaroo\Magento2\Logging\BuckarooLoggerInterface; use Buckaroo\Magento2\Model\ConfigProvider\Account; -use Buckaroo\Magento2\Model\ConfigProvider\Method\Giftcards; use Buckaroo\Magento2\Model\ConfigProvider\Method\Payconiq; use Buckaroo\Magento2\Model\Giftcard\Remove as GiftcardRemove; use Buckaroo\Magento2\Model\Service\Order; @@ -32,7 +32,6 @@ use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Quote\Api\CartRepositoryInterface; -use Magento\Sales\Model\Order as OrderModel; class RestoreQuote implements ObserverInterface { @@ -67,14 +66,14 @@ class RestoreQuote implements ObserverInterface private $checkoutSession; /** - * @var Data + * @var BuckarooLoggerInterface */ - private Data $helper; + private BuckarooLoggerInterface $logger; /** * @param Session $checkoutSession * @param Account $accountConfig - * @param Data $helper + * @param BuckarooLoggerInterface $logger * @param CartRepositoryInterface $quoteRepository * @param Order $orderService * @param GiftcardRemove $giftcardRemoveService @@ -83,7 +82,7 @@ class RestoreQuote implements ObserverInterface public function __construct( Session $checkoutSession, Account $accountConfig, - Data $helper, + BuckarooLoggerInterface $logger, CartRepositoryInterface $quoteRepository, Order $orderService, GiftcardRemove $giftcardRemoveService, @@ -92,7 +91,7 @@ public function __construct( $this->orderService = $orderService; $this->checkoutSession = $checkoutSession; $this->accountConfig = $accountConfig; - $this->helper = $helper; + $this->logger = $logger; $this->quoteRepository = $quoteRepository; $this->giftcardRemoveService = $giftcardRemoveService; $this->groupTransaction = $groupTransaction; @@ -111,8 +110,6 @@ public function __construct( */ public function execute(Observer $observer): void { - $this->helper->addDebug(__METHOD__ . '|1|'); - $lastRealOrder = $this->checkoutSession->getLastRealOrder(); $previousOrderId = $lastRealOrder->getId(); @@ -120,21 +117,16 @@ public function execute(Observer $observer): void if ($this->shouldSkipFurtherEventHandling() || strpos($payment->getMethod(), 'buckaroo_magento2') === false || in_array($payment->getMethod(), [Payconiq::CODE])) { - $this->helper->addDebug(__METHOD__ . '|10|'); return; } if ($this->accountConfig->getCartKeepAlive($lastRealOrder->getStore())) { - $this->helper->addDebug(__METHOD__ . '|20|'); - if ($this->checkoutSession->getQuote() && $this->checkoutSession->getQuote()->getId() && ($quote = $this->quoteRepository->getActive($this->checkoutSession->getQuote()->getId())) ) { - $this->helper->addDebug(__METHOD__ . '|25|'); if ($shippingAddress = $quote->getShippingAddress()) { if (!$shippingAddress->getShippingMethod()) { - $this->helper->addDebug(__METHOD__ . '|35|'); $shippingAddress->load($shippingAddress->getAddressId()); } } @@ -142,25 +134,36 @@ public function execute(Observer $observer): void if ( ( - $this->helper->getRestoreQuoteLastOrder() && - ($lastRealOrder->getData('state') === 'new') && - ($lastRealOrder->getData('status') === 'pending') && + $this->checkoutSession->getRestoreQuoteLastOrder() && + $lastRealOrder->getData('state') === 'new' && + $lastRealOrder->getData('status') === 'pending' && $payment->getMethodInstance()->usesRedirect ) || $this->canRestoreFailedFromSpam() ) { - $this->helper->addDebug(__METHOD__ . '|40|'); + $this->logger->addDebug(sprintf( + '[RESTORE_QUOTE] | [Observer] | [%s:%s] - Restore Quote | ' . + 'lastRealOrder: %s | previousOrderId: %s', + __METHOD__, __LINE__, + $lastRealOrder->getIncrementId(), + $previousOrderId + )); + $this->checkoutSession->restoreQuote(); $this->rollbackPartialPayment($lastRealOrder->getIncrementId(), $payment); $this->setOrderToCancel($previousOrderId); } } - $this->helper->addDebug(__METHOD__ . '|50|'); - $this->helper->setRestoreQuoteLastOrder(false); + $this->logger->addDebug(sprintf( + '[RESTORE_QUOTE] | [Observer] | [%s:%s] - Restore Skipped: ' + . 'Quote restoration was not carried out. | lastRealOrder: %s', + __METHOD__, __LINE__, + $lastRealOrder->getIncrementId(), + )); + + $this->checkoutSession->setRestoreQuoteLastOrder(false); $this->checkoutSession->unsBuckarooFailedMaxAttempts(); } - - $this->helper->addDebug(__METHOD__ . '|55|'); } /** @@ -180,7 +183,7 @@ public function shouldSkipFurtherEventHandling(): bool */ public function canRestoreFailedFromSpam() { - return $this->helper->getRestoreQuoteLastOrder() && + return $this->checkoutSession->getRestoreQuoteLastOrder() && $this->checkoutSession->getBuckarooFailedMaxAttempts() === true; } @@ -188,6 +191,7 @@ public function canRestoreFailedFromSpam() * Rollback Partial Payment * * @param string $incrementId + * @param $payment * @return void */ public function rollbackPartialPayment(string $incrementId, $payment): void @@ -198,9 +202,12 @@ public function rollbackPartialPayment(string $incrementId, $payment): void $this->giftcardRemoveService->remove($transaction->getTransactionId(), $incrementId, $payment); } } catch (\Throwable $th) { - $this->helper->addDebug(__METHOD__ . $th); + $this->logger->addError(sprintf( + '[RESTORE_QUOTE] | [Observer] | [%s:%s] - Rollback Partial Payment | [ERROR]: %s', + __METHOD__, __LINE__, + $th->getMessage() + )); } - } /** diff --git a/Plugin/ShippingMethodManagement.php b/Plugin/ShippingMethodManagement.php index 132ce6c72..6b821f29e 100644 --- a/Plugin/ShippingMethodManagement.php +++ b/Plugin/ShippingMethodManagement.php @@ -22,6 +22,7 @@ namespace Buckaroo\Magento2\Plugin; use Buckaroo\Magento2\Helper\Data; +use Buckaroo\Magento2\Logging\BuckarooLoggerInterface; use Buckaroo\Magento2\Model\ConfigProvider\Account; use Magento\Checkout\Model\Session; use Magento\Customer\Model\Session as CustomerSession; @@ -45,47 +46,47 @@ class ShippingMethodManagement /** * @var CustomerSession */ - private $customerSession; + private CustomerSession $customerSession; /** - * @var Data + * @var BuckarooLoggerInterface */ - private Data $helper; + private BuckarooLoggerInterface $logger; /** * @var CartRepositoryInterface */ - private $quoteRepository; + private CartRepositoryInterface $quoteRepository; /** * @param Session $checkoutSession * @param CustomerSession $customerSession * @param Account $accountConfig - * @param Data $helper + * @param BuckarooLoggerInterface $logger * @param CartRepositoryInterface $quoteRepository */ public function __construct( Session $checkoutSession, CustomerSession $customerSession, Account $accountConfig, - Data $helper, + BuckarooLoggerInterface $logger, CartRepositoryInterface $quoteRepository ) { $this->checkoutSession = $checkoutSession; $this->customerSession = $customerSession; $this->accountConfig = $accountConfig; - $this->helper = $helper; + $this->logger = $logger; $this->quoteRepository = $quoteRepository; } /** * Ensures that the shipping address is loaded and shipping rates are collected. * + * @param \Magento\Quote\Model\ShippingMethodManagement $subject * @param int $cartId * @return void * @throws LocalizedException * @throws NoSuchEntityException - * * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ @@ -100,20 +101,29 @@ public function beforeGet(\Magento\Quote\Model\ShippingMethodManagement $subject $order = $payment->getOrder(); - $this->helper->addDebug(__METHOD__ . '|1|'); + $this->logger->addDebug(sprintf( + '[SET_SHIPPING] | [Plugin] | [%s:%s] - START - Ensures that the shipping address is loaded ' + . ' and shipping rates are collected. | lastRealOrder: %s', + __METHOD__, __LINE__, + $lastRealOrder->getIncrementId(), + )); + if ($this->accountConfig->getCartKeepAlive($order->getStore()) && $this->isNeedRecreate($order->getStore()) ) { - $this->helper->addDebug(__METHOD__ . '|2|'); if ($this->checkoutSession->getQuote() && $this->checkoutSession->getQuote()->getId() && ($quote = $this->quoteRepository->getActive($this->checkoutSession->getQuote()->getId())) ) { - $this->helper->addDebug(__METHOD__ . '|3|'); if ($shippingAddress = $quote->getShippingAddress()) { - $this->helper->addDebug(__METHOD__ . '|4|'); if (!$shippingAddress->getShippingMethod()) { - $this->helper->addDebug(__METHOD__ . '|5|'); + $this->logger->addDebug(sprintf( + '[SET_SHIPPING] | [Plugin] | [%s:%s] - SET SHIPPING ADDRESS - Ensures that ' + . 'the shipping address is loaded. | lastRealOrder: %s | shippingAddressId: %s', + __METHOD__, __LINE__, + $lastRealOrder->getIncrementId(), + $shippingAddress->getAddressId() + )); $shippingAddress->load($shippingAddress->getAddressId()); } $shippingAddress->setCollectShippingRates(true);