Skip to content

Commit

Permalink
[*] refactoring
Browse files Browse the repository at this point in the history
  * wording > unselect > deselect
  * quote as property
  + isDeselectionAllowed
  • Loading branch information
sfritzsche committed Feb 11, 2022
1 parent 76164a7 commit 105099e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 38 deletions.
33 changes: 21 additions & 12 deletions src/Plugin/UnselectShipping.php → src/Plugin/DeselectShipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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('');
}
}
57 changes: 32 additions & 25 deletions src/Plugin/PreselectShipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class PreselectShipping
private AddressSetMockdata $addressSetMockData;
private AddressResetConditions $addressReset;
private ShippingAddressAssignment $addressAssignment;
private ?Quote $quote;

public function __construct(
ShippingMethodManagement $methodManagement,
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<plugin name="integernet_shippingpreselection_preselectshippingmethod"
type="IntegerNet\ShippingPreselection\Plugin\PreselectShipping" sortOrder="20"/>
<plugin name="integernet_shippingpreselection_resetshippingaddress"
type="IntegerNet\ShippingPreselection\Plugin\UnselectShipping" sortOrder="30"/>
type="IntegerNet\ShippingPreselection\Plugin\DeselectShipping" sortOrder="30"/>
</type>
</config>

0 comments on commit 105099e

Please sign in to comment.