ldc-cocotte/app/Policies/Backend/ProductPolicy.php
Jérémy Munsch 70600ea623
refactor(products): migrate BO and FO ProductsControllers to CQS pattern
- Refactor Bo\ProductsController to use ActionExecutor and Queries
- Refactor Fo\Pages\ProductsController and Fo\Ajax\ProductsController to CQS
- Add Actions: CreateProduct, UpdateProduct, DeleteProduct with Inputs
- Add Queries: GetBoPaginatedProductsQuery, GetProductFormDataQuery, GetFoProductIndexPageQuery, GetFoFilteredProductsQuery
- Add DTOs: ProductFormDataDto and enrich ProductDto
- Encapsulate Action/Query inputs conversion in FormRequests
- Remove obsolete ProductDirectory and EloquentProductDirectory
- Add tests for Fo\Ajax\ProductsController
2026-10-27 09:58:42 +01:00

120 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Policies\Backend;
use App\Dto\ProductDto;
use App\Models\Customer;
use App\Models\Product;
use App\Models\User;
use Domain\Enums\UserRole;
use Domain\Models\ValueObjects\Entities\ProductId;
use Domain\Ports\Repositories\ProductRepository;
use Domain\Services\ProductAvailabilityService;
use Ramsey\Uuid\Uuid;
class ProductPolicy
{
public function __construct(
private readonly ProductAvailabilityService $productService,
private readonly ProductRepository $productRepository
) {
}
public function viewAny(Customer | User $user): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function create(Customer | User $user): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function update(Customer | User $user, Product | ProductDto $_): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function delete(Customer | User $user, Product | ProductDto $_): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function createInco(Customer | User $user, Product | ProductDto $product): bool
{
if ($user instanceof Customer) {
return false;
}
$hasInco = $product instanceof ProductDto ? $product->inco !== null : $product->inco()->exists();
return $user->role->isAtLeastEqualsTo(UserRole::Admin) && ! $hasInco;
}
public function updateInco(Customer | User $user, Product | ProductDto $product): bool
{
if ($user instanceof Customer) {
return false;
}
$hasInco = $product instanceof ProductDto ? $product->inco !== null : $product->inco()->exists();
return $user->role->isAtLeastEqualsTo(UserRole::Admin) && $hasInco;
}
public function toggleStatus(Customer | User $user): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function toggleStar(Customer | User $user): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function viewStarred(Customer | User $user): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function viewPublished(null | Customer | User $_, Product | ProductDto $product): bool
{
$productId = $product instanceof ProductDto
? new ProductId(Uuid::fromString($product->id))
: $product->getId();
$domainProduct = $this->productRepository->find($productId);
return $domainProduct && $this->productService->isPublished($domainProduct);
}
}