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 Jun 5, 2024
1 parent c66e342 commit b919764
Show file tree
Hide file tree
Showing 24 changed files with 159 additions and 151 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,10 @@ use Igniter\Cart\Models\Order;
$record = Order::find(1);
// $record = Reservation::find(1);

$review = Review::create([
'location_id' => $record->location_id,
'customer_id' => $record->customer_id,
'sale_id' => $record->getKey(),
'sale_type' => $record->getMorphClass(),
$review = $record->leaveReview([
'quality' => 5,
'delivery' => 5,
'service' => 5,
'review_status' => true,
'author' => 'John Doe',
'review_text' => 'Great food and service!',
]);
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up()
{
Schema::table('igniter_reviews', function(Blueprint $table) {
$table->renameColumn('sale_id', 'reviewable_id');
$table->renameColumn('sale_type', 'reviewable_type');
});
}

public function down()
{
}
};
41 changes: 3 additions & 38 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -140,51 +140,16 @@ parameters:
count: 1
path: src/Classes/WorkingPeriod.php

-
message: "#^Call to method end\\(\\) on an unknown class Igniter\\\\Flame\\\\Location\\\\WorkingRange\\.$#"
count: 2
path: src/Classes/WorkingRange.php

-
message: "#^Call to method start\\(\\) on an unknown class Igniter\\\\Flame\\\\Location\\\\WorkingRange\\.$#"
count: 2
path: src/Classes/WorkingRange.php

-
message: "#^Comparison operation \"\\=\\=\" between DateInterval and 0 results in an error\\.$#"
count: 1
path: src/Classes/WorkingRange.php

-
message: "#^Comparison operation \"\\>\\=\" between DateInterval and 23 results in an error\\.$#"
count: 1
path: src/Classes/WorkingRange.php

-
message: "#^Method Igniter\\\\Local\\\\Classes\\\\WorkingRange\\:\\:fromRanges\\(\\) has invalid return type Igniter\\\\Flame\\\\Location\\\\WorkingRange\\.$#"
count: 1
path: src/Classes/WorkingRange.php

-
message: "#^PHPDoc tag @return with type Igniter\\\\Flame\\\\Location\\\\WorkingRange is not subtype of native type Igniter\\\\Local\\\\Classes\\\\WorkingRange\\.$#"
count: 1
path: src/Classes/WorkingRange.php

-
message: "#^Parameter \\$ranges of method Igniter\\\\Local\\\\Classes\\\\WorkingRange\\:\\:fromRanges\\(\\) has invalid type Igniter\\\\Flame\\\\Location\\\\WorkingRange\\.$#"
count: 1
path: src/Classes/WorkingRange.php

-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 1
path: src/Classes/WorkingRange.php

-
message: "#^Call to an undefined method DateTime\\:\\:copy\\(\\)\\.$#"
count: 1
path: src/Classes/WorkingSchedule.php

-
message: "#^Call to an undefined method DateTimeInterface\\:\\:addDay\\(\\)\\.$#"
count: 1
Expand Down Expand Up @@ -356,7 +321,7 @@ parameters:
path: src/Http/Middleware/CheckLocation.php

-
message: "#^Access to an undefined property Igniter\\\\Local\\\\Http\\\\Requests\\\\ReviewRequest\\:\\:\\$sale_type\\.$#"
message: "#^Access to an undefined property Igniter\\\\Local\\\\Http\\\\Requests\\\\ReviewRequest\\:\\:\\$reviewable_type\\.$#"
count: 2
path: src/Http/Requests/ReviewRequest.php

Expand Down Expand Up @@ -651,12 +616,12 @@ parameters:
path: src/Models/Review.php

-
message: "#^Access to an undefined property Igniter\\\\Local\\\\Models\\\\Review\\:\\:\\$sale_id\\.$#"
message: "#^Access to an undefined property Igniter\\\\Local\\\\Models\\\\Review\\:\\:\\$reviewable_id\\.$#"
count: 1
path: src/Models/Review.php

-
message: "#^Access to an undefined property Igniter\\\\Local\\\\Models\\\\Review\\:\\:\\$sale_type\\.$#"
message: "#^Access to an undefined property Igniter\\\\Local\\\\Models\\\\Review\\:\\:\\$reviewable_type\\.$#"
count: 1
path: src/Models/Review.php

Expand Down
9 changes: 5 additions & 4 deletions resources/lang/en/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
'alert_invalid_area' => 'Invalid area selected.',
'alert_settings_not_loaded' => 'Location settings not loaded.',
'alert_schedule_not_loaded' => 'Schedule not loaded.',
'alert_schedule_not_found' => 'Schedule not found.',
'alert_invalid_schedule_type' => "Defined parameter '%s' is not a valid working type.",

'help_permalink_disabled' => 'Permalink is disabled when single location mode is activated.',
Expand Down Expand Up @@ -335,12 +336,12 @@

'column_location' => 'Location',
'column_author' => 'Author',
'column_sale_id' => 'Sale ID',
'column_sale_type' => 'Sale Type',
'column_reviewable_id' => 'Reviewable ID',
'column_reviewable_type' => 'Reviewable Type',
'column_text' => 'Review Text',

'label_sale_type' => 'Sale Type',
'label_sale_id' => 'Sale ID',
'label_reviewable_type' => 'Reviewable Type',
'label_reviewable_id' => 'Reviewable ID',
'label_location' => 'Location',
'label_customer' => 'Customer',
'label_author' => 'Author',
Expand Down
16 changes: 8 additions & 8 deletions resources/models/review.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@
'select' => "concat(first_name, ' ', last_name)",
'searchable' => true,
],
'sale_id' => [
'label' => 'lang:igniter.local::default.reviews.column_sale_id',
'reviewable_id' => [
'label' => 'lang:igniter.local::default.reviews.column_reviewable_id',
'type' => 'number',
'searchable' => true,
],
'sale_type' => [
'label' => 'lang:igniter.local::default.reviews.column_sale_type',
'reviewable_type' => [
'label' => 'lang:igniter.local::default.reviews.column_reviewable_type',
'type' => 'select',
'searchable' => true,
'formatter' => function($record, $column, $value) {
Expand Down Expand Up @@ -152,14 +152,14 @@
'span' => 'right',
'placeholder' => 'lang:admin::lang.text_please_select',
],
'sale_type' => [
'label' => 'lang:igniter.local::default.reviews.label_sale_type',
'reviewable_type' => [
'label' => 'lang:igniter.local::default.reviews.label_reviewable_type',
'type' => 'radiotoggle',
'span' => 'left',
'default' => 'orders',
],
'sale_id' => [
'label' => 'lang:igniter.local::default.reviews.label_sale_id',
'reviewable_id' => [
'label' => 'lang:igniter.local::default.reviews.label_reviewable_id',
'type' => 'number',
'span' => 'right',
],
Expand Down
19 changes: 17 additions & 2 deletions src/AutomationRules/Conditions/ReviewCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Igniter\Local\AutomationRules\Conditions;

use Igniter\Automation\AutomationException;
use Igniter\Automation\Classes\BaseModelAttributesCondition;
use Igniter\Cart\Models\Order;
use Igniter\Local\Models\Review;
Expand Down Expand Up @@ -37,8 +38,22 @@ public function getReviewCountAttribute($value, $object)
}

return Review::query()->where([
'sale_id' => $object->getKey(),
'sale_type' => $object->getMorphClass(),
'reviewable_id' => $object->getKey(),
'reviewable_type' => $object->getMorphClass(),
])->count();
}

/**
* Checks whether the condition is TRUE for specified parameters
* @param array $params Specifies a list of parameters as an associative array.
* @return bool
*/
public function isTrue(&$params)
{
if (!$orderOrReservation = array_get($params, 'order', array_get($params, 'reservation'))) {
throw new AutomationException('Error evaluating the review count condition: the order/reservation object is not found in the condition parameters.');
}

return $this->evalIsTrue($orderOrReservation);
}
}
4 changes: 2 additions & 2 deletions src/Classes/CoveredArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ protected function getConditionValue($type, $cartTotal)
return $type == 'total' ? $condition->total : -1;
}

// At this stage, minimum total is 0 when the matched condition is a below
// At this stage, return the minimum total when the matched condition is a below
if ($type == 'total' && $condition->type == 'below') {
return 0;
return $condition->total;
}

return $condition->{$type};
Expand Down
8 changes: 4 additions & 4 deletions src/Classes/ScheduleItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public function getHours()
'day' => $day,
'open' => '00:00',
'close' => '23:59',
'status' => 1,
'status' => true,
]],
'daily' => [[
'day' => $day,
'open' => $this->open,
'close' => $this->close,
'status' => (int)in_array($day, $this->days),
'status' => in_array($day, $this->days),
]],
'timesheet' => $this->createHours($day, $this->timesheet[$day]),
'flexible' => $this->createHours($day, $this->flexible[$day]),
Expand Down Expand Up @@ -126,7 +126,7 @@ protected function flexible(array $data)
$result[$key] = [
'day' => $hour['day'] ?? $key,
'hours' => $hour['hours'] ?? '00:00-23:59',
'status' => $hour['status'] ?? 1,
'status' => (bool)($hour['status'] ?? 1),
];
}

Expand All @@ -147,7 +147,7 @@ protected function createHours(int $day, $data): array
'day' => $day,
'open' => array_get($hour, 0, array_get($hour, 'open')),
'close' => array_get($hour, 1, array_get($hour, 'close')),
'status' => $data['status'],
'status' => (bool)($data['status'] ?? 1),
];
}

Expand Down
7 changes: 1 addition & 6 deletions src/Classes/WorkingRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public static function create(array $times): self
);
}

/**
* @param \Igniter\Flame\Location\WorkingRange[] $ranges
* @return \Igniter\Flame\Location\WorkingRange
* @throws \Igniter\Local\Exceptions\WorkingHourException
*/
public static function fromRanges(array $ranges): self
{
if (count($ranges) === 0) {
Expand Down Expand Up @@ -82,7 +77,7 @@ public function opensAllDay(): bool
{
$diffInHours = $this->start()->diff($this->end());

return $diffInHours >= 23 || $diffInHours == 0;
return ($diffInHours->h == 23 && $diffInHours->i == 59) || $diffInHours == 0;
}

public function containsTime(WorkingTime $time): bool
Expand Down
15 changes: 10 additions & 5 deletions src/Classes/WorkingSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function isOpen()

public function isOpening()
{
return $this->nextOpenAt(new DateTime()) ? true : false;
return (bool)$this->nextOpenAt(new DateTime());
}

public function isClosed()
Expand Down Expand Up @@ -209,14 +209,21 @@ public function nextOpenAt(DateTimeInterface $dateTime)
return null;
}

$days = 0;
while ($nextOpenAt === false) {
if ($days >= $this->maxDays) {
return null;
}

$dateTime = $dateTime->modify('+1 day')->setTime(0, 0);
$workingTime = WorkingTime::fromDateTime($dateTime);

$forDate = $this->forDate($dateTime);
$nextOpenAt = !$forDate->isEmpty()
? $forDate->nextOpenAt($workingTime)
: false;

$days++;
}

return $dateTime->setTime(
Expand Down Expand Up @@ -254,12 +261,10 @@ public function nextCloseAt(DateTimeInterface $dateTime)
: false;
}

$dateTime = $dateTime->setTime(
return $dateTime->setTime(
$nextCloseAt->toDateTime()->format('G'),
$nextCloseAt->toDateTime()->format('i')
);

return $dateTime;
}

/**
Expand Down Expand Up @@ -358,7 +363,7 @@ public function generateTimeslot(DateTime $date, DateInterval $interval, ?DateIn
return $this->forDate($date)
->timeslot($date, $interval, $leadTime)
->filter(function($timeslot) use ($date, $leadTime) {
$dateTime = $date->copy()->setTimeFromTimeString($timeslot->format('H:i'));
$dateTime = make_carbon($date)->setTimeFromTimeString($timeslot->format('H:i'));

return $this->isTimeslotValid($timeslot, $dateTime, $leadTime->i);
})
Expand Down
2 changes: 1 addition & 1 deletion src/Classes/WorkingTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class WorkingTime
/** @var int */
protected $minutes;

protected function __construct(int $hours, int $minutes)
public function __construct(int $hours, int $minutes)
{
$this->hours = $hours;
$this->minutes = $minutes;
Expand Down
3 changes: 2 additions & 1 deletion src/Database/Factories/LocationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public function definition(): array
'location_email' => $this->faker->email,
'location_address_1' => $this->faker->streetAddress,
'location_country_id' => $this->faker->numberBetween(1, 200),
'is_auto_lat_lng' => false,
'location_lat' => $this->faker->latitude,
'location_lng' => $this->faker->longitude,
'is_auto_lat_lng' => false,
'location_status' => true,
];
}
}
4 changes: 2 additions & 2 deletions src/Database/Factories/ReviewFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function definition(): array
return [
'location_id' => $this->faker->numberBetween(1, 200),
'customer_id' => $this->faker->numberBetween(1, 200),
'sale_id' => $this->faker->numberBetween(1, 200),
'sale_type' => 'orders',
'reviewable_id' => $this->faker->numberBetween(1, 200),
'reviewable_type' => 'orders',
'author' => $this->faker->name,
'quality' => $this->faker->numberBetween(0, 6),
'delivery' => $this->faker->numberBetween(0, 6),
Expand Down
5 changes: 5 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use Igniter\Admin\DashboardWidgets\Charts;
use Igniter\Admin\Facades\AdminMenu;
use Igniter\Cart\Classes\OrderTypes;
use Igniter\Cart\Models\Order;
use Igniter\Flame\Geolite\Facades\Geocoder;
use Igniter\Flame\Igniter;
use Igniter\Local\Classes\Location;
use Igniter\Local\Facades\Location as LocationFacade;
use Igniter\Local\Http\Requests\LocationRequest;
use Igniter\Local\Listeners\MaxOrderPerTimeslotReached;
use Igniter\Local\MainMenuWidgets\LocationPicker;
use Igniter\Local\Models\Actions\ReviewAction;
use Igniter\Local\Models\Location as LocationModel;
use Igniter\Local\Models\LocationArea;
use Igniter\Local\Models\Review;
Expand Down Expand Up @@ -87,6 +89,9 @@ public function boot()
});
});

Order::implement(ReviewAction::class);
Reservation::implement(ReviewAction::class);

if (Igniter::runningInAdmin()) {
$this->registerLocationsMainMenuItems();
}
Expand Down
Loading

0 comments on commit b919764

Please sign in to comment.