109 lines
3.5 KiB
PHP
109 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Infrastructure\Adapters\Repositories;
|
|
|
|
use App\Models\PickupException;
|
|
use App\Models\Shop;
|
|
use App\Models\ShopPickupExceptionPivot;
|
|
use Carbon\CarbonPeriod;
|
|
use Domain\Models\Entities\PickupExceptionOnDate;
|
|
use Domain\Models\ValueObjects\Entities\PickupExceptionId;
|
|
use Domain\Models\ValueObjects\Entities\ShopId;
|
|
use Domain\Models\ValueObjects\ShopPickupOverride;
|
|
use Domain\Ports\Repositories\PickupExceptionRepository;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class EloquentPickupExceptionRepository implements PickupExceptionRepository
|
|
{
|
|
#[\Override]
|
|
public function find(PickupExceptionId $id): ?PickupExceptionOnDate
|
|
{
|
|
$ex = PickupException::query()
|
|
->where('id', $id->value)
|
|
->first();
|
|
|
|
return $ex ? $this->toDomain($ex) : null;
|
|
}
|
|
|
|
#[\Override]
|
|
public function persist(PickupExceptionOnDate $entity): void
|
|
{
|
|
$model = PickupException::query()
|
|
->where('id', $entity->id->value)
|
|
->firstOrNew();
|
|
|
|
$model->id = $entity->id->value->toString();
|
|
$model->date = Carbon::instance($entity->date);
|
|
$model->label = $entity->label;
|
|
$model->repeatable = $entity->repeatable;
|
|
$model->repeat_frequency = $entity->repeatFrequency;
|
|
$model->auto_updated = $entity->autoUpdated;
|
|
|
|
$model->save();
|
|
}
|
|
|
|
#[\Override]
|
|
public function delete(PickupExceptionOnDate $entity): void
|
|
{
|
|
PickupException::query()
|
|
->where('id', $entity->id->value)
|
|
->delete();
|
|
}
|
|
|
|
public function getExceptionsForShop(ShopId $shopId, CarbonPeriod $period): array
|
|
{
|
|
$shop = Shop::query()
|
|
->where('id', $shopId->value)
|
|
->firstOrFail();
|
|
|
|
return $shop->pickupExceptions()
|
|
->where('date', '>=', $period->getStartDate()->format('Y-m-d'))
|
|
->where('date', '<=', $period->getEndDate()?->format('Y-m-d'))
|
|
->get()
|
|
->map(fn (PickupException $ex) => $this->toShopDomain($ex, $ex->pivot))
|
|
->all();
|
|
}
|
|
|
|
public function getAllExceptions(): array
|
|
{
|
|
return PickupException::query()
|
|
->get()
|
|
->map(fn (PickupException $ex) => $this->toDomain($ex))
|
|
->all();
|
|
}
|
|
|
|
public function getExceptionsAssociatedWithShop(ShopId $shopId): array
|
|
{
|
|
$shop = Shop::query()
|
|
->where('id', $shopId->value)
|
|
->firstOrFail();
|
|
$shop->relationLoaded('pickupExceptions') ?: $shop->load('pickupExceptions');
|
|
|
|
return $shop->pickupExceptions
|
|
->map(fn (PickupException $ex) => $this->toShopDomain($ex, $ex->pivot))
|
|
->all();
|
|
}
|
|
|
|
private function toDomain(PickupException $model): PickupExceptionOnDate
|
|
{
|
|
return new PickupExceptionOnDate(
|
|
id: $model->getId(),
|
|
date: $model->date->toImmutable(),
|
|
label: $model->label,
|
|
repeatable: (bool) $model->repeatable,
|
|
repeatFrequency: $model->repeat_frequency,
|
|
autoUpdated: (bool) $model->auto_updated
|
|
);
|
|
}
|
|
|
|
private function toShopDomain(PickupException $model, ?ShopPickupExceptionPivot $pivot = null): ShopPickupOverride
|
|
{
|
|
return new ShopPickupOverride(
|
|
exception: $this->toDomain($model),
|
|
replacementDate: $pivot?->replacement_date?->toImmutable(),
|
|
orderDeadlineAt: $pivot?->order_deadline_at?->toImmutable()
|
|
);
|
|
}
|
|
}
|