Skip to content

Commit

Permalink
Move business logic out of components into extensions
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed May 11, 2024
1 parent 2ee0bfc commit 50f210b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/Classes/CartManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

namespace Igniter\Cart\Classes;

use Exception;
use Igniter\Cart\CartCondition;
use Igniter\Cart\CartItem;
use Igniter\Cart\Exceptions\InvalidRowIDException;
use Igniter\Cart\Models\CartSettings;
use Igniter\Cart\Models\Menu;
use Igniter\Cart\Models\MenuItemOption;
use Igniter\Cart\Models\MenuItemOptionValue;
use Igniter\Cart\Models\Order;
use Igniter\Coupons\Models\Coupon;
use Igniter\Flame\Exception\ApplicationException;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Event;
Expand Down Expand Up @@ -454,4 +457,63 @@ public function deliveryChargeIsUnavailable()
return $this->location->orderTypeIsDelivery()
&& $this->location->deliveryAmount($this->cart->subtotal()) < 0;
}

//
// Reorder
//

public function addOrderMenus(Order $order)
{
$notes = [];

foreach ($order->getOrderMenus() as $orderMenu) {
try {
throw_unless($orderMenu->menu, new ApplicationException(
lang('igniter.cart::default.alert_menu_not_found')
));

$this->validateCartMenuItem($orderMenu->menu, $orderMenu->quantity);

if (is_string($orderMenu->option_values)) {
$orderMenu->option_values = @unserialize($orderMenu->option_values);
}

if ($orderMenu->option_values instanceof Arrayable) {
$orderMenu->option_values = $orderMenu->option_values->toArray();
}

$options = $this->prepareCartItemOptionsFromOrderMenu($orderMenu->menu, $orderMenu->option_values, $notes);

$this->cart->add($orderMenu->menu, $orderMenu->quantity, $options, $orderMenu->comment);
} catch (Exception $ex) {
$notes[] = $ex->getMessage();
}
}

return $notes;
}

protected function prepareCartItemOptionsFromOrderMenu($menuModel, $optionValues, &$notes)
{
$options = [];
foreach ($optionValues as $cartOption) {
if (!$menuOption = $menuModel->menu_options->keyBy('menu_option_id')->get($cartOption['id'])) {
continue;
}

try {
$this->validateMenuItemOption($menuOption, $cartOption['values']->toArray());

$cartOption['values'] = $cartOption['values']->filter(function ($cartOptionValue) use ($menuOption) {
return $menuOption->menu_option_values->keyBy('menu_option_value_id')->has($cartOptionValue->id);
})->toArray();

$options[] = $cartOption;
} catch (Exception $ex) {
$notes[] = $ex->getMessage();
}
}

return $options;
}
}
4 changes: 4 additions & 0 deletions src/Classes/OrderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public function loadOrder()
$order = Order::make($this->getCustomerAttributes());
}

if (!$order->isPaymentProcessed()) {
$this->applyRequiredAttributes($order);
}

return $order;
}

Expand Down
22 changes: 22 additions & 0 deletions src/Models/CartSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,26 @@ public function getConditionsAttribute($value)
//
//
//

public function tippingEnabled()
{
return (bool)self::get('enable_tipping');

Check failure on line 40 in src/Models/CartSettings.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - Static Analysis

Call to an undefined static method Igniter\Cart\Models\CartSettings::get().

Check failure on line 40 in src/Models/CartSettings.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - Static Analysis

Call to an undefined static method Igniter\Cart\Models\CartSettings::get().
}

public function tippingAmounts()
{
$result = [];

$tipValueType = self::get('tip_value_type', 'F');

Check failure on line 47 in src/Models/CartSettings.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - Static Analysis

Call to an undefined static method Igniter\Cart\Models\CartSettings::get().

Check failure on line 47 in src/Models/CartSettings.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - Static Analysis

Call to an undefined static method Igniter\Cart\Models\CartSettings::get().
$amounts = (array)self::get('tip_amounts', []);

Check failure on line 48 in src/Models/CartSettings.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - Static Analysis

Call to an undefined static method Igniter\Cart\Models\CartSettings::get().

Check failure on line 48 in src/Models/CartSettings.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - Static Analysis

Call to an undefined static method Igniter\Cart\Models\CartSettings::get().

$amounts = sort_array($amounts, 'priority');

foreach ($amounts as $index => $amount) {
$amount['valueType'] = $tipValueType;
$result[$index] = (object)$amount;
}

return $result;
}
}

0 comments on commit 50f210b

Please sign in to comment.