Skip to content

Commit

Permalink
Checkout interface return types changed
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Dec 6, 2023
1 parent 97b2e71 commit deee031
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 40 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
- Added the `Stockable` interface to the `Product` and `MasterProductVariant` models
- Added the `backorder` field to products and product variants
- BC: Added the `?CheckoutSubject` return type to the `getCart()` method of the `Checkout` interface
- BC: Changed `Checkout::getShippingAddress()` return type to be nullable
- BC: Added the void return type to `Checkout::setShippingAddress()`
- BC: Added the `removeShippingAddress()` method to the Checkout interface
- BC: The unused `$config` parameter has been removed from the `RequestStore` checkout driver constructor
- Fixed possible null return type on Billpayer::getName() when is_organization is true but the company name is null

Expand Down
3 changes: 3 additions & 0 deletions src/Checkout/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
- Changed minimal Enum requirement to v4.1
- BC: Added the `?CheckoutSubject` return type to the `getCart()` method of the `Checkout` interface
- BC: The unused `$config` parameter has been removed from the `RequestStore` checkout driver constructor
- BC: Changed `Checkout::getShippingAddress()` return type to be nullable
- BC: Added the void return type to `Checkout::setShippingAddress()`
- BC: Added the `removeShippingAddress()` method to the Checkout interface

## 3.x Series

Expand Down
15 changes: 7 additions & 8 deletions src/Checkout/CheckoutManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,21 @@ public function setBillpayer(Billpayer $billpayer)
return $this->store->setBillpayer($billpayer);
}

/**
* @inheritdoc
*/
public function getShippingAddress(): Address
public function getShippingAddress(): ?Address
{
return $this->store->getShippingAddress();
}

/**
* @inheritDoc
*/
public function setShippingAddress(Address $address)
public function setShippingAddress(Address $address): void
{
$this->store->setShippingAddress($address);
}

public function removeShippingAddress(): void
{
$this->store->removeShippingAddress();
}

public function setCustomAttribute(string $key, $value): void
{
$this->store->setCustomAttribute($key, $value);
Expand Down
18 changes: 4 additions & 14 deletions src/Checkout/Contracts/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,11 @@ public function getBillpayer(): Billpayer;
*/
public function setBillpayer(Billpayer $billpayer);

/**
* Returns the shipping address
*
* @todo make this nullable in v4
*
* @return Address
*/
public function getShippingAddress(): Address;
public function getShippingAddress(): ?Address;

/**
* Sets the shipping address
*
* @param Address $address
*/
public function setShippingAddress(Address $address);
public function setShippingAddress(Address $address): void;

public function removeShippingAddress(): void;

public function setCustomAttribute(string $key, $value): void;

Expand Down
19 changes: 9 additions & 10 deletions src/Checkout/Drivers/RequestStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class RequestStore extends BaseCheckoutStore
/** @var Billpayer */
protected $billpayer;

/** @var Address */
protected $shippingAddress;
protected ?Address $shippingAddress = null;

protected ?string $shippingMethodId = null;

Expand All @@ -66,6 +65,7 @@ public function __construct(CheckoutDataFactory $dataFactory, Request $request =
parent::__construct($dataFactory);
$this->request = $request ?? request();
$this->billpayer = $dataFactory->createBillpayer();
/** @todo examine the request and only create one if there is one */
$this->shippingAddress = $dataFactory->createShippingAddress();
$this->taxes = new DetailedAmountDto(0);
$this->shippingFees = new DetailedAmountDto(0);
Expand All @@ -87,23 +87,22 @@ public function setBillpayer(Billpayer $billpayer)
$this->billpayer = $billpayer;
}

/**
* @inheritdoc
*/
public function getShippingAddress(): Address
public function getShippingAddress(): ?Address
{
return $this->shippingAddress;
}

/**
* @inheritdoc
*/
public function setShippingAddress(Address $address)
public function setShippingAddress(Address $address): void
{
$this->shippingAddress = $address;
Event::dispatch(new ShippingAddressChanged($this));
}

public function removeShippingAddress(): void
{
$this->shippingAddress = null;
}

public function getShippingAmount(): DetailedAmount
{
return $this->shippingFees;
Expand Down
18 changes: 13 additions & 5 deletions src/Checkout/Drivers/SessionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,31 @@ public function __construct(CheckoutDataFactory $factory, Session $session = nul
$this->prefix = $prefix ?? static::DEFAULT_PREFIX;
}

public function getShippingAddress(): Address
public function getShippingAddress(): ?Address
{
$result = $this->factory->createShippingAddress();
if (is_array($rawData = $this->readRawDataFromStore('shipping_address'))) {
$this->fill($result, $rawData);
$rawData = $this->readRawDataFromStore('shipping_address');
if (!is_array($rawData)) {
return null;
}

$result = $this->factory->createShippingAddress();
$this->fill($result, $rawData);

return $result;
}

public function setShippingAddress(Address $address)
public function setShippingAddress(Address $address): void
{
$this->writeRawDataToStore('shipping_address', $address);

Event::dispatch(new ShippingAddressChanged($this));
}

public function removeShippingAddress(): void
{
$this->writeRawDataToStore('shipping_address', null);
}

public function getShippingAmount(): DetailedAmount
{
$raw = $this->readRawDataFromStore('shipping_fees', []);
Expand Down
11 changes: 8 additions & 3 deletions src/Checkout/Tests/Example/MemoryStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MemoryStore implements CheckoutStore

private Billpayer $billpayer;

private Address $shippingAddress;
private ?Address $shippingAddress = null;

private $customAttributes = [];

Expand Down Expand Up @@ -76,16 +76,21 @@ public function setBillpayer(Billpayer $billpayer)
$this->billpayer = $billpayer;
}

public function getShippingAddress(): Address
public function getShippingAddress(): ?Address
{
return $this->shippingAddress;
}

public function setShippingAddress(Address $address)
public function setShippingAddress(Address $address): void
{
$this->shippingAddress = $address;
}

public function removeShippingAddress(): void
{
$this->shippingAddress = null;
}

public function setCustomAttribute(string $key, $value): void
{
$this->customAttributes[$key] = $value;
Expand Down

0 comments on commit deee031

Please sign in to comment.