<?php
namespace App\EventSubscriber;
use App\Entity\Patient;
use App\Event\CreateAppointmentEvent;
use Doctrine\DBAL\Driver\Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Repository\SmsMessageRepository;
use App\Repository\PatientRepository;
class CreateAppointmentEventSubscriber implements EventSubscriberInterface
{
private $smsMessageRepository;
private $patientRepository;
/**
* CreatePatientEventSubscriber constructor.
*/
public function __construct(SmsMessageRepository $smsMessageRepository,
PatientRepository $patientRepository)
{
$this->smsMessageRepository = $smsMessageRepository;
$this->patientRepository = $patientRepository;
}
public static function getSubscribedEvents(): array
{
return [
CreateAppointmentEvent::NAME => [
['updateSmsMessage', 0],
],
];
}
/**
* @throws Exception
* @throws \Doctrine\DBAL\Exception
*/
public function updateSmsMessage(CreateAppointmentEvent $event)
{
if (!$event->getAppointment()->getPatientId()) {
return;
}
/** @var Patient $patient */
$patient = $this->patientRepository->find($event->getAppointment()->getPatientId());
if (!$patient) {
return;
}
if (!$patient->getUpdateSmsMessage()) {
return;
}
$this->smsMessageRepository->setInstitutionForSentSmsMessage(
$patient->getPhoneNumber(),
$event->getAppointment()->getDoctor()->getDepartment()->getInstitution());
$patient->setNoNeedUpdateSmsMessage();
$this->patientRepository->update();
}
}