Skip to content

Commit

Permalink
feat: enhance review timestamps and refactor migration and model code
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Mar 2, 2025
1 parent 3b00cde commit a9a3f27
Show file tree
Hide file tree
Showing 45 changed files with 181 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php

declare(strict_types=1);

use Igniter\Local\Models\ReviewSettings;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
public function up(): void
{
if (Schema::hasTable('reviews')) {
Schema::rename('reviews', 'igniter_reviews');
}

if (!Schema::hasTable('igniter_reviews')) {
Schema::create('igniter_reviews', function(Blueprint $table) {
Schema::create('igniter_reviews', function(Blueprint $table): void {
$table->engine = 'InnoDB';
$table->integer('review_id', true);
$table->integer('customer_id');
Expand All @@ -41,7 +43,7 @@ public function up()
]);
}

public function down()
public function down(): void
{
Schema::dropIfExists('igniter_reviews');
}
Expand Down
21 changes: 13 additions & 8 deletions database/migrations/2020_12_10_000300_update_reviews_table.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?php

declare(strict_types=1);

use Igniter\Cart\Models\Order;
use Igniter\Reservation\Models\Reservation;

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

return new class extends Migration
{
public function up()
public function up(): void
{
Schema::table('igniter_reviews', function(Blueprint $table) {
Schema::table('igniter_reviews', function(Blueprint $table): void {
$table->integer('customer_id')->nullable()->change();
$table->string('author')->nullable()->change();
$table->text('review_text')->nullable()->change();
Expand All @@ -18,21 +23,21 @@ public function up()
$this->updateMorphsOnReviews();
}

public function down() {}
public function down(): void {}

protected function updateMorphsOnReviews()
protected function updateMorphsOnReviews(): void
{
if (DB::table('igniter_reviews')
->where('sale_type', \Igniter\Cart\Models\Order::class)
->orWhere('sale_type', \Igniter\Reservation\Models\Reservation::class)
->where('sale_type', Order::class)
->orWhere('sale_type', Reservation::class)
->count()
) {
return;
}

$morphs = [
'order' => \Igniter\Cart\Models\Order::class,
'reservation' => \Igniter\Reservation\Models\Reservation::class,
'order' => Order::class,
'reservation' => Reservation::class,
];

DB::table('igniter_reviews')->get()->each(function($model) use ($morphs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<?php

declare(strict_types=1);

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

return new class extends Migration
{
public function up()
public function up(): void
{
if (Schema::hasColumn('customers', 'last_location_area')) {
return;
}

Schema::table('customers', function(Blueprint $table) {
Schema::table('customers', function(Blueprint $table): void {
$table->text('last_location_area');
});
}

public function down() {}
public function down(): void {}
};
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<?php

declare(strict_types=1);

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

return new class extends Migration
{
public function up()
public function up(): void
{
if (!Schema::hasColumn('igniter_reviews', 'date_added')) {
return;
}

Schema::table('igniter_reviews', function(Blueprint $table) {
Schema::table('igniter_reviews', function(Blueprint $table): void {
$table->renameColumn('date_added', 'created_at');
});

Schema::table('igniter_reviews', function(Blueprint $table) {
Schema::table('igniter_reviews', function(Blueprint $table): void {
$table->timestamp('created_at')->change();
$table->timestamp('updated_at');
});
}

public function down() {}
public function down(): void {}
};
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php

declare(strict_types=1);

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

return new class extends Migration
{
public function up()
public function up(): void
{
Schema::table('igniter_reviews', function(Blueprint $table) {
Schema::table('igniter_reviews', function(Blueprint $table): void {
$table->unsignedBigInteger('review_id', true)->change();
$table->unsignedBigInteger('customer_id')->nullable()->change();
$table->unsignedBigInteger('sale_id')->nullable()->change();
$table->unsignedBigInteger('location_id')->nullable()->change();
});
}

public function down() {}
public function down(): void {}
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?php

declare(strict_types=1);

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

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

public function down() {}
public function down(): void {}
};
7 changes: 6 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@

return RectorConfig::configure()
->withImportNames(removeUnusedImports: true)
->withPaths([__DIR__.'/src', __DIR__.'/tests'])
->withPaths([
__DIR__.'/database',
__DIR__.'/src',
__DIR__.'/tests',
])
->withRules([
DeclareStrictTypesRector::class,
])
->withSkip([
ReturnTypeFromStrictNewArrayRector::class,
])
->withPhpSets(php83: true)
->withPreparedSets(
deadCode: true,
codeQuality: true,
Expand Down
4 changes: 4 additions & 0 deletions src/AutomationRules/Conditions/ReviewCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Igniter\Local\AutomationRules\Conditions;

use Override;
use Igniter\Automation\AutomationException;
use Igniter\Automation\Classes\BaseModelAttributesCondition;
use Igniter\Cart\Models\Order;
Expand All @@ -16,6 +17,7 @@ class ReviewCount extends BaseModelAttributesCondition

protected $modelAttributes;

#[Override]
public function conditionDetails(): array
{
return [
Expand All @@ -24,6 +26,7 @@ public function conditionDetails(): array
];
}

#[Override]
public function defineModelAttributes(): array
{
return [
Expand All @@ -50,6 +53,7 @@ public function getReviewCountAttribute($value, $object)
* @param array $params Specifies a list of parameters as an associative array.
* @return bool
*/
#[Override]
public function isTrue(&$params)
{
if (!$orderOrReservation = array_get($params, 'order', array_get($params, 'reservation'))) {
Expand Down
5 changes: 5 additions & 0 deletions src/CartConditions/Delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Igniter\Local\CartConditions;

use Override;
use Igniter\Cart\CartCondition;
use Igniter\Cart\Facades\Cart;
use Igniter\Local\Facades\Location;
Expand All @@ -15,6 +16,7 @@ class Delivery extends CartCondition

protected $deliveryCharge = 0;

#[Override]
public function beforeApply(): ?bool
{
// Do not apply condition when orderType is not delivery
Expand All @@ -28,20 +30,23 @@ public function beforeApply(): ?bool
return null;
}

#[Override]
public function getRules(): array
{
return [
$this->deliveryCharge.' >= 0',
];
}

#[Override]
public function getActions(): array
{
return [
['value' => '+'.$this->deliveryCharge],
];
}

#[Override]
public function getValue()
{
return $this->calculatedValue > 0 ? $this->calculatedValue : lang('igniter::main.text_free');
Expand Down
29 changes: 9 additions & 20 deletions src/Classes/CoveredArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
*/
class CoveredArea
{
protected LocationArea $model;

public function __construct(LocationArea $model)
public function __construct(protected LocationArea $model)
{
$this->model = $model;
}

public function deliveryAmount($cartTotal): float|int
Expand All @@ -42,9 +39,7 @@ public function listConditions(): Collection

public function getConditionLabels(): array
{
return $this->listConditions()->map(function(CoveredAreaCondition $condition): string {
return ucfirst(strtolower($condition->getLabel()));
})->all();
return $this->listConditions()->map(fn(CoveredAreaCondition $condition): string => ucfirst(strtolower($condition->getLabel())))->all();
}

protected function getConditionValue($type, $cartTotal): float|int
Expand All @@ -68,9 +63,7 @@ protected function getConditionValue($type, $cartTotal): float|int

protected function checkConditions($cartTotal, $value = 'total'): ?CoveredAreaCondition
{
return $this->listConditions()->first(function(CoveredAreaCondition $condition) use ($cartTotal): bool {
return $condition->isValid($cartTotal);
});
return $this->listConditions()->first(fn(CoveredAreaCondition $condition): bool => $condition->isValid($cartTotal));
}

protected function calculateDistanceCharges(): float|int
Expand All @@ -85,16 +78,12 @@ protected function calculateDistanceCharges(): float|int

$condition = $distanceCharges
->sortBy('priority')
->map(function(array $condition): CoveredAreaCondition {
return new CoveredAreaCondition([
'type' => $condition['type'],
'amount' => $condition['charge'],
'total' => $condition['distance'],
]);
})
->first(function(CoveredAreaCondition $condition) use ($distanceFromLocation): bool {
return $condition->isValid($distanceFromLocation);
});
->map(fn(array $condition): CoveredAreaCondition => new CoveredAreaCondition([
'type' => $condition['type'],
'amount' => $condition['charge'],
'total' => $condition['distance'],
]))
->first(fn(CoveredAreaCondition $condition): bool => $condition->isValid($distanceFromLocation));

return optional($condition)->amount ?? 0;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Classes/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,7 @@ public function orderTimeInterval()

public function checkNoOrderTypeAvailable()
{
return $this->getOrderTypes()->filter(function($orderType): bool {
return !$orderType->isDisabled();
})->isEmpty();
return $this->getOrderTypes()->filter(fn($orderType): bool => !$orderType->isDisabled())->isEmpty();
}

public function hasLaterSchedule(): bool
Expand Down
5 changes: 1 addition & 4 deletions src/Classes/ScheduleItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

class ScheduleItem
{
public string $name;

public string $type;

public array $days;
Expand All @@ -27,9 +25,8 @@ class ScheduleItem
*/
protected $data;

public function __construct(string $name)
public function __construct(public string $name)
{
$this->name = $name;
}

public static function create(string $name, array $data = []): self
Expand Down
2 changes: 1 addition & 1 deletion src/Classes/WorkingDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function toISO(string $day): int

public static function normalizeName($day): string
{
$day = strtolower($day);
$day = strtolower((string) $day);

if (!static::isValid($day)) {
throw new WorkingHourException(sprintf("Day `%s` isn't a valid day name. Valid day names are lowercase english words, e.g. `monday`, `thursday`.", $day));
Expand Down
Loading

0 comments on commit a9a3f27

Please sign in to comment.