Created
June 20, 2020 14:47
-
-
Save parijke/d2eae43b80d2d6f9db28cf4e582e9cef to your computer and use it in GitHub Desktop.
Todo EVent Subscriber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
->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