Skip to content

Commit

Permalink
Added CheckoutRequest interface + Checkout refactoring - IN PROG
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Dec 17, 2023
1 parent 99911ee commit 66e45be
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 9 deletions.
39 changes: 39 additions & 0 deletions src/Checkout/Contracts/CheckoutRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/**
* Contains the CheckoutRequest interface.
*
* @copyright Copyright (c) 2023 Vanilo UG
* @author Attila Fulop
* @license MIT
* @since 2023-12-06
*
*/

namespace Vanilo\Checkout\Contracts;

use ArrayAccess;
use Vanilo\Contracts\Address;

interface CheckoutRequest extends ArrayAccess
{
public function has($key);

public function all($keys = null);

public function input($key = null, $default = null);

public function old($key = null, $default = null);

public function replace(array $input);

public function doesntWantShipping(): bool;

public function wantsShipping(): bool;

public function wantsShippingToBillingAddress(): bool;

public function getShippingAddress(): null|array|Address;
}
16 changes: 13 additions & 3 deletions src/Checkout/Drivers/BaseCheckoutStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Vanilo\Checkout\Events\ShippingMethodSelected;
use Vanilo\Checkout\Models\CheckoutStateProxy;
use Vanilo\Checkout\Traits\EmulatesFillAttributes;
use Vanilo\Contracts\Address;
use Vanilo\Contracts\Billpayer;
use Vanilo\Contracts\CheckoutSubject;
use Vanilo\Contracts\DetailedAmount;
Expand Down Expand Up @@ -248,12 +249,21 @@ protected function updateBillpayer($data): void
$this->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
Expand Down
112 changes: 112 additions & 0 deletions src/Checkout/Drivers/ImplicitCheckoutRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

/**
* Contains the ImplicitCheckoutRequest class.
*
* @copyright Copyright (c) 2023 Vanilo UG
* @author Attila Fulop
* @license MIT
* @since 2023-12-06
*
*/

namespace Vanilo\Checkout\Drivers;

use Illuminate\Http\Request;
use Vanilo\Checkout\Contracts\CheckoutRequest;
use Vanilo\Contracts\Address;

/**
* This class represents a checkout request that implicitly contains
* the fields required for the checkout, without implementing the
* CheckoutRequest interface. It decorates the via duck-typing
*/
final class ImplicitCheckoutRequest implements CheckoutRequest
{
public function __construct(
private Request $request,
) {
}

public static function from(Request|CheckoutRequest $request): CheckoutRequest
{
if ($request instanceof CheckoutRequest) {
return $request;
}

return new self($request);
}

public function doesntWantShipping(): bool
{
return (bool) $this->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);
}
}
14 changes: 8 additions & 6 deletions src/Checkout/Drivers/RequestStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,7 +37,6 @@ class RequestStore extends BaseCheckoutStore
{
use HasCheckoutState;
use EmulatesFillAttributes;
use FillsCommonCheckoutAttributes;

protected $state;

Expand All @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/Stockable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface Stockable
{
public function isOnStock(): bool;

public function isOutOfStock(): bool;

public function onStockQuantity(): float;

public function isBackorderUnrestricted(): bool;
Expand Down

0 comments on commit 66e45be

Please sign in to comment.