45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Policies\Frontend;
|
|
|
|
use App\Models\Customer;
|
|
use App\Models\Product;
|
|
use App\Models\User;
|
|
use Domain\Ports\Repositories\ProductRepository;
|
|
use Domain\Services\ProductAvailabilityService;
|
|
|
|
class ProductPolicy
|
|
{
|
|
public function __construct(
|
|
private readonly ProductAvailabilityService $productService,
|
|
private readonly ProductRepository $productRepository
|
|
) {
|
|
}
|
|
|
|
public function viewPublished(null | Customer | User $_, Product $product): bool
|
|
{
|
|
$domainProduct = $this->productRepository->find($product->getId());
|
|
|
|
return $domainProduct && $this->productService->isPublished($domainProduct);
|
|
}
|
|
|
|
public function addToBasket(null | User | Customer $_, Product $product): bool
|
|
{
|
|
$domainProduct = $this->productRepository->find($product->getId());
|
|
|
|
return $domainProduct && $this->productService->isOrderable($domainProduct);
|
|
}
|
|
|
|
public function viewPrices(null | User | Customer $_, Product $product): bool
|
|
{
|
|
if (! ($_ instanceof Customer || $_ instanceof User)) {
|
|
return false;
|
|
}
|
|
|
|
$domainProduct = $this->productRepository->find($product->getId());
|
|
|
|
return $domainProduct && $this->productService->isPublished($domainProduct);
|
|
}
|
|
}
|