ldc-cocotte/app/Policies/Backend/ProductPolicy.php
2026-10-27 09:58:12 +01:00

109 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Policies\Backend;
use App\Models\Customer;
use App\Models\Product;
use App\Models\User;
use Domain\Enums\UserRole;
use Domain\Ports\Repositories\ProductRepository;
use Domain\Services\ProductAvailabilityService;
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 $_): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function delete(Customer | User $user, Product $_): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function createInco(Customer | User $user, Product $product): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin) && ! $product->inco()->exists();
}
public function updateInco(Customer | User $user, Product $product): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin) && $product->inco()->exists();
}
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 $product): bool
{
$domainProduct = $this->productRepository->find($product->getId());
return $domainProduct && $this->productService->isPublished($domainProduct);
}
}