<?php
namespace Boab\EcommerceBundle\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Boab\CmsBundle\Sms\SMSGatewayInterface;
use Boab\EcommerceBundle\Entity\OrderInterface;
use Boab\EcommerceBundle\Event\PaymentCompletedEvent;
use Boab\EcommerceBundle\Events;
class SMSMesengerSubscriber implements EventSubscriberInterface
{
private $smsGateway;
private $requestStack;
public function __construct(SMSGatewayInterface $smsGateway, RequestStack $requestStack)
{
$this->smsGateway = $smsGateway;
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents():array
{
return [
Events::PAYMENT_SUCCESS => 'onPaymentSuccess',
Events::SEND_PIN_CODE => 'onSendPin',
];
}
public function onPaymentSuccess(PaymentCompletedEvent $event)
{
$this->sendPin($event->getOrder());
}
public function onSendPin(OrderInterface $order)
{
$this->sendPin($order);
}
public function sendPin(OrderInterface $order)
{
$request = $this->requestStack->getCurrentRequest();
$textSession = $request->getSession()->get('TEXT_MESSAGE_SENT');
if($order->isPaid()){
$smsMessenger = $this->smsGateway->create('mnotify');
$smsResponse = $smsMessenger->send($order->getContactNumber(), sprintf('OrderId: %s, PIN: %s ', $order->getOrderId(), $order->getPin()));
$request->getSession()->set('TEXT_MESSAGE_SENT', $order->getOrderId());
}
}
}