Skip to content

Commit

Permalink
BP-2433 - Remove helper addDebug function
Browse files Browse the repository at this point in the history
  • Loading branch information
LucianTuriacArnia committed Aug 30, 2023
1 parent 1177d49 commit 3740f65
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 75 deletions.
37 changes: 0 additions & 37 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ class Data extends AbstractHelper
*/
protected $json;

/**
* @var array
*/
private $staticCache = [];

/**
* @var StoreManagerInterface
*/
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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.
*
Expand Down
57 changes: 32 additions & 25 deletions Observer/RestoreQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -111,56 +110,60 @@ public function __construct(
*/
public function execute(Observer $observer): void
{
$this->helper->addDebug(__METHOD__ . '|1|');

$lastRealOrder = $this->checkoutSession->getLastRealOrder();
$previousOrderId = $lastRealOrder->getId();

if ($payment = $lastRealOrder->getPayment()) {
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());
}
}
}

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|');
}

/**
Expand All @@ -180,14 +183,15 @@ public function shouldSkipFurtherEventHandling(): bool
*/
public function canRestoreFailedFromSpam()
{
return $this->helper->getRestoreQuoteLastOrder() &&
return $this->checkoutSession->getRestoreQuoteLastOrder() &&
$this->checkoutSession->getBuckarooFailedMaxAttempts() === true;
}

/**
* Rollback Partial Payment
*
* @param string $incrementId
* @param $payment
* @return void
*/
public function rollbackPartialPayment(string $incrementId, $payment): void
Expand All @@ -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()
));
}

}

/**
Expand Down
36 changes: 23 additions & 13 deletions Plugin/ShippingMethodManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
*/
Expand All @@ -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);
Expand Down

0 comments on commit 3740f65

Please sign in to comment.