From 66e45be0eab836f5d26f72a2fa9f9d374237bf73 Mon Sep 17 00:00:00 2001 From: Attila Fulop <1162360+fulopattila122@users.noreply.github.com> Date: Sun, 17 Dec 2023 09:03:29 +0200 Subject: [PATCH] Added `CheckoutRequest` interface + Checkout refactoring - IN PROG --- src/Checkout/Contracts/CheckoutRequest.php | 39 ++++++ src/Checkout/Drivers/BaseCheckoutStore.php | 16 ++- .../Drivers/ImplicitCheckoutRequest.php | 112 ++++++++++++++++++ src/Checkout/Drivers/RequestStore.php | 14 ++- src/Contracts/Stockable.php | 2 + 5 files changed, 174 insertions(+), 9 deletions(-) create mode 100644 src/Checkout/Contracts/CheckoutRequest.php create mode 100644 src/Checkout/Drivers/ImplicitCheckoutRequest.php diff --git a/src/Checkout/Contracts/CheckoutRequest.php b/src/Checkout/Contracts/CheckoutRequest.php new file mode 100644 index 00000000..8650dee6 --- /dev/null +++ b/src/Checkout/Contracts/CheckoutRequest.php @@ -0,0 +1,39 @@ +setBillpayer($billpayer); } - protected function updateShippingAddress($data): void + protected function updateShippingAddress(null|array|Address $data): void { - if (null !== $shippingAddress = $this->getShippingAddress()) { + if (empty($data)) { + $this->removeShippingAddress(); + return; + } + + if ($data instanceof Address) { + $shippingAddress = $data; + } else { + $shippingAddress = $this->getShippingAddress() ?? $this->factory->createShippingAddress(); $this->fill($shippingAddress, $data); - $this->setShippingAddress($shippingAddress); } + + $this->setShippingAddress($shippingAddress); } protected function updateShipToBillingAddress($data): void diff --git a/src/Checkout/Drivers/ImplicitCheckoutRequest.php b/src/Checkout/Drivers/ImplicitCheckoutRequest.php new file mode 100644 index 00000000..99c76594 --- /dev/null +++ b/src/Checkout/Drivers/ImplicitCheckoutRequest.php @@ -0,0 +1,112 @@ +input('no_shipping'); + } + + public function wantsShipping(): bool + { + return !$this->doesntWantShipping(); + } + + public function wantsShippingToBillingAddress(): bool + { + return (bool) $this->input('ship_to_billing_address'); + } + + public function getShippingAddress(): null|array|Address + { + $address = $this->input('shipping_address'); + + return match (true) { + empty($address) => null, + is_array($address) => $address, + default => null, + }; + } + + public function has($key) + { + return $this->request->has($key); + } + + public function all($keys = null) + { + return $this->request->all($keys); + } + + public function input($key = null, $default = null) + { + return $this->request->input($key, $default); + } + + public function old($key = null, $default = null) + { + return $this->request->old($key, $default); + } + + public function replace(array $input) + { + return $this->request->replace($input); + } + + public function offsetExists(mixed $offset): bool + { + return $this->request->offsetExists($offset); + } + + public function offsetGet(mixed $offset): mixed + { + return $this->request->offsetGet($offset); + } + + public function offsetSet(mixed $offset, mixed $value): void + { + $this->request->offsetSet($offset, $value); + } + + public function offsetUnset(mixed $offset): void + { + $this->request->offsetUnset($offset); + } +} diff --git a/src/Checkout/Drivers/RequestStore.php b/src/Checkout/Drivers/RequestStore.php index 686711d4..af607523 100644 --- a/src/Checkout/Drivers/RequestStore.php +++ b/src/Checkout/Drivers/RequestStore.php @@ -18,6 +18,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\Event; use Vanilo\Checkout\Contracts\CheckoutDataFactory; +use Vanilo\Checkout\Contracts\CheckoutRequest; use Vanilo\Checkout\Events\ShippingAddressChanged; use Vanilo\Checkout\Traits\EmulatesFillAttributes; use Vanilo\Checkout\Traits\FillsCommonCheckoutAttributes; @@ -36,7 +37,6 @@ class RequestStore extends BaseCheckoutStore { use HasCheckoutState; use EmulatesFillAttributes; - use FillsCommonCheckoutAttributes; protected $state; @@ -55,18 +55,20 @@ class RequestStore extends BaseCheckoutStore protected DetailedAmount $taxes; - protected Request $request; + protected CheckoutRequest $request; /** @var array */ protected $customData = []; - public function __construct(CheckoutDataFactory $dataFactory, Request $request = null) + public function __construct(CheckoutDataFactory $dataFactory, Request|CheckoutRequest $request = null) { parent::__construct($dataFactory); - $this->request = $request ?? request(); + $this->request = ImplicitCheckoutRequest::from($request ?? request()); $this->billpayer = $dataFactory->createBillpayer(); - /** @todo examine the request and only create one if there is one */ - $this->shippingAddress = $dataFactory->createShippingAddress(); + if ($this->request->wantsShipping()) { + $this->updateShippingAddress($this->request->getShippingAddress()); + $this->shippingAddress = $dataFactory->createShippingAddress(); + } $this->taxes = new DetailedAmountDto(0); $this->shippingFees = new DetailedAmountDto(0); } diff --git a/src/Contracts/Stockable.php b/src/Contracts/Stockable.php index b2571efe..5bb1703d 100644 --- a/src/Contracts/Stockable.php +++ b/src/Contracts/Stockable.php @@ -18,6 +18,8 @@ interface Stockable { public function isOnStock(): bool; + public function isOutOfStock(): bool; + public function onStockQuantity(): float; public function isBackorderUnrestricted(): bool;