Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Todo EVent Subscriber
<?php
namespace App\EventSubscribers;
use App\Entity\Todo;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
class TodoSubscriber implements EventSubscriber {
/**
* @var MailerInterface
*/
private $mailer;
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
public function getSubscribedEvents()
{
return [
Events::postPersist,
Events::postUpdate,
];
}
public function postUpdate(LifecycleEventArgs $args)
{
$this->index($args);
}
public function postPersist(LifecycleEventArgs $args)
{
$this->index($args);
}
public function index(LifecycleEventArgs $args)
{
$entity = $args->getObject();
// perhaps you only want to act on some "Product" entity
if (!$entity instanceof Todo) {
// todo send email to assigned user
// $entityManager = $args->getObjectManager();
return;
}
$mail = new Email();
$mail
->from('mail@example.com')
->to($entity->getAssignedTo()->getEmail())
->subject('Todo is changed or created')
->html(print_r($entity));
dd($mail);
$this->mailer->send($mail);
// dd('Todo is gemaakt of gewijzigd');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.