Skip to content

Commit

Permalink
[TASK] Replace parent BeVariant and Product with $parent and a union …
Browse files Browse the repository at this point in the history
…type

Relates: #611
  • Loading branch information
extcode committed Jan 7, 2025
1 parent dde4d81 commit ef5413f
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 157 deletions.
20 changes: 0 additions & 20 deletions Build/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,6 @@ parameters:
count: 1
path: ../Classes/Controller/Cart/OrderController.php

-
message: "#^Deprecated in PHP 8\\.0\\: Required parameter \\$price follows optional parameter \\$beVariant\\.$#"
count: 1
path: ../Classes/Domain/Model/Cart/BeVariant.php

-
message: "#^Deprecated in PHP 8\\.0\\: Required parameter \\$priceCalcMethod follows optional parameter \\$beVariant\\.$#"
count: 1
path: ../Classes/Domain/Model/Cart/BeVariant.php

-
message: "#^Deprecated in PHP 8\\.0\\: Required parameter \\$sku follows optional parameter \\$beVariant\\.$#"
count: 1
path: ../Classes/Domain/Model/Cart/BeVariant.php

-
message: "#^Deprecated in PHP 8\\.1\\: Required parameter \\$title follows optional parameter \\$beVariant\\.$#"
count: 1
path: ../Classes/Domain/Model/Cart/BeVariant.php

-
message: "#^Call to an undefined method Extcode\\\\Cart\\\\Domain\\\\Model\\\\Cart\\\\CartCouponInterface\\:\\:getTaxClass\\(\\)\\.$#"
count: 2
Expand Down
104 changes: 32 additions & 72 deletions Classes/Domain/Model/Cart/BeVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

class BeVariant
{
protected ?Product $product = null;

protected ?BeVariant $parentBeVariant = null;

protected string $titleDelimiter = ' - ';

protected string $skuDelimiter = '-';
Expand Down Expand Up @@ -45,30 +41,13 @@ class BeVariant

public function __construct(
protected string $id,
?Product $product = null,
?self $beVariant = null,
protected self|Product $parent,
protected string $title,
protected string $sku,
protected int $priceCalcMethod,
protected float $price,
protected int $quantity = 0
) {
if ($product === null && $beVariant === null) {
throw new \InvalidArgumentException();
}

if ($product != null && $beVariant != null) {
throw new \InvalidArgumentException();
}

if ($product !== null) {
$this->product = $product;
}

if ($beVariant !== null) {
$this->parentBeVariant = $beVariant;
}

$this->reCalc();
}

Expand Down Expand Up @@ -102,36 +81,28 @@ public function toArray(): array
return $variantArr;
}

public function getProduct(): ?Product
public function getParent(): self|Product
{
return $this->product;
return $this->parent;
}

public function setProduct(Product $product): void
public function setParent(self|Product $parent): void
{
$this->product = $product;
$this->parent = $parent;
}

public function getParentBeVariant(): ?self
public function getProduct(): Product
{
return $this->parentBeVariant;
}
if ($this->parent instanceof Product) {
return $this->parent;
}

public function setParentBeVariant(self $parentBeVariant): void
{
$this->parentBeVariant = $parentBeVariant;
return $this->parent->getProduct();
}

public function isNetPrice(): bool
{
if ($this->getParentBeVariant()) {
return $this->getParentBeVariant()->isNetPrice();
}
if ($this->getProduct()) {
return $this->getProduct()->isNetPrice();
}

return false;
return $this->parent->isNetPrice();
}

public function getId(): string
Expand All @@ -158,10 +129,10 @@ public function getCompleteTitle(): string
{
$title = '';

if ($this->getParentBeVariant()) {
$title = $this->getParentBeVariant()->getCompleteTitle();
} elseif ($this->getProduct()) {
$title = $this->getProduct()->getTitle();
if ($this->parent instanceof self) {
$title = $this->parent->getCompleteTitle();
} elseif ($this->parent instanceof Product) {
$title = $this->parent->getTitle();
}

if ($this->isFeVariant) {
Expand All @@ -178,8 +149,8 @@ public function getCompleteTitleWithoutProduct(): string
$title = '';
$titleDelimiter = '';

if ($this->getParentBeVariant()) {
$title = $this->getParentBeVariant()->getCompleteTitleWithoutProduct();
if ($this->parent instanceof self) {
$title = $this->parent->getCompleteTitleWithoutProduct();
$titleDelimiter = $this->titleDelimiter;
}

Expand Down Expand Up @@ -244,12 +215,12 @@ public function getPriceCalculated(): float
{
$price = $this->getBestPrice();

if ($this->getParentBeVariant()) {
$parentPrice = $this->getParentBeVariant()->getBestPrice();
} elseif ($this->getProduct()) {
$parentPrice = $this->getProduct()->getBestPrice($this->getQuantity());
if ($this->parent instanceof self) {
$parentPrice = $this->parent->getBestPrice();
} elseif ($this->parent instanceof Product) {
$parentPrice = $this->parent->getBestPrice($this->getQuantity());
} else {
$parentPrice = 0;
$parentPrice = 0.0;
}

if ($this->priceCalcMethod === 0) {
Expand Down Expand Up @@ -285,15 +256,11 @@ public function getParentPrice(): float
return 0.0;
}

if ($this->getParentBeVariant()) {
return $this->getParentBeVariant()->getBestPrice();
if ($this->parent instanceof self) {
return $this->parent->getBestPrice();
}

if ($this->getProduct()) {
return $this->getProduct()->getBestPrice($this->getQuantity());
}

return 0.0;
return $this->parent->getBestPrice($this->getQuantity());
}

public function setPrice(float $price): void
Expand Down Expand Up @@ -332,10 +299,10 @@ public function getCompleteSku(): string
{
$sku = '';

if ($this->getParentBeVariant()) {
$sku = $this->getParentBeVariant()->getCompleteSku();
} elseif ($this->getProduct()) {
$sku = $this->getProduct()->getSku();
if ($this->parent instanceof self) {
$sku = $this->parent->getCompleteSku();
} elseif ($this->parent instanceof Product) {
$sku = $this->parent->getSku();
}

if ($this->isFeVariant) {
Expand All @@ -353,8 +320,8 @@ public function getCompleteSkuWithoutProduct(): string

$skuDelimiter = '';

if ($this->getParentBeVariant()) {
$sku = $this->getParentBeVariant()->getCompleteSkuWithoutProduct();
if ($this->parent instanceof self) {
$sku = $this->parent->getCompleteSkuWithoutProduct();
$skuDelimiter = $this->titleDelimiter;
}

Expand Down Expand Up @@ -416,14 +383,7 @@ public function getTax(): float

public function getTaxClass(): ?TaxClass
{
if ($this->getParentBeVariant()) {
return $this->getParentBeVariant()->getTaxClass();
}
if ($this->getProduct()) {
return $this->getProduct()->getTaxClass();
}

return null;
return $this->parent->getTaxClass();
}

public function setQuantity(int $newQuantity): void
Expand Down
2 changes: 1 addition & 1 deletion Classes/Domain/Model/Cart/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function addBeVariant(BeVariant $newVariant): void
$variant->setQuantity($newQuantity);
}
} else {
$newVariant->setProduct($this);
$newVariant->setParent($this);
$this->beVariants[$newVariantsId] = $newVariant;
}

Expand Down
21 changes: 2 additions & 19 deletions Classes/EventListener/Order/Create/PersistOrder/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,7 @@ protected function addVariantsOfVariant(BeVariant $variant, int $level): void

protected function addBeVariant(BeVariant $variant, int $level): void
{
$variantInner = $variant;
for ($count = $level; $count > 0; $count--) {
if ($count > 1) {
$variantInner = $variantInner->getParentBeVariant();
} else {
$cartProduct = $variantInner->getProduct();
}
}
unset($variantInner);

if (!isset($cartProduct)) {
// ToDo Add Error Message
return;
}
$cartProduct = $variant->getProduct();

$orderProduct = GeneralUtility::makeInstance(
\Extcode\Cart\Domain\Model\Order\Product::class
Expand Down Expand Up @@ -193,11 +180,7 @@ protected function addBeVariant(BeVariant $variant, int $level): void

$orderProduct->addProductAdditional($orderProductAdditional);

if ($count > 1) {
$variantInner = $variantInner->getParentBeVariant();
} else {
$cartProduct = $variantInner->getProduct();
}
$variantInner = $variantInner->getParent();
}
unset($variantInner);

Expand Down
9 changes: 5 additions & 4 deletions Classes/ViewHelpers/FieldNameViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ protected function getVariantFieldName(BeVariant $variant): string
{
$fieldName = '';

if ($variant->getParentBeVariant()) {
$fieldName .= $this->getVariantFieldName($variant->getParentBeVariant());
if ($variant->getParent() instanceof BeVariant) {
$fieldName .= $this->getVariantFieldName($variant->getParent());
}
if ($variant->getProduct()) {
$fieldName .= '[' . $variant->getProduct()->getId() . ']';

if ($variant->getParent() instanceof Product) {
$fieldName .= '[' . $variant->getParent()->getId() . ']';
}

$fieldName .= '[' . $variant->getId() . ']';
Expand Down
33 changes: 33 additions & 0 deletions Documentation/Changelog/10.0/Breaking-611-ChangePhpDependency.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.. include:: ../../Includes.rst.txt

=================================================
Breaking: #611 - Change BeVariant Parent Handling
=================================================

See `Issue 611 <https://github.com/extcode/cart/issues/611>`__

Description
===========

Previously, a `BeVariant` had either another `BeVariant` or a `Product` as its parent element. These were passed in the
constructor method.

Thanks to PHP's union types, this can now be resolved. A `BeVariant` now has a `$parent` that is either of type
`BeVariant` or of type `Product`.

The individual methods have been adapted accordingly and it is checked of which class `$parent` is an instance. In some
cases, the case differentiation could be omitted completely.

Affected Installations
======================

All product extensions that use their own `BeVariant` in the products are affected. The provided extensions
extcode/cart-products and extcode/cart-events will be adapted accordingly.

Migration
=========

If a custom product extension is used, the constructor must be adapted accordingly. Furthermore, a few
methods in `BeVariant` have been replaced or their behavior adapted.

.. index:: API
20 changes: 20 additions & 0 deletions Documentation/Changelog/10.0/Index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.. include:: ../../Includes.rst.txt

10.0 Changes
============

**Table of contents**

.. contents::
:local:
:depth: 1

Breaking
--------

.. toctree::
:maxdepth: 1
:titlesonly:
:glob:

Breaking-*
1 change: 1 addition & 0 deletions Documentation/Changelog/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ChangeLog
:maxdepth: 5
:titlesonly:

10.0/Index
9.3/Index
9.1/Index
9.0/Index
Expand Down
Loading

0 comments on commit ef5413f

Please sign in to comment.