<?php
declare(strict_types=1);
namespace Harmonizely\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Harmonizely\Model\EventInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Webmozart\Assert\Assert;
class SetEventEndDateListener
{
public function prePersist(LifecycleEventArgs $event): void
{
$object = $event->getObject();
if (!$object instanceof EventInterface) {
return;
}
$this->setEndDate($object);
}
public function preUpdate(LifecycleEventArgs $args): void
{
$entity = $args->getEntity();
if (!$entity instanceof EventInterface) {
return;
}
$this->setEndDate($entity);
}
public function setEventEndDate(GenericEvent $event): void
{
/** @var EventInterface $event */
$event = $event->getSubject();
Assert::isInstanceOf($event, EventInterface::class);
$this->setEndDate($event);
}
private function setEndDate(EventInterface $event): void
{
$duration = $event->getDuration();
$endDate = clone $event->getScheduledAt();
$endDate = $endDate->modify(sprintf('+%d minutes', $duration));
$event->setEndDate($endDate);
}
}