Skip to content

Commit 105099e

Browse files
committed
[*] refactoring
* wording > unselect > deselect * quote as property + isDeselectionAllowed
1 parent 76164a7 commit 105099e

File tree

3 files changed

+54
-38
lines changed

3 files changed

+54
-38
lines changed

src/Plugin/UnselectShipping.php renamed to src/Plugin/DeselectShipping.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
use IntegerNet\ShippingPreselection\Service\AddressUnsetMockdata;
1212
use IntegerNet\ShippingPreselection\Service\AddressResetConditions;
1313

14-
class UnselectShipping
14+
class DeselectShipping
1515
{
1616
private AddressUnsetMockdata $addressUnsetMockData;
1717
private AddressResetConditions $addressReset;
1818
private ShippingAddressAssignment $addressAssignment;
1919
private AddressFactory $addressFactory;
20+
private ?Quote $quote;
2021

2122
public function __construct(
2223
AddressUnsetMockdata $addressUnsetMockdata,
@@ -35,30 +36,38 @@ public function __construct(
3536
*/
3637
public function afterGetQuote(Session $subject, Quote $result): Quote
3738
{
38-
$isResetRequest = $this->addressReset->isAddressResetRequest();
39-
$quoteIsValid = !$result->getIsVirtual() && $result->getItemsCount();
40-
if (!$isResetRequest || !$quoteIsValid) {
41-
return $result;
39+
$this->quote = $result;
40+
if (!$this->isDeselectionAllowed()) {
41+
return $this->quote;
4242
}
43-
44-
$address = $result->getShippingAddress();
43+
$address = $this->quote->getShippingAddress();
4544
if ($this->addressUnsetMockData->isMockedAddress($address)) {
46-
$this->addressAssignment->setAddress($result, $this->getNewShippingAddress(), true); // deletion included
45+
$this->addressAssignment->setAddress($this->quote, $this->getNewAddress(), true);
4746
} else {
4847
$this->addressUnsetMockData->checkForEmptyAddressFields($address);
4948
$this->unsetShippingMethod($address);
50-
$this->addressAssignment->setAddress($result, $address);
49+
$this->addressAssignment->setAddress($this->quote, $address);
5150
}
52-
return $result;
51+
return $this->quote;
52+
}
53+
54+
public function isDeselectionAllowed(): bool
55+
{
56+
$isResetRequest = $this->addressReset->isAddressResetRequest();
57+
$quoteIsValid = !$this->quote->getIsVirtual() && $this->quote->getItemsCount();
58+
return $isResetRequest && $quoteIsValid;
5359
}
5460

55-
public function getNewShippingAddress(): Address
61+
public function getNewAddress(): Address
5662
{
5763
return $this->addressFactory->create()->setAddressType(Address::TYPE_SHIPPING);
5864
}
5965

6066
public function unsetShippingMethod(Address $address): void
6167
{
62-
$address->setShippingAmount(0)->setBaseShippingAmount(0)->setShippingMethod('')->setShippingDescription('');
68+
$address->setShippingAmount(0)
69+
->setBaseShippingAmount(0)
70+
->setShippingMethod('')
71+
->setShippingDescription('');
6372
}
6473
}

src/Plugin/PreselectShipping.php

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class PreselectShipping
1818
private AddressSetMockdata $addressSetMockData;
1919
private AddressResetConditions $addressReset;
2020
private ShippingAddressAssignment $addressAssignment;
21+
private ?Quote $quote;
2122

2223
public function __construct(
2324
ShippingMethodManagement $methodManagement,
@@ -36,44 +37,50 @@ public function __construct(
3637
*/
3738
public function afterGetQuote(Session $subject, Quote $result): Quote
3839
{
39-
if (!$this->isPreselectionAllowed($result)) {
40-
return $result;
40+
$this->quote = $result;
41+
if (!$this->isPreselectionAllowed()) {
42+
return $this->quote;
4143
}
42-
$address = $result->getShippingAddress();
43-
if ($this->shouldMockAddress($address)) {
44-
$this->addressSetMockData->setMockDataOnAddress($address);
45-
$this->addressAssignment->setAddress($result, $address);
46-
}
47-
$this->preselectShippingMethod($result);
48-
return $result;
44+
$this->preselectShippingAddress();
45+
$this->preselectShippingMethod();
46+
return $this->quote;
4947
}
5048

5149
public function shouldMockAddress(Address $address): bool
5250
{
5351
return (true !== $address->validate());
5452
}
53+
54+
public function preselectShippingAddress(): void
55+
{
56+
$address = $this->quote->getShippingAddress();
57+
if ($this->shouldMockAddress($address)) {
58+
$this->addressSetMockData->setMockDataOnAddress($address);
59+
$this->addressAssignment->setAddress($this->quote, $address);
60+
}
61+
}
5562

56-
public function preselectShippingMethod(Quote $quote): void
63+
public function preselectShippingMethod(): void
5764
{
58-
$quote->getShippingAddress()->requestShippingRates(); // load new rates
59-
if (!$rate = $this->getCheapestShippingRate($quote)) {
65+
$this->quote->getShippingAddress()->requestShippingRates(); // load new rates
66+
if (!$rate = $this->getCheapestShippingRate()) {
6067
return;
6168
}
6269
try {
6370
$this->methodManagement->set(
64-
$quote->getId(),
71+
$this->quote->getId(),
6572
$rate->getCarrier(),
6673
$rate->getMethod()
6774
);
6875
} catch (\Exception $e) {
69-
$quote->addErrorInfo('error', null, $e->getCode(), $e->getMessage());
76+
$this->quote->addErrorInfo('error', null, $e->getCode(), $e->getMessage());
7077
}
7178
}
7279

73-
public function getCheapestShippingRate(Quote $quote): ?Rate
80+
public function getCheapestShippingRate(): ?Rate
7481
{
7582
$selectedRate = null;
76-
foreach ($this->getShippingRates($quote) as $rate) {
83+
foreach ($this->getShippingRates() as $rate) {
7784
/** @var Rate $rate */
7885
if ($selectedRate === null || $rate->getPrice() < $selectedRate->getPrice()) {
7986
$selectedRate = $rate;
@@ -82,16 +89,16 @@ public function getCheapestShippingRate(Quote $quote): ?Rate
8289
return $selectedRate;
8390
}
8491

85-
public function getShippingRates(Quote $quote)
92+
public function getShippingRates()
8693
{
87-
return $quote->getShippingAddress()->getShippingRatesCollection();
94+
return $this->quote->getShippingAddress()->getShippingRatesCollection();
8895
}
8996

90-
public function isPreselectionAllowed(Quote $quote): bool
97+
public function isPreselectionAllowed(): bool
9198
{
9299
return $this->validateShippingResetConditions() &&
93-
$this->validateQuoteConditions($quote) &&
94-
$this->validateShippingConditions($quote);
100+
$this->validateQuoteConditions() &&
101+
$this->validateShippingConditions();
95102
}
96103

97104
public function validateShippingResetConditions(): bool
@@ -100,14 +107,14 @@ public function validateShippingResetConditions(): bool
100107
!$this->addressReset->isAddressIgnoreRequest();
101108
}
102109

103-
public function validateQuoteConditions(Quote $quote): bool
110+
public function validateQuoteConditions(): bool
104111
{
105-
return !$quote->getIsVirtual() && $quote->getItemsCount();
112+
return !$this->quote->getIsVirtual() && $this->quote->getItemsCount();
106113
}
107114

108-
public function validateShippingConditions(Quote $quote): bool
115+
public function validateShippingConditions(): bool
109116
{
110-
$address = $quote->getShippingAddress();
117+
$address = $this->quote->getShippingAddress();
111118
$shippingIsFine = $address->validate() && !empty($address->getShippingMethod());
112119
return !$shippingIsFine;
113120
}

src/etc/frontend/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<plugin name="integernet_shippingpreselection_preselectshippingmethod"
66
type="IntegerNet\ShippingPreselection\Plugin\PreselectShipping" sortOrder="20"/>
77
<plugin name="integernet_shippingpreselection_resetshippingaddress"
8-
type="IntegerNet\ShippingPreselection\Plugin\UnselectShipping" sortOrder="30"/>
8+
type="IntegerNet\ShippingPreselection\Plugin\DeselectShipping" sortOrder="30"/>
99
</type>
1010
</config>

0 commit comments

Comments
 (0)