Added GetAvailableTimeslotsQuery, GetAvailableTimeslotsQueryInput, and GetAvailableTimeslotsQueryOutput to handle fetching available timeslots for a shop, improving the structure and separation of concerns.
56 lines
1.5 KiB
PHP
56 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Queries\Timeslots;
|
|
|
|
use App\Contracts\Query;
|
|
use App\Contracts\QueryInput;
|
|
use Carbon\CarbonImmutable;
|
|
use Carbon\CarbonInterface;
|
|
use Domain\Ports\Repositories\ShopRepository;
|
|
use Domain\Services\PickupSchedulingService;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Query<\App\Queries\Timeslots\GetAvailableTimeslotsQueryInput, \App\Queries\Timeslots\GetAvailableTimeslotsQueryOutput>
|
|
*/
|
|
final readonly class GetAvailableTimeslotsQuery implements Query
|
|
{
|
|
private const int TIMESLOTS_DAY_TARGET = 28;
|
|
|
|
public function __construct(
|
|
private readonly PickupSchedulingService $pickupService,
|
|
private readonly ShopRepository $shopRepository
|
|
) {
|
|
}
|
|
|
|
public function __invoke(QueryInput $input): GetAvailableTimeslotsQueryOutput
|
|
{
|
|
Assert::isAOf(
|
|
$input,
|
|
GetAvailableTimeslotsQueryInput::class,
|
|
);
|
|
|
|
$domainShop = $this->shopRepository->find($input->forShop);
|
|
|
|
/** @var \Domain\Models\ValueObjects\PickupTimeSlot[] */
|
|
$timeslotList = [];
|
|
|
|
if ($domainShop !== null) {
|
|
$timeslotList = $this->pickupService->getTimeSlotsFor(
|
|
$domainShop,
|
|
$this->getUntil()
|
|
);
|
|
}
|
|
|
|
return new GetAvailableTimeslotsQueryOutput(
|
|
timeslotList: $timeslotList
|
|
);
|
|
}
|
|
|
|
private function getUntil(): CarbonInterface
|
|
{
|
|
return CarbonImmutable::now()->addDays(self::TIMESLOTS_DAY_TARGET);
|
|
}
|
|
}
|