50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domain\Services;
|
|
|
|
use Carbon\CarbonImmutable;
|
|
use Domain\Models\Entities\Product;
|
|
use Domain\Models\ValueObjects\BasketProduct;
|
|
use Domain\Ports\Repositories\ProductRepository;
|
|
|
|
class ProductAvailabilityService
|
|
{
|
|
public function __construct(
|
|
private readonly ProductRepository $productRepository
|
|
) {
|
|
}
|
|
|
|
public function isOrderable(Product $product): bool
|
|
{
|
|
$now = CarbonImmutable::now();
|
|
$start = ! $product->orderable_start || $now->gt($product->orderable_start);
|
|
$end = ! $product->orderable_end || $now->lt($product->orderable_end);
|
|
$isOrderable = $start && $end;
|
|
|
|
return $isOrderable && $product->status && $this->isAssociatedWithInco($product);
|
|
}
|
|
|
|
public function isPublished(Product $product): bool
|
|
{
|
|
return $product->status;
|
|
}
|
|
|
|
public function isBasketProductPublished(BasketProduct $basketProduct): bool
|
|
{
|
|
$product = $this->productRepository->find($basketProduct->id);
|
|
|
|
if (! $product) {
|
|
// NOTE: Don't throw an exception here, as the product may have been deleted after being added to the basket
|
|
return false;
|
|
}
|
|
|
|
return $this->isPublished($product);
|
|
}
|
|
|
|
public function isAssociatedWithInco(Product $product): bool
|
|
{
|
|
return $product->hasInco;
|
|
}
|
|
}
|