2026-07-08 16:32:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
|
|
|
|
|
use App\Contracts\Directories\ProductDirectory;
|
2026-07-08 18:16:46 +02:00
|
|
|
use App\Contracts\Directories\RecipeDirectory;
|
|
|
|
|
use App\Models\Product;
|
|
|
|
|
use App\Models\Recipe;
|
|
|
|
|
use Domain\Models\Entities\Product as DomainProduct;
|
|
|
|
|
use Domain\Models\Entities\Recipe as DomainRecipe;
|
2026-07-08 16:32:12 +02:00
|
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
2026-07-08 18:16:46 +02:00
|
|
|
use Domain\Models\ValueObjects\Entities\RecipeId;
|
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2026-07-08 16:32:12 +02:00
|
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
|
use Shared\Helpers\UuidHelper;
|
|
|
|
|
|
|
|
|
|
class RouteServiceProvider extends ServiceProvider
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Define your route model bindings, pattern filters, and other route configuration.
|
|
|
|
|
*/
|
|
|
|
|
public function boot(): void
|
|
|
|
|
{
|
|
|
|
|
Route::bind('product', function (string $value) {
|
2026-07-08 18:16:46 +02:00
|
|
|
if (request()->is('back/*') || request()->is('back') || request()->routeIs('back.*')) {
|
|
|
|
|
return Product::withTrashed()
|
|
|
|
|
->where('id', $value)
|
|
|
|
|
->orWhere('slug', $value)
|
|
|
|
|
->firstOrFail();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-08 16:32:12 +02:00
|
|
|
return $this->resolveProduct($value);
|
|
|
|
|
});
|
2026-07-08 18:16:46 +02:00
|
|
|
|
|
|
|
|
Route::bind('recipe', function (string $value) {
|
|
|
|
|
if (request()->is('back/*') || request()->is('back') || request()->routeIs('back.*')) {
|
|
|
|
|
return Recipe::withTrashed()
|
|
|
|
|
->where('id', $value)
|
|
|
|
|
->orWhere('slug', $value)
|
|
|
|
|
->firstOrFail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->resolveRecipe($value);
|
|
|
|
|
});
|
2026-07-08 16:32:12 +02:00
|
|
|
}
|
|
|
|
|
|
2026-07-08 18:16:46 +02:00
|
|
|
private function resolveProduct(string $value): DomainProduct
|
2026-07-08 16:32:12 +02:00
|
|
|
{
|
|
|
|
|
$productDirectory = app(ProductDirectory::class);
|
|
|
|
|
|
|
|
|
|
if (Str::isUuid($value) && UuidHelper::isV7(Uuid::fromString($value))) {
|
|
|
|
|
$product = $productDirectory->find(new ProductId(Uuid::fromString($value)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! isset($product)) {
|
|
|
|
|
$product = $productDirectory->findBySlug($value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-08 18:16:46 +02:00
|
|
|
if (! isset($product)) {
|
|
|
|
|
throw new ModelNotFoundException('Product not found');
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-08 16:32:12 +02:00
|
|
|
return $product;
|
|
|
|
|
}
|
2026-07-08 18:16:46 +02:00
|
|
|
|
|
|
|
|
private function resolveRecipe(string $value): DomainRecipe
|
|
|
|
|
{
|
|
|
|
|
$recipeDirectory = app(RecipeDirectory::class);
|
|
|
|
|
|
|
|
|
|
if (Str::isUuid($value) && UuidHelper::isV7(Uuid::fromString($value))) {
|
|
|
|
|
$recipe = $recipeDirectory->find(new RecipeId(Uuid::fromString($value)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! isset($recipe)) {
|
|
|
|
|
$recipe = $recipeDirectory->findBySlug($value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! isset($recipe)) {
|
|
|
|
|
throw new ModelNotFoundException('Recipe not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $recipe;
|
|
|
|
|
}
|
2026-07-08 16:32:12 +02:00
|
|
|
}
|