ldc-cocotte/app/Policies/Backend/ProductPolicy.php

130 lines
3.3 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\Ports\Repositories\ProductRepository;
class ProductPolicy
{
public function __construct(
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 associateInco(Customer | User $user, Product | ProductDto $_): bool
{
if ($user instanceof Customer) {
return false;
}
return $user->role->isAtLeastEqualsTo(UserRole::Admin);
}
public function syncInco(Customer | User $user, Product | ProductDto $product): bool
{
return $this->updateInco($user, $product);
}
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
{
if ($product instanceof ProductDto) {
return $product->status;
}
$domainProduct = $this->productRepository->find($product->getId());
return $domainProduct && $domainProduct->isPublished();
}
}