ldc-boutique-cnc-sable/app/Queries/Shops/GetShopQuery.php

103 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Queries\Shops;
use App\Contracts\Query;
use App\Contracts\QueryInput;
use App\Dto\PickupExceptionDto;
use App\Dto\ShopDto;
use App\Dto\ShopPickupExceptionPivotDto;
use Carbon\CarbonInterface;
use Domain\Models\Entities\Shop;
use Domain\Models\ValueObjects\ShopPickupOverride;
use Domain\Ports\Repositories\PickupExceptionRepository;
use Domain\Ports\Repositories\ShopRepository;
use Illuminate\Support\Collection;
use Webmozart\Assert\Assert;
/**
* @implements \App\Contracts\Query<\App\Queries\Shops\GetShopQueryInput, \App\Dto\ShopDto>
*/
final readonly class GetShopQuery implements Query
{
public function __construct(
private ShopRepository $shopRepository,
private PickupExceptionRepository $pickupExceptionRepository,
) {
}
/**
* @param \App\Queries\Shops\GetShopQueryInput $input
*/
#[\Override]
public function __invoke(QueryInput $input): ShopDto
{
Assert::isInstanceOf($input, GetShopQueryInput::class);
$domainShop = $this->shopRepository->getDefaultShop();
$pickupExceptionList = $this->pickupExceptionRepository->getExceptionsAssociatedWithShop($domainShop->id);
$mappedExceptions = Collection::make(
array_map(
fn (ShopPickupOverride $pickupOverride): PickupExceptionDto => $this->mapDomainPickupExceptionToDto(
$pickupOverride,
),
$pickupExceptionList
)
);
return $this->mapDomainShopToDto(
$domainShop,
$mappedExceptions
);
}
/** @param \Illuminate\Support\Collection<array-key, \App\Dto\PickupExceptionDto> $pickupExceptionList */
private function mapDomainShopToDto(
Shop $domainShop,
Collection $pickupExceptionList,
): ShopDto {
return new ShopDto(
id: $domainShop->id->value->toString(),
name: $domainShop->name,
address: $domainShop->address,
postcode: $domainShop->postcode,
city: $domainShop->city,
phone: $domainShop->phone,
latitude: $domainShop->latitude,
longitude: $domainShop->longitude,
default: $domainShop->isDefault,
pickupExceptions: $pickupExceptionList,
);
}
private function mapDomainPickupExceptionToDto(ShopPickupOverride $shopPickupOverride): PickupExceptionDto
{
$exception = $shopPickupOverride->exception;
return new PickupExceptionDto(
id: $exception->id->value->toString(),
date: $exception->date,
label: $exception->label,
repeatable: $exception->repeatable,
repeat_frequency: $exception->repeatFrequency,
auto_updated: $exception->autoUpdated,
pivot: $this->mapShopPickupExceptionPivotDto(
$shopPickupOverride->replacementDate,
$shopPickupOverride->orderDeadlineAt,
),
);
}
private function mapShopPickupExceptionPivotDto(
?CarbonInterface $replacementDate,
?CarbonInterface $orderDeadlineAt,
): ShopPickupExceptionPivotDto {
return new ShopPickupExceptionPivotDto(
$replacementDate,
$orderDeadlineAt
);
}
}