<?php
namespace App\EventListener;
use App\Event\ApplicationCompletedEvent;
use App\Event\OrderEvent;
use App\Event\OrderVerifiedEvent;
use App\Event\RegisterCompletedEvent;
use App\Event\SendOrderPincodeEvent;
use App\Security\EmailVerifier;
use Boab\EcommerceBundle\Entity\OrderInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Address;
class EmailMessengerListener
{
private $mailer;
private EmailVerifier $emailVerifier;
public function __construct(EmailVerifier $emailVerifier)
{
$this->emailVerifier = $emailVerifier;
}
public function onOrderCreated(OrderEvent $event)
{
$order = $event->getOrder();
$this->sendEmailMessage(
sprintf('Your OrderId is %s ', $order->getOrderId()),
'Thank you for your order',
$order
);
}
public function onSendPincode(SendOrderPincodeEvent $event)
{
$order = $event->getOrder();
$pincode = $order->getPincode();
$this->sendEmailMessage(
sprintf('Your PIN is %s ', $pincode->getPin()),
'Registration Pin',
$order
);
}
private function sendEmailMessage($text, $subject, OrderInterface $order)
{
$message = (new \Swift_Message($subject))
->setFrom(['mailer@lpi.edu.gh'=>'Lister Professional Institute'])
->setTo([$order->getEmail() => $order->getFirstName().' '. $order->getLastName()])
->setBody($text, 'text/html')
->addPart($text, 'text/plain');
try{
$this->mailer->send($message);
}catch(\Exception $e){
}
}
public function onOrderVerified(OrderVerifiedEvent $event)
{
$order = $event->getOrder();
if($order->getStatus() === OrderInterface::PENDING){
return;
}
$pincode = $order->getPincode();
$this->sendEmailMessage(
sprintf('Your PIN is %s ', $pincode->getPin()),
'Registration Pin',
$order
);
}
public function onApplicationCompleted(ApplicationCompletedEvent $event)
{
$application = $event->getApplication();
$user = $application->getStudent();
// generate a signed url and email it to the user
$this->emailVerifier->sendEmailConfirmation('app.verify_registration', $user,
(new TemplatedEmail())
->from(new Address('mailer@cmc-ghana.org', 'Application Verification'))
->to($user->getEmail())
->subject('Please Confirm your Email')
->htmlTemplate('registration/confirmation_email.html.twig')
);
}
}