calendi/backend/src/Domain/Event/PersistEventHandler.php
2025-04-28 07:42:42 +02:00

28 lines
735 B
PHP

<?php
namespace App\Domain\Event;
use App\Domain\Event\PersistEvent;
use App\Domain\Model\Persisted\PersistedEvent;
use App\Infrastructure\Repository\EventRepository;
class PersistEventHandler
{
public function __construct(
private readonly EventRepository $eventRepository,
) {
}
public function handle(PersistEvent $event): void
{
$persistedEvent = new PersistedEvent();
if ($event->id !== null) {
$persistedEvent = $this->eventRepository->find($event->id);
if (!$persistedEvent) {
throw new \Exception('Event not found');
}
}
$this->eventRepository->save($event->draft->mergeIntoPersisted($persistedEvent));
}
}