ldc-boutique-cnc-sable/app/Models/Range.php
Jérémy Munsch 42bdae01cc
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-10-27 09:58:34 +01:00

111 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Interfaces\ModelWithOrder;
use App\Interfaces\ModelWithStatus;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\HasOrder;
use App\Traits\Models\HasStatus;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
use Uwil\Laravel\Redirections\Interfaces\RedirectableInterface;
use Uwil\Laravel\Redirections\Traits\IsRedirectable;
/**
* @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
{
use HasOrder;
use HasStatus;
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)) {
$maxOrder = self::query()->max('order');
$model->order = is_numeric($maxOrder) ? (int) $maxOrder + 1 : 0;
}
}
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
{
return $this->hasMany(Product::class, 'range_id');
}
#[\Override]
protected function casts(): array
{
return [
'status' => 'bool',
];
}
}