Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed May 19, 2024
1 parent 22f0440 commit 9578ea9
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 37 deletions.
2 changes: 0 additions & 2 deletions config/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,5 @@

'destroyOnLogout' => false,

'conditions' => [],

'abandonedCart' => false,
];
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ parameters:
count: 1
path: src/Classes/CartManager.php

-
message: "#^Access to an undefined property Igniter\\\\Cart\\\\Models\\\\Order\\:\\:\\$location_id\\.$#"
count: 1
path: src/Classes/CartManager.php

-
message: "#^Call to an undefined static method Illuminate\\\\Support\\\\Facades\\\\Event\\:\\:fire\\(\\)\\.$#"
count: 1
Expand Down
7 changes: 6 additions & 1 deletion pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
"try"
]
},
"single_blank_line_at_eof": false
"single_blank_line_at_eof": false,
"single_space_around_construct": false,
"function_declaration": {
"closure_function_spacing": "none",
"closure_fn_spacing": "none"
}
}
}
3 changes: 2 additions & 1 deletion resources/lang/en/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
'button_apply_tip' => 'Apply Tip',
'button_view_cart' => 'Back to My Order',

'column_condition_name' => 'Name',
'column_condition_name' => 'Unique code',
'column_condition_priority' => 'Priority',
'column_condition_title' => 'Label',
'column_tip_amount' => 'Amount',
Expand Down Expand Up @@ -192,6 +192,7 @@
'text_no_payment' => 'No payment method selected',
'text_comment' => 'Your comment',
'text_no_comment' => 'You did not leave a comment',
'text_checkout_terms' => 'checkout terms and conditions',

'label_customer_name' => 'Customer Name',
'label_first_name' => 'First Name',
Expand Down
8 changes: 4 additions & 4 deletions resources/models/cartsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@
'label' => 'lang:igniter.cart::default.column_condition_priority',
'type' => 'hidden',
],
'label' => [
'label' => 'lang:igniter.cart::default.column_condition_title',
'type' => 'text',
],
'name' => [
'label' => 'lang:igniter.cart::default.column_condition_name',
'type' => 'text',
'attributes' => [
'readonly' => true,
],
],
'label' => [
'label' => 'lang:igniter.cart::default.column_condition_title',
'type' => 'text',
],
'status' => [
'label' => 'lang:admin::lang.label_status',
'type' => 'switch',
Expand Down
4 changes: 4 additions & 0 deletions resources/models/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@
'type' => 'permalink',
'comment' => 'lang:igniter::admin.help_permalink',
'span' => 'right',
'preset' => [
'field' => 'name',
'type' => 'slug',
],
],
'parent_id' => [
'label' => 'lang:igniter.cart::default.categories.label_parent',
Expand Down
27 changes: 22 additions & 5 deletions src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function currentInstance()
public function add($buyable, $qty = 0, array $options = [], $comment = null)
{
if ($this->isMulti($buyable)) {
return array_map(function ($item) {
return array_map(function($item) {
return $this->add($item);
}, $buyable);
}
Expand Down Expand Up @@ -352,7 +352,7 @@ public function clearConditions()
{
$this->fireEvent('condition.clearing');

$this->getConditions()->each(function (CartCondition $condition) {
$this->getConditions()->each(function(CartCondition $condition) {
$condition->clearMetaData();
});

Expand Down Expand Up @@ -444,7 +444,7 @@ protected function loadItemsCondition($condition)
{
$content = $this->getContent();

$content->each(function (CartItem $cartItem) use ($condition) {
$content->each(function(CartItem $cartItem) use ($condition) {
$this->applyConditionToItem($condition, $cartItem);
});

Expand All @@ -460,7 +460,7 @@ protected function removeItemCondition($condition)
{
$content = $this->getContent();

$content->each(function (CartItem $cartItem) use ($condition) {
$content->each(function(CartItem $cartItem) use ($condition) {
$cartItem->conditions->forget($condition->name);
});

Expand All @@ -474,7 +474,7 @@ protected function clearItemConditions()
{
$content = $this->getContent();

$content->each(function (CartItem $cartItem) {
$content->each(function(CartItem $cartItem) {
$cartItem->clearConditions();
});

Expand Down Expand Up @@ -659,6 +659,23 @@ protected function createModel()
// Session
//

public function keepSession(Closure $callback)
{
if (config('igniter-cart.destroyOnLogout')) {
return $callback();
}

$cartContent = $this->getContent();
$cartConditions = $this->getConditions();

$result = $callback();

$this->putSession('content', $cartContent);
$this->putSession('conditions', $cartConditions);

return $result;
}

protected function getSession($key, $default = null)
{
return $this->session->get(sprintf('cart.%s.%s', $this->instance, $key), $default);
Expand Down
21 changes: 13 additions & 8 deletions src/Classes/CartManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ protected function prepareCartMenuItemOptions(Collection $menuOptions, array $se
$selected = collect($selected);
$menuOptions = $menuOptions->keyBy('menu_option_id')->sortBy('priority');

return $menuOptions->map(function (MenuItemOption $menuOption) use ($selected) {
return $menuOptions->map(function(MenuItemOption $menuOption) use ($selected) {
$selectedOption = $selected->get($menuOption->getKey());
$selectedValues = array_filter((array)array_get($selectedOption, 'option_values', []));

Expand Down Expand Up @@ -248,7 +248,7 @@ protected function prepareCartItemOptionValues(Collection $menuOptionValues, arr
$menuOptionValues = $menuOptionValues->keyBy('menu_option_value_id')->sortBy('priority');

return $menuOptionValues
->map(function (MenuItemOptionValue $optionValue) use ($selectedValues) {
->map(function(MenuItemOptionValue $optionValue) use ($selectedValues) {
$selectedIds = array_column($selectedValues, 'id') ?: $selectedValues;
if (!in_array($optionValue->menu_option_value_id, $selectedIds)
&& !(array_get($selectedIds, $optionValue->menu_option_value_id) === true
Expand Down Expand Up @@ -280,14 +280,14 @@ public function validateContents()
throw new ApplicationException(lang('igniter.cart::default.checkout.alert_no_menu_to_order'));
}

$this->cart->content()->each(function (CartItem $cartItem) {
$this->cart->content()->each(function(CartItem $cartItem) {
$menuItem = $cartItem->model;

$this->validateCartMenuItem($menuItem, $cartItem->qty);

$menuOptions = $menuItem->menu_options->keyBy('menu_option_id');

$cartItem->options->each(function ($cartItemOption) use ($menuOptions) {
$cartItem->options->each(function($cartItemOption) use ($menuOptions) {
throw_unless($menuItemOption = $menuOptions->get($cartItemOption->id), new ApplicationException(
lang('igniter.cart::default.alert_option_not_found')
));
Expand Down Expand Up @@ -343,7 +343,7 @@ public function validateMenuItem(Menu $menuItem)
sprintf(
lang('igniter.cart::default.alert_menu_not_within_mealtimes'),
$menuItem->menu_name,
$menuItem->mealtimes->map(function ($mealtime) {
$menuItem->mealtimes->map(function($mealtime) {
return sprintf(
lang('igniter.cart::default.alert_menu_not_within_mealtimes_option'),
$mealtime->mealtime_name,
Expand Down Expand Up @@ -413,7 +413,7 @@ public function validateMenuItemOption(MenuItemOption $menuOption, $selectedValu
}

if ($menuOption->display_type == 'quantity') {
$countSelected = array_reduce($selectedValues, function ($qty, $selectedValue) {
$countSelected = array_reduce($selectedValues, function($qty, $selectedValue) {
return $qty + $selectedValue['qty'];
});
} else {
Expand Down Expand Up @@ -462,10 +462,13 @@ public function deliveryChargeIsUnavailable()
// Reorder
//

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

$currentInstance = $this->cart->currentInstance();
$this->cart->instance('location-'.$order->location_id);

foreach ($order->getOrderMenus() as $orderMenu) {
try {
throw_unless($orderMenu->menu, new ApplicationException(
Expand All @@ -490,6 +493,8 @@ public function addOrderMenus(Order $order)
}
}

$this->cart->instance($currentInstance);

return $notes;
}

Expand All @@ -504,7 +509,7 @@ protected function prepareCartItemOptionsFromOrderMenu($menuModel, $optionValues
try {
$this->validateMenuItemOption($menuOption, $cartOption['values']->toArray());

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

Expand Down
26 changes: 13 additions & 13 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function boot()

LocationModel::implement(LocationAction::class);

Customers::extendFormFields(function (Form $form) {
Customers::extendFormFields(function(Form $form) {
if (!$form->model instanceof Customer) {
return;
}
Expand Down Expand Up @@ -336,15 +336,15 @@ public function registerLocationSettings()

protected function bindCartEvents()
{
Event::listen('igniter.user.login', function () {
Event::listen('igniter.user.login', function() {
if (Models\CartSettings::get('abandoned_cart')
&& Facades\Cart::content()->isEmpty()
) {
Facades\Cart::restore(Auth::getId());
}
});

Event::listen('igniter.user.logout', function () {
Event::listen('igniter.user.logout', function() {
if (Models\CartSettings::get('destroy_on_logout')) {
Facades\Cart::destroy();
}
Expand All @@ -353,7 +353,7 @@ protected function bindCartEvents()

protected function bindCheckoutEvents()
{
Event::listen('payregister.paypalexpress.extendFields', function ($payment, &$fields, $order, $data) {
Event::listen('payregister.paypalexpress.extendFields', function($payment, &$fields, $order, $data) {
if ($tax = $order->getOrderTotals()->firstWhere('code', 'tax')) {
$fields['purchase_units'][0]['amount']['breakdown']['tax_total'] = [
'currency_code' => $fields['purchase_units'][0]['amount']['currency_code'],
Expand All @@ -362,38 +362,38 @@ protected function bindCheckoutEvents()
}
});

Event::listen('admin.order.paymentProcessed', function (Order $model) {
Event::listen('admin.order.paymentProcessed', function(Order $model) {
Notifications\OrderCreatedNotification::make()->subject($model)->broadcast();

$model->mailSend('igniter.cart::mail.order', 'customer');
$model->mailSend('igniter.cart::mail.order_alert', 'location');
$model->mailSend('igniter.cart::mail.order_alert', 'admin');
});

Event::listen('admin.order.beforePaymentProcessed', function (Order $model) {
Event::listen('admin.order.beforePaymentProcessed', function(Order $model) {
$model->subtractStock();
});
}

protected function bindOrderStatusEvent()
{
Event::listen('admin.statusHistory.beforeAddStatus', function ($model, $object, $statusId, $previousStatus) {
Event::listen('admin.statusHistory.beforeAddStatus', function($model, $object, $statusId, $previousStatus) {
if (!$object instanceof Order) {
return;
}

Event::fire('igniter.cart.beforeAddOrderStatus', [$model, $object, $statusId, $previousStatus], true);
});

Event::listen('admin.statusHistory.added', function ($model, $statusHistory) {
Event::listen('admin.statusHistory.added', function($model, $statusHistory) {
if (!$model instanceof Order) {
return;
}

Event::fire('igniter.cart.orderStatusAdded', [$model, $statusHistory], true);
});

Event::listen('admin.assignable.assigned', function ($model) {
Event::listen('admin.assignable.assigned', function($model) {
if (!$model instanceof Order) {
return;
}
Expand All @@ -404,24 +404,24 @@ protected function bindOrderStatusEvent()

protected function registerCart(): void
{
$this->app->singleton('cart', function ($app) {
$this->app->singleton('cart', function($app) {
$this->app['config']->set('igniter-cart.model', Models\Cart::class);
$this->app['config']->set('igniter-cart.abandonedCart', Models\CartSettings::get('abandoned_cart'));
$this->app['config']->set('igniter-cart.destroyOnLogout', Models\CartSettings::get('destroy_on_logout'));

$this->app['events']->fire('cart.beforeRegister', [$this]);
$this->app['events']->dispatch('cart.beforeRegister', [$this]);

$instance = new Cart($app['session'], $app['events']);

$this->app['events']->fire('cart.afterRegister', [$instance, $this]);
$this->app['events']->dispatch('cart.afterRegister', [$instance, $this]);

return $instance;
});
}

protected function registerSystemSettings()
{
Settings::registerCallback(function (Settings $manager) {
Settings::registerCallback(function(Settings $manager) {
$manager->registerSettingItems('core', [
'order' => [
'label' => 'lang:igniter.cart::default.text_tab_order',
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/CartMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function handle($request, \Closure $next)

public function terminate($request, $response)
{
if (config('cart.abandonedCart')) {
if (config('igniter-cart.abandonedCart')) {
$this->storeUserCart();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Models/CartSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public function getConditionsAttribute($value)
//
//

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

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

Expand Down

0 comments on commit 9578ea9

Please sign in to comment.