Skip to content

Instantly share code, notes, and snippets.

@parijke
Created June 20, 2020 14:47
Show Gist options
  • Save parijke/d2eae43b80d2d6f9db28cf4e582e9cef to your computer and use it in GitHub Desktop.
Save parijke/d2eae43b80d2d6f9db28cf4e582e9cef to your computer and use it in GitHub Desktop.
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