- Implement tests for product publication status. - Add tests for product orderability based on various conditions. - Enhance pricing calculation tests for different discount scenarios. - Remove outdated PricingServiceTest and refactor ProductAvailabilityServiceTest.
132 lines
3.4 KiB
PHP
132 lines
3.4 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 Ramsey\Uuid\Uuid;
|
|
|
|
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 $product): 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
|
|
{
|
|
$productId = $product instanceof ProductDto
|
|
? new ProductId(Uuid::fromString($product->id))
|
|
: $product->getId();
|
|
|
|
$domainProduct = $this->productRepository->find($productId);
|
|
|
|
return $domainProduct && $domainProduct->isPublished();
|
|
}
|
|
}
|