<?php
namespace App\Subscriber;
use App\Entity\JobOffer;
use App\Service\JobOfferNewsletterSender;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class EasyAdminSubscriber implements EventSubscriberInterface
{
private $jobOfferNewsletterSender;
public function __construct(JobOfferNewsletterSender $jobOfferNewsletterSender)
{
$this->jobOfferNewsletterSender = $jobOfferNewsletterSender;
}
public static function getSubscribedEvents()
{
return array(
'easy_admin.pre_update' => array('setUpdatedAt'),
'easy_admin.post_persist' => array('afterCreated'),
'easy_admin.post_update' => array('afterUpdated'),
);
}
public function setUpdatedAt(GenericEvent $event)
{
$entity = $event->getSubject();
try {
$entity->setUpdatedAt(new \DateTime('now'));
} catch (\Exception $e) {
die($e->getMessage());
}
$event['entity'] = $entity;
}
/**
* @param GenericEvent $event
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function afterCreated(GenericEvent $event)
{
$entity = $event->getSubject();
if($entity instanceof JobOffer && $entity->isEnabled())
$this->jobOfferNewsletterSender->send($entity);
}
/**
* @param GenericEvent $event
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function afterUpdated(GenericEvent $event)
{
$entity = $event->getSubject();
if($entity instanceof JobOffer && $entity->isEnabled() && !$entity->isNewsletterSent())
$this->jobOfferNewsletterSender->send($entity);
}
}