-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make cart items implementation extensible in core (#17)
* Fix failure in unit tests * Define Cart Item factory to allow extensibility * Clean unused code * Run PHPUnit before the PHPStan on the pipeline
- Loading branch information
1 parent
31c175e
commit df628aa
Showing
7 changed files
with
109 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/BusinessLogic/Domain/Order/Models/OrderRequest/Item/AbstractItemFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace SeQura\Core\BusinessLogic\Domain\Order\Models\OrderRequest\Item; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* AbstractItemFactory | ||
* | ||
* @package SeQura\Core\BusinessLogic\Domain\Order\Models\OrderRequest\Item | ||
*/ | ||
abstract class AbstractItemFactory | ||
{ | ||
/** | ||
* Create Item object from array. | ||
* | ||
* @param array<string, mixed> $data | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
abstract public function createFromArray(array $data): Item; | ||
|
||
/** | ||
* Create a list of Item objects from an array of data. | ||
* | ||
* @param array<array<string, mixed>> $data Array of arrays containing the data of the items. | ||
* | ||
* @throws InvalidArgumentException | ||
* | ||
* @return Item[] | ||
*/ | ||
public function createListFromArray(array $data): array | ||
{ | ||
$items = []; | ||
foreach ($data as $itemData) { | ||
$item = $this->createFromArray($itemData); | ||
if ($item !== null) { | ||
$items[] = $item; | ||
} | ||
} | ||
return $items; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/BusinessLogic/Domain/Order/Models/OrderRequest/Item/ItemFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace SeQura\Core\BusinessLogic\Domain\Order\Models\OrderRequest\Item; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* AbstractItemFactory implementation | ||
* | ||
* @package SeQura\Core\BusinessLogic\Domain\Order\Models\OrderRequest\Item | ||
*/ | ||
class ItemFactory extends AbstractItemFactory | ||
{ | ||
/** | ||
* Create Item object from array. | ||
* | ||
* @param array<string, mixed> $itemData | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function createFromArray(array $itemData): Item | ||
{ | ||
$type = $itemData['type'] ?? null; | ||
switch ($type) { | ||
case ItemType::TYPE_PRODUCT: | ||
return ProductItem::fromArray($itemData); | ||
case ItemType::TYPE_HANDLING: | ||
return HandlingItem::fromArray($itemData); | ||
case ItemType::TYPE_DISCOUNT: | ||
return DiscountItem::fromArray($itemData); | ||
case ItemType::TYPE_SERVICE: | ||
return ServiceItem::fromArray($itemData); | ||
case ItemType::TYPE_INVOICE_FEE: | ||
return InvoiceFeeItem::fromArray($itemData); | ||
case ItemType::TYPE_OTHER_PAYMENT: | ||
return OtherPaymentItem::fromArray($itemData); | ||
default: | ||
throw new InvalidArgumentException('Invalid cart item type ' . $type); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters