src/EventListener/EmailMessengerListener.php line 25

Open in your IDE?
  1. <?php 
  2. namespace App\EventListener;
  3. use App\Event\ApplicationCompletedEvent;
  4. use App\Event\OrderEvent;
  5. use App\Event\OrderVerifiedEvent;
  6. use App\Event\RegisterCompletedEvent;
  7. use App\Event\SendOrderPincodeEvent;
  8. use App\Security\EmailVerifier;
  9. use Boab\EcommerceBundle\Entity\OrderInterface;
  10. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  11. use Symfony\Component\Mime\Address;
  12. class EmailMessengerListener
  13. {
  14.     private $mailer;
  15.     private EmailVerifier $emailVerifier;
  16.     public function __construct(EmailVerifier $emailVerifier)
  17.     {
  18.         $this->emailVerifier $emailVerifier;
  19.     }
  20.     public function onOrderCreated(OrderEvent $event)
  21.     {
  22.         $order $event->getOrder();
  23.         $this->sendEmailMessage(
  24.             sprintf('Your OrderId is %s '$order->getOrderId()), 
  25.             'Thank you for your order',
  26.             $order
  27.         );
  28.     }    
  29.     public function onSendPincode(SendOrderPincodeEvent $event)
  30.     {
  31.         $order $event->getOrder();
  32.         $pincode $order->getPincode();
  33.         $this->sendEmailMessage(
  34.             sprintf('Your PIN is %s '$pincode->getPin()),
  35.             'Registration Pin',
  36.             $order
  37.         );
  38.     }
  39.     private function sendEmailMessage($text$subjectOrderInterface $order)
  40.     {
  41.         $message = (new \Swift_Message($subject))
  42.             ->setFrom(['mailer@lpi.edu.gh'=>'Lister Professional Institute'])
  43.             ->setTo([$order->getEmail() => $order->getFirstName().' '$order->getLastName()])
  44.             ->setBody($text'text/html')
  45.             ->addPart($text'text/plain');
  46.         try{
  47.             $this->mailer->send($message);
  48.         }catch(\Exception $e){
  49.         }        
  50.     }
  51.     public function onOrderVerified(OrderVerifiedEvent $event)
  52.     {
  53.         $order $event->getOrder();
  54.         if($order->getStatus() === OrderInterface::PENDING){
  55.             return;
  56.         }
  57.         
  58.         $pincode $order->getPincode();
  59.         $this->sendEmailMessage(
  60.             sprintf('Your PIN is %s '$pincode->getPin()),
  61.             'Registration Pin',
  62.             $order
  63.         );        
  64.     }
  65.     public function onApplicationCompleted(ApplicationCompletedEvent $event)
  66.     {
  67.         $application $event->getApplication();
  68.         $user $application->getStudent();
  69.         // generate a signed url and email it to the user
  70.         $this->emailVerifier->sendEmailConfirmation('app.verify_registration'$user,
  71.             (new TemplatedEmail())
  72.                 ->from(new Address('mailer@cmc-ghana.org''Application Verification'))
  73.                 ->to($user->getEmail())
  74.                 ->subject('Please Confirm your Email')
  75.                 ->htmlTemplate('registration/confirmation_email.html.twig')
  76.         );
  77.     }
  78. }