ldc-boutique-cnc-sable/app/Models/Range.php

112 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2020-11-05 17:08:32 +01:00
<?php
2026-06-14 21:32:07 +02:00
declare(strict_types=1);
2020-11-05 17:08:32 +01:00
namespace App\Models;
use App\Interfaces\ModelWithOrder;
use App\Interfaces\ModelWithStatus;
2026-06-14 21:32:07 +02:00
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\HasOrder;
use App\Traits\Models\HasStatus;
2026-06-14 21:32:07 +02:00
use Illuminate\Database\Eloquent\Attributes\Fillable;
2020-11-05 17:08:32 +01:00
use Illuminate\Database\Eloquent\Model;
2026-06-14 21:32:07 +02:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
use Uwil\Laravel\Redirections\Interfaces\RedirectableInterface;
use Uwil\Laravel\Redirections\Traits\IsRedirectable;
2020-11-05 17:08:32 +01:00
2026-06-14 21:32:07 +02:00
/**
* @property int $id
* @property string $name
* @property string $slug
* @property string|null $description
* @property string|null $picture
* @property string $picture_alt
* @property string $picture_title
* @property bool $status
* @property int $order
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Product> $products
*
* @mixin \Eloquent
*/
#[Fillable([
'name',
'description',
'picture',
'picture_alt',
'picture_title',
'status',
])]
class Range extends Model implements ModelWithOrder, ModelWithStatus, RedirectableInterface
2020-11-05 17:08:32 +01:00
{
use HasOrder;
use HasStatus;
2026-06-14 21:32:07 +02:00
use IsRedirectable;
#[\Override]
protected static function booted()
{
static::creating(function (self $model): void {
self::manageFields($model);
});
static::updating(function (self $model): void {
self::manageFields($model);
});
static::deleting(function (self $model): void {
FileStorageHelper::removeFile($model, 'picture');
});
}
#[\Override]
public function getRouteKeyName(): string
{
return 'slug';
}
/** @return string[]|string */
#[\Override]
public function getFrontUrl(string $modelKey): array | string
{
return route('fo.products.index') . '#' . http_build_query([
'ranges' => [$modelKey],
]);
}
private static function manageFields(self $model): void
{
self::assertNameIsUnique($model->name, $model->id);
$model->slug = Str::slug($model->name);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
if (is_null($model->order)) {
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
$maxOrder = self::query()->max('order');
$model->order = is_numeric($maxOrder) ? (int) $maxOrder + 1 : 0;
2026-06-14 21:32:07 +02:00
}
}
private static function assertNameIsUnique(?string $name, ?int $id = null): void
{
$table = new self()->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id);
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name ?? ''), $id);
}
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\Product,$this> */
public function products(): HasMany
2022-09-08 11:29:39 +02:00
{
2026-06-14 21:32:07 +02:00
return $this->hasMany(Product::class, 'range_id');
2020-11-05 17:08:32 +01:00
}
2026-06-14 21:32:07 +02:00
#[\Override]
protected function casts(): array
2022-09-08 11:29:39 +02:00
{
2026-06-14 21:32:07 +02:00
return [
'status' => 'bool',
];
2020-11-05 17:08:32 +01:00
}
}