From 105099e4fd05f6d1126b0a4b774b4e82e9bf0a5d Mon Sep 17 00:00:00 2001 From: Steven Fritzsche Date: Fri, 11 Feb 2022 16:02:04 +0100 Subject: [PATCH] [*] refactoring * wording > unselect > deselect * quote as property + isDeselectionAllowed --- ...electShipping.php => DeselectShipping.php} | 33 +++++++---- src/Plugin/PreselectShipping.php | 57 +++++++++++-------- src/etc/frontend/di.xml | 2 +- 3 files changed, 54 insertions(+), 38 deletions(-) rename src/Plugin/{UnselectShipping.php => DeselectShipping.php} (68%) diff --git a/src/Plugin/UnselectShipping.php b/src/Plugin/DeselectShipping.php similarity index 68% rename from src/Plugin/UnselectShipping.php rename to src/Plugin/DeselectShipping.php index 45494f5..943a6f9 100644 --- a/src/Plugin/UnselectShipping.php +++ b/src/Plugin/DeselectShipping.php @@ -11,12 +11,13 @@ use IntegerNet\ShippingPreselection\Service\AddressUnsetMockdata; use IntegerNet\ShippingPreselection\Service\AddressResetConditions; -class UnselectShipping +class DeselectShipping { private AddressUnsetMockdata $addressUnsetMockData; private AddressResetConditions $addressReset; private ShippingAddressAssignment $addressAssignment; private AddressFactory $addressFactory; + private ?Quote $quote; public function __construct( AddressUnsetMockdata $addressUnsetMockdata, @@ -35,30 +36,38 @@ public function __construct( */ public function afterGetQuote(Session $subject, Quote $result): Quote { - $isResetRequest = $this->addressReset->isAddressResetRequest(); - $quoteIsValid = !$result->getIsVirtual() && $result->getItemsCount(); - if (!$isResetRequest || !$quoteIsValid) { - return $result; + $this->quote = $result; + if (!$this->isDeselectionAllowed()) { + return $this->quote; } - - $address = $result->getShippingAddress(); + $address = $this->quote->getShippingAddress(); if ($this->addressUnsetMockData->isMockedAddress($address)) { - $this->addressAssignment->setAddress($result, $this->getNewShippingAddress(), true); // deletion included + $this->addressAssignment->setAddress($this->quote, $this->getNewAddress(), true); } else { $this->addressUnsetMockData->checkForEmptyAddressFields($address); $this->unsetShippingMethod($address); - $this->addressAssignment->setAddress($result, $address); + $this->addressAssignment->setAddress($this->quote, $address); } - return $result; + return $this->quote; + } + + public function isDeselectionAllowed(): bool + { + $isResetRequest = $this->addressReset->isAddressResetRequest(); + $quoteIsValid = !$this->quote->getIsVirtual() && $this->quote->getItemsCount(); + return $isResetRequest && $quoteIsValid; } - public function getNewShippingAddress(): Address + public function getNewAddress(): Address { return $this->addressFactory->create()->setAddressType(Address::TYPE_SHIPPING); } public function unsetShippingMethod(Address $address): void { - $address->setShippingAmount(0)->setBaseShippingAmount(0)->setShippingMethod('')->setShippingDescription(''); + $address->setShippingAmount(0) + ->setBaseShippingAmount(0) + ->setShippingMethod('') + ->setShippingDescription(''); } } diff --git a/src/Plugin/PreselectShipping.php b/src/Plugin/PreselectShipping.php index fd0675c..5d36f2a 100644 --- a/src/Plugin/PreselectShipping.php +++ b/src/Plugin/PreselectShipping.php @@ -18,6 +18,7 @@ class PreselectShipping private AddressSetMockdata $addressSetMockData; private AddressResetConditions $addressReset; private ShippingAddressAssignment $addressAssignment; + private ?Quote $quote; public function __construct( ShippingMethodManagement $methodManagement, @@ -36,44 +37,50 @@ public function __construct( */ public function afterGetQuote(Session $subject, Quote $result): Quote { - if (!$this->isPreselectionAllowed($result)) { - return $result; + $this->quote = $result; + if (!$this->isPreselectionAllowed()) { + return $this->quote; } - $address = $result->getShippingAddress(); - if ($this->shouldMockAddress($address)) { - $this->addressSetMockData->setMockDataOnAddress($address); - $this->addressAssignment->setAddress($result, $address); - } - $this->preselectShippingMethod($result); - return $result; + $this->preselectShippingAddress(); + $this->preselectShippingMethod(); + return $this->quote; } public function shouldMockAddress(Address $address): bool { return (true !== $address->validate()); } + + public function preselectShippingAddress(): void + { + $address = $this->quote->getShippingAddress(); + if ($this->shouldMockAddress($address)) { + $this->addressSetMockData->setMockDataOnAddress($address); + $this->addressAssignment->setAddress($this->quote, $address); + } + } - public function preselectShippingMethod(Quote $quote): void + public function preselectShippingMethod(): void { - $quote->getShippingAddress()->requestShippingRates(); // load new rates - if (!$rate = $this->getCheapestShippingRate($quote)) { + $this->quote->getShippingAddress()->requestShippingRates(); // load new rates + if (!$rate = $this->getCheapestShippingRate()) { return; } try { $this->methodManagement->set( - $quote->getId(), + $this->quote->getId(), $rate->getCarrier(), $rate->getMethod() ); } catch (\Exception $e) { - $quote->addErrorInfo('error', null, $e->getCode(), $e->getMessage()); + $this->quote->addErrorInfo('error', null, $e->getCode(), $e->getMessage()); } } - public function getCheapestShippingRate(Quote $quote): ?Rate + public function getCheapestShippingRate(): ?Rate { $selectedRate = null; - foreach ($this->getShippingRates($quote) as $rate) { + foreach ($this->getShippingRates() as $rate) { /** @var Rate $rate */ if ($selectedRate === null || $rate->getPrice() < $selectedRate->getPrice()) { $selectedRate = $rate; @@ -82,16 +89,16 @@ public function getCheapestShippingRate(Quote $quote): ?Rate return $selectedRate; } - public function getShippingRates(Quote $quote) + public function getShippingRates() { - return $quote->getShippingAddress()->getShippingRatesCollection(); + return $this->quote->getShippingAddress()->getShippingRatesCollection(); } - public function isPreselectionAllowed(Quote $quote): bool + public function isPreselectionAllowed(): bool { return $this->validateShippingResetConditions() && - $this->validateQuoteConditions($quote) && - $this->validateShippingConditions($quote); + $this->validateQuoteConditions() && + $this->validateShippingConditions(); } public function validateShippingResetConditions(): bool @@ -100,14 +107,14 @@ public function validateShippingResetConditions(): bool !$this->addressReset->isAddressIgnoreRequest(); } - public function validateQuoteConditions(Quote $quote): bool + public function validateQuoteConditions(): bool { - return !$quote->getIsVirtual() && $quote->getItemsCount(); + return !$this->quote->getIsVirtual() && $this->quote->getItemsCount(); } - public function validateShippingConditions(Quote $quote): bool + public function validateShippingConditions(): bool { - $address = $quote->getShippingAddress(); + $address = $this->quote->getShippingAddress(); $shippingIsFine = $address->validate() && !empty($address->getShippingMethod()); return !$shippingIsFine; } diff --git a/src/etc/frontend/di.xml b/src/etc/frontend/di.xml index 4cb876d..42c644c 100644 --- a/src/etc/frontend/di.xml +++ b/src/etc/frontend/di.xml @@ -5,6 +5,6 @@ + type="IntegerNet\ShippingPreselection\Plugin\DeselectShipping" sortOrder="30"/>