82 lines
2.8 KiB
PHP
82 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Contracts\Executor;
|
|
use App\Cqs\Queries\Products\FindProductByIdInput;
|
|
use App\Cqs\Queries\Products\FindProductBySlugInput;
|
|
use App\Cqs\Queries\Products\FindProductQuery;
|
|
use App\Cqs\Queries\Recipes\FindRecipeByIdInput;
|
|
use App\Cqs\Queries\Recipes\FindRecipeBySlugInput;
|
|
use App\Cqs\Queries\Recipes\FindRecipeQuery;
|
|
use App\Dto\ProductDto;
|
|
use App\Dto\RecipeDto;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
class RouteServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Define your route model bindings, pattern filters, and other route configuration.
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
Route::bind('product', fn (string $value): ProductDto => $this->resolveProduct($value));
|
|
Route::bind('recipe', fn (string $value): RecipeDto => $this->resolveRecipe($value));
|
|
Route::bind('frontProduct', fn (string $value): ProductDto => $this->resolveProduct($value));
|
|
Route::bind('frontRecipe', fn (string $value): RecipeDto => $this->resolveRecipe($value));
|
|
}
|
|
|
|
private function resolveProduct(string $value): ProductDto
|
|
{
|
|
$executor = app()->make(Executor::class);
|
|
$findProductQuery = app()->make(FindProductQuery::class);
|
|
|
|
$input = Uuid::isValid($value)
|
|
? new FindProductByIdInput(Uuid::fromString($value), includeTrashed: $this->isBackRequest())
|
|
: new FindProductBySlugInput($value, includeTrashed: $this->isBackRequest());
|
|
|
|
/** @var ProductDto $product */
|
|
$product = $executor->execute($findProductQuery, $input) ?? abort(404);
|
|
|
|
if (! $this->isBackRequest() && ! $product->isPublished) {
|
|
abort(404);
|
|
}
|
|
|
|
return $product;
|
|
}
|
|
|
|
private function resolveRecipe(string $value): RecipeDto
|
|
{
|
|
$executor = app()->make(Executor::class);
|
|
$findRecipeQuery = app()->make(FindRecipeQuery::class);
|
|
|
|
$input = Uuid::isValid($value)
|
|
? new FindRecipeByIdInput(Uuid::fromString($value), includeTrashed: $this->isBackRequest())
|
|
: new FindRecipeBySlugInput($value, includeTrashed: $this->isBackRequest());
|
|
|
|
/** @var RecipeDto $recipe */
|
|
$recipe = $executor->execute($findRecipeQuery, $input) ?? abort(404);
|
|
|
|
if (! $this->isBackRequest() && ! $recipe->isPublished) {
|
|
abort(404);
|
|
}
|
|
|
|
return $recipe;
|
|
}
|
|
|
|
private function isBackRequest(): bool
|
|
{
|
|
$prefix = config('uwil-backend.routes.prefix');
|
|
$adminPrefix = is_string($prefix) ? $prefix : 'admin';
|
|
|
|
return request()->is($adminPrefix . '/*')
|
|
|| request()->is($adminPrefix)
|
|
|| request()->is('back/*')
|
|
|| request()->is('back')
|
|
|| request()->routeIs('back.*');
|
|
}
|
|
}
|