Skip to content

Commit

Permalink
Added the $hooks and $itemHooks parameters to the OrderFactory
Browse files Browse the repository at this point in the history
…interface
  • Loading branch information
fulopattila122 committed Mar 4, 2024
1 parent ab37a25 commit 9fce6f1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
16 changes: 15 additions & 1 deletion src/Foundation/Factories/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Vanilo\Contracts\CheckoutSubject;
use Vanilo\Contracts\Configurable;
use Vanilo\Order\Contracts\Order;
use Vanilo\Order\Contracts\OrderItem;
use Vanilo\Order\Factories\OrderFactory as BaseOrderFactory;

class OrderFactory extends BaseOrderFactory
Expand All @@ -40,7 +41,7 @@ public function createFromCheckout(Checkout $checkout)

$this->sourceAdjustments = $checkout->getCart()->adjustments();

return $this->createFromDataArray($orderData, $items, \Closure::fromCallable([$this, 'copyAdjustmentsHook']));
return $this->createFromDataArray($orderData, $items, $this->copyAdjustmentsHook(...), $this->copyItemAdjustmentsHook(...));
}

protected function copyAdjustmentsHook(Order $order): void
Expand All @@ -57,13 +58,26 @@ protected function copyAdjustmentsHook(Order $order): void
}
}

protected function copyItemAdjustmentsHook(OrderItem $item, Order $order, array $sourceItems, array $sourceItem): void
{
foreach (($sourceItem['adjustments'] ?? []) as $adjustment) {
$clone = $adjustment->newInstance(
Arr::except($adjustment->getAttributes(), ['id', 'adjustable_type', 'adjustable_id', 'created_at', 'updated_at'])
);
$clone->data = $adjustment->data;
$item->adjustments()->add($clone);
$clone->lock();
}
}

protected function convertCartItemsToDataArray(CheckoutSubject $cart)
{
return $cart->getItems()->map(function ($item) {
return [
'product' => $item->getBuyable(),
'quantity' => $item->getQuantity(),
'configuration' => $item->getBuyable() instanceof Configurable ? $item->configuration() : null,
'adjustments' => $item instanceof Adjustable ? $item->adjustments() : [],
];
})->all();
}
Expand Down
1 change: 1 addition & 0 deletions src/Order/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Changed minimal Enum requirement to v4.2
- Upgraded to Konekt Address and User modules to v3
- Added the `currency` field to the orders table
- BC: Added the `$hooks` and `$itemHooks` parameters to the `OrderFactory` interface
- BC: Changed the `OrderItem` interface into Configurable
- BC: Added 7 methods to the `OrderItem` interface
- BC: Added the `getLanguage()`, `getFulfillmentStatus()` and `itemsTotal()` methods to the `Order` interface
Expand Down
7 changes: 1 addition & 6 deletions src/Order/Contracts/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ interface OrderFactory
{
/**
* Creates a new order from simple data arrays
*
* @param array $data
* @param array $items
*
* @return Order
*/
public function createFromDataArray(array $data, array $items): Order;
public function createFromDataArray(array $data, array $items, array|callable $hooks = null, array|callable $itemHooks = null): Order;
}
44 changes: 10 additions & 34 deletions src/Order/Factories/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ public function __construct(OrderNumberGenerator $generator)
$this->orderNumberGenerator = $generator;
}

/**
* @inheritDoc
*/
public function createFromDataArray(array $data, array $items, callable ...$hooks): Order
public function createFromDataArray(array $data, array $items, array|callable $hooks = null, array|callable $itemHooks = null): Order
{
if (empty($items)) {
throw new CreateOrderException(__('Can not create an order without items'));
Expand All @@ -62,17 +59,9 @@ public function createFromDataArray(array $data, array $items, callable ...$hook
$this->createBillpayer($order, $data);
$this->createShippingAddress($order, $data);

$this->createItems(
$order,
array_map(function ($item) {
// Default quantity is 1 if unspecified
$item['quantity'] = $item['quantity'] ?? 1;

return $item;
}, $items)
);
$this->createItems($order, array_map(fn ($item) => $item + ['quantity' => 1], $items), ...Arr::wrap($itemHooks));

foreach ($hooks as $hook) {
foreach (Arr::wrap($hooks) as $hook) {
$this->callHook($hook, $order, $data, $items);
}

Expand Down Expand Up @@ -115,24 +104,10 @@ protected function createBillpayer(Order $order, array $data)

protected function createItems(Order $order, array $items, callable ...$hooks)
{
$that = $this;
$hasBuyables = collect($items)->contains(function ($item) use ($that) {
return $that->itemContainsABuyable($item);
});

if (!$hasBuyables) { // This is faster
$order->items()->createMany($items);
foreach ($order->getItems() as $createdOrderItem) {
foreach ($hooks as $hook) {
$this->callItemHook($hook, $createdOrderItem, $order, $items);
}
}
} else {
foreach ($items as $item) {
$createdOrderItem = $this->createItem($order, $item);
foreach ($hooks as $hook) {
$this->callItemHook($hook, $createdOrderItem, $order, $items);
}
foreach ($items as $item) {
$createdOrderItem = $this->createItem($order, $item);
foreach ($hooks as $hook) {
$this->callItemHook($hook, $createdOrderItem, $order, $items, $item);
}
}
}
Expand Down Expand Up @@ -177,14 +152,15 @@ protected function callHook(callable $hook, mixed $order, array $data, array $it
/**
* @throws \ReflectionException
*/
protected function callItemHook(callable $hook, OrderItem $orderItem, Order $order, array $sourceItems): void
protected function callItemHook(callable $hook, OrderItem $orderItem, Order $order, array $sourceItems, array $sourceItem): void
{
$ref = new ReflectionFunction($hook);
match ($ref->getNumberOfParameters()) {
0 => $hook(),
1 => $hook($orderItem),
2 => $hook($orderItem, $order),
default => $hook($orderItem, $order, $sourceItems),
3 => $hook($orderItem, $order, $sourceItems),
default => $hook($orderItem, $order, $sourceItems, $sourceItem),
};
}

Expand Down

0 comments on commit 9fce6f1

Please sign in to comment.