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 be4a863 commit 068546e
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 155 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ The settings definition array should contain the following keys:
- `form`: An array of form fields or path to the form definition file to be used for the settings section.
- `request`: The form request class to be used for the settings section.

#### The `Locationable` model trait

The `Locationable` model trait allows you to associate a model with a location.

### Delivery areas

#### Adding delivery areas
Expand Down
23 changes: 4 additions & 19 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -286,27 +286,12 @@ parameters:
path: src/Http/Actions/LocationAwareController.php

-
message: "#^Call to an undefined method Igniter\\\\Local\\\\Http\\\\Controllers\\\\LocationSettings\\:\\:getFormModel\\(\\)\\.$#"
count: 1
path: src/Http/Controllers/LocationSettings.php

-
message: "#^Call to an undefined static method Igniter\\\\Admin\\\\Facades\\\\AdminMenu\\:\\:setContext\\(\\)\\.$#"
count: 1
path: src/Http/Controllers/LocationSettings.php

-
message: "#^Call to an undefined static method Igniter\\\\Local\\\\Facades\\\\Location\\:\\:check\\(\\)\\.$#"
message: "#^Call to an undefined method Igniter\\\\Local\\\\Http\\\\Controllers\\\\Locations\\:\\:getFormModel\\(\\)\\.$#"
count: 1
path: src/Http/Controllers/LocationSettings.php

-
message: "#^Call to an undefined static method Igniter\\\\Local\\\\Facades\\\\Location\\:\\:getId\\(\\)\\.$#"
count: 2
path: src/Http/Controllers/LocationSettings.php
path: src/Http/Controllers/Locations.php

-
message: "#^Call to an undefined method Igniter\\\\Local\\\\Http\\\\Controllers\\\\Locations\\:\\:getFormModel\\(\\)\\.$#"
message: "#^Call to an undefined method Igniter\\\\Local\\\\Http\\\\Controllers\\\\Locations\\:\\:refreshList\\(\\)\\.$#"
count: 1
path: src/Http/Controllers/Locations.php

Expand Down Expand Up @@ -702,7 +687,7 @@ parameters:

-
message: "#^Call to an undefined static method Igniter\\\\Local\\\\Models\\\\ReviewSettings\\:\\:get\\(\\)\\.$#"
count: 3
count: 4
path: src/Models/ReviewSettings.php

-
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"
}
}
}
5 changes: 3 additions & 2 deletions resources/lang/en/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
'text_empty' => 'There are no locations available.',
'text_24_7' => '24/7',
'text_daily' => 'Daily',
'settings_title' => 'Settings',
'text_flexible' => 'Flexible',
'text_timesheet' => 'Timesheet',
'text_schedule' => 'Schedule',
Expand All @@ -219,8 +220,6 @@
'text_below_order_total' => 'below',
'text_asap_only' => 'ASAP only',
'text_later_only' => 'Later only',
'text_side_menu_location' => 'Locations',
'text_tab_desc_location' => 'Manage your restaurant locations.',

'column_city' => 'City',
'column_state' => 'State',
Expand Down Expand Up @@ -349,6 +348,7 @@
'label_delivery' => 'Delivery Rating',
'label_service' => 'Service Rating',
'label_text' => 'Review Text',
'label_score_types' => 'Score Types',
'label_hints' => 'Rating Hints',

'label_allow_reviews' => 'Allow Reviews',
Expand All @@ -361,6 +361,7 @@
'help_allow_reviews' => 'Enable or disable new customer review entry and display of reviews on storefront',
'help_approve_reviews' => 'Approve new review entry automatically or manually',
'help_hints' => 'Used when adding reviews.',
'help_score_types' => 'Rename score types for rating',
'help_chase_reviews' => 'Send a follow up email when a review has not been submitted',

'error_not_found_in_order' => 'The sale ID entered can not be found in orders',
Expand Down
10 changes: 9 additions & 1 deletion resources/models/location.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@
'href' => 'locations/edit/{location_id}',
],
],
'settings' => [
'type' => 'button',
'iconCssClass' => 'fa fa-cog',
'attributes' => [
'class' => 'btn btn-edit',
'href' => 'locations/settings/{location_id}',
],
],
'default' => [
'type' => 'button',
'iconCssClass' => 'fa fa-star-o',
'attributes' => [
'class' => 'btn btn-outline-warning bg-transparent',
'class' => 'btn btn-light text-warning',
'data-request' => 'onSetDefault',
'data-request-data' => 'default:{location_id}',
],
Expand Down
26 changes: 26 additions & 0 deletions resources/models/reviewsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,36 @@
'condition' => 'checked',
],
],
'score_types' => [
'label' => 'lang:igniter.local::default.reviews.label_score_types',
'tab' => 'lang:igniter.local::default.reviews.text_tab_title_reviews',
'type' => 'repeater',
'sortable' => true,
'showAddButton' => false,
'showRemoveButton' => false,
'default' => ReviewSettings::$defaultScoreTypes,
'form' => [
'fields' => [
'value' => [
'type' => 'text',
],
'code' => [
'type' => 'text',
'attributes' => [
'readonly' => true,
],
],
],
],
'commentAbove' => 'lang:igniter.local::default.reviews.help_score_types',
],
'hints' => [
'label' => 'lang:igniter.local::default.reviews.label_hints',
'tab' => 'lang:igniter.local::default.reviews.text_tab_title_reviews',
'type' => 'repeater',
'sortable' => true,
'showAddButton' => false,
'showRemoveButton' => false,
'default' => ReviewSettings::$defaultHints,
'form' => [
'fields' => [
Expand Down
18 changes: 9 additions & 9 deletions src/Classes/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public function updateOrderType($code = null)
}

if (strlen($code)) {
$this->forgetSession('order-timeslot');

$this->putSession('orderType', $code);
$this->fireSystemEvent('location.orderType.updated', [$code, $oldOrderType]);
}
Expand All @@ -68,15 +66,16 @@ public function updateUserPosition(UserLocation $position)

public function updateScheduleTimeSlot($dateTime, $isAsap = true)
{
$oldSlot = $this->getSession('order-timeslot');
$orderType = $this->orderType();
$oldSlot = $this->getSession($orderType.'-timeslot');

$slot['dateTime'] = (!$isAsap && !is_null($dateTime)) ? make_carbon($dateTime) : null;
$slot['isAsap'] = $isAsap;

if (!$slot) {
$this->forgetSession('order-timeslot');
$this->forgetSession($orderType.'-timeslot');
} else {
$this->putSession('order-timeslot', $slot);
$this->putSession($orderType.'-timeslot', $slot);
}

$this->fireSystemEvent('location.timeslot.updated', [$slot, $oldSlot]);
Expand Down Expand Up @@ -238,8 +237,9 @@ public function orderTimeIsAsap()
return false;
}

$dateTime = $this->getSession('order-timeslot.dateTime');
$orderTimeIsAsap = (bool)$this->getSession('order-timeslot.isAsap', true);
$orderType = $this->orderType();
$dateTime = $this->getSession($orderType.'-timeslot.dateTime');
$orderTimeIsAsap = (bool)$this->getSession($orderType.'-timeslot.isAsap', true);

if (!$this->isOpened()) {
return false;
Expand All @@ -253,7 +253,7 @@ public function orderTimeIsAsap()
*/
public function orderDateTime()
{
$dateTime = $this->getSession('order-timeslot.dateTime');
$dateTime = $this->getSession($this->orderType().'-timeslot.dateTime');
if ($this->orderTimeIsAsap()) {
$dateTime = $this->asapScheduleTimeslot();
}
Expand Down Expand Up @@ -332,7 +332,7 @@ public function checkOrderTime($timestamp = null, $orderTypeCode = null)

public function checkNoOrderTypeAvailable()
{
return $this->getOrderTypes()->filter(function ($orderType) {
return $this->getOrderTypes()->filter(function($orderType) {
return !$orderType->isDisabled();
})->isEmpty();
}
Expand Down
63 changes: 22 additions & 41 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Igniter\Local\Models\Scopes\LocationScope;
use Igniter\Local\Requests\LocationRequest;
use Igniter\Reservation\Models\Reservation;
use Igniter\System\Models\Settings;
use Igniter\User\Facades\Auth;
use Igniter\User\Models\User;
use Illuminate\Database\Eloquent\Relations\Relation;
Expand Down Expand Up @@ -53,15 +52,13 @@ public function register()
{
parent::register();

$this->callAfterResolving('location', function (Location $location) {
$this->callAfterResolving('location', function(Location $location) {
$location->setSessionKey(Igniter::runningInAdmin() ? 'admin_location' : 'location');
});

Route::pushMiddlewareToGroup('igniter', \Igniter\Local\Http\Middleware\CheckLocation::class);

AliasLoader::getInstance()->alias('Location', LocationFacade::class);

$this->registerSystemSettings();
}

public function boot()
Expand All @@ -72,16 +69,16 @@ public function boot()
$this->addAssetsToReviewsSettingsPage();
$this->extendDashboardChartsDatasets();

User::extend(function ($model) {
$model->addDynamicMethod('getAvailableLocations', function () use ($model) {
User::extend(function($model) {
$model->addDynamicMethod('getAvailableLocations', function() use ($model) {
if ($model->isSuperUser()) {
return $model->locations()->getModel()->query()->get();
}

return $model->locations;
});

$model->addDynamicMethod('isAssignedLocation', function ($location) use ($model) {
$model->addDynamicMethod('isAssignedLocation', function($location) use ($model) {
if ($model->isSuperUser()) {
return true;
}
Expand Down Expand Up @@ -155,17 +152,6 @@ public function registerMailTemplates(): array
public function registerNavigation(): array
{
return [
'restaurant' => [
'child' => [
'locationsettings' => [
'priority' => 10,
'class' => 'locationsettings',
'href' => admin_url('locationsettings'),
'title' => lang('igniter.local::default.text_settings'),
'permission' => 'Admin.Locations',
],
],
],
'marketing' => [
'child' => [
'reviews' => [
Expand All @@ -177,6 +163,17 @@ public function registerNavigation(): array
],
],
],
'system' => [
'child' => [
'locations' => [
'priority' => 0,
'class' => 'locations',
'href' => admin_url('locations'),
'title' => lang('igniter.local::default.text_title'),
'permission' => 'Admin.Locations',
],
],
],
];
}

Expand Down Expand Up @@ -249,7 +246,7 @@ public function registerOnboardingSteps()

protected function extendDashboardChartsDatasets()
{
Charts::registerDatasets(function () {
Charts::registerDatasets(function() {
if (!ReviewSettings::allowReviews()) {
return [];
}
Expand All @@ -267,7 +264,7 @@ protected function extendDashboardChartsDatasets()

protected function addAssetsToReviewsSettingsPage()
{
Event::listen('admin.form.extendFieldsBefore', function ($form) {
Event::listen('admin.form.extendFieldsBefore', function($form) {
if (!$form->model instanceof ReviewSettings) {
return;
}
Expand All @@ -284,14 +281,14 @@ protected function addReviewsRelationship(): void
'reviews' => \Igniter\Local\Models\Review::class,
]);

Reservation::extend(function ($model) {
Reservation::extend(function($model) {
$model->relation['morphMany']['review'] = [\Igniter\Local\Models\Review::class];
});
}

protected function bindRememberLocationAreaEvents(): void
{
Event::listen('location.position.updated', function ($location, $position, $oldPosition) {
Event::listen('location.position.updated', function($location, $position, $oldPosition) {
if ($position->format() === $oldPosition?->format()) {
return;
}
Expand All @@ -301,13 +298,13 @@ protected function bindRememberLocationAreaEvents(): void
]);
});

Event::listen('location.area.updated', function ($location, $coveredArea) {
Event::listen('location.area.updated', function($location, $coveredArea) {
$this->updateCustomerLastArea([
'areaId' => $coveredArea->getKey(),
]);
});

Event::listen(['igniter.user.login', 'igniter.socialite.login'], function () {
Event::listen(['igniter.user.login', 'igniter.socialite.login'], function() {
try {
if (!strlen($lastArea = Auth::customer()->last_location_area)) {
return;
Expand Down Expand Up @@ -345,7 +342,7 @@ protected function updateCustomerLastArea($value)

protected function registerLocationsMainMenuItems()
{
AdminMenu::registerCallback(function (Navigation $manager) {
AdminMenu::registerCallback(function(Navigation $manager) {
$manager->registerMainItems([
MainMenuItem::widget('locations', LocationPicker::class)
->priority(0)
Expand All @@ -357,20 +354,4 @@ protected function registerLocationsMainMenuItems()
]);
});
}

protected function registerSystemSettings()
{
Settings::registerCallback(function (Settings $manager) {
$manager->registerSettingItems('core', [
'locations' => [
'label' => 'lang:igniter.local::default.text_side_menu_location',
'description' => 'lang:igniter.local::default.text_tab_desc_location',
'icon' => 'fa fa-map-pin',
'priority' => 45,
'permission' => ['Admin.Locations'],
'url' => admin_url('locations'),
],
]);
});
}
}
Loading

0 comments on commit 068546e

Please sign in to comment.