<?php
namespace App\Event;
use App\Entity\JobAlertHistory;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
class JobAlertSubscriber
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function jobalertSent(JobAlertSentEvent $event)
{
$now = new DateTime('now');
$jobAlert = $event->getJobalert();
$jobAlert->setLastSentOn($now);
foreach ($event->getVacancies() as $vacancy) {
$jobAlertHistory = (new JobAlertHistory())
->setJobAlert($jobAlert)
->setVacancy($vacancy)
->setSentAt($now)
;
$this->entityManager->persist($jobAlertHistory);
}
$this->entityManager->persist($jobAlert);
$this->entityManager->flush();
}
}