<?php
namespace App\Util;
use App\Entity\SMS;
use App\Repository\SMSRepository;
use App\Repository\SMSTemplateRepository;
use Doctrine\ORM\EntityManagerInterface;
use Swift_Attachment;
use Symfony\Component\HttpClient\HttpClient;
class SMSUtil
{
protected $endpoint = 'http://vpn.czech-kiosk.cz:46500/api/';
const NEW_ACCESS_COMMANDER_CODE = 'new-access-commander-code';
const NEW_ACCESS_COMMANDER_CODE_EN = 'new-access-commander-code-en';
const SMS_PRIORITY_NORMAL = 'Normal';
const SMS_PRIORITY_HIGH = 'High';
const FROM = '+420739000002';
/**
* @var SMSRepository
*/
protected $SMSRepository;
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var SMSTemplateRepository
*/
protected $SMSTemplateRepository;
private $smsDataUtil;
public function __construct(
SMSRepository $SMSRepository,
EntityManagerInterface $entityManager,
SMSTemplateRepository $SMSTemplateRepository,
SMSDataUtil $SMSDataUtil
)
{
$this->SMSRepository = $SMSRepository;
$this->entityManager = $entityManager;
$this->SMSTemplateRepository = $SMSTemplateRepository;
$this->smsDataUtil = $SMSDataUtil;
}
public function transformDataByTemplate($SMSIdentifier, $data)
{
switch ($SMSIdentifier) {
case self::NEW_ACCESS_COMMANDER_CODE:
return $this->smsDataUtil->transform(SMSDataUtil::SMS_DATA_TRANSFORM_ACCESS_COMMANDER, $data);
break;
}
return [];
}
public function addSMSToQueueFromTemplateWithDataTransform($recipient, $SMSIdentifier, $data, $guid = null, $sendAt = null, $priority = self::SMS_PRIORITY_NORMAL)
{
$data = $this->transformDataByTemplate($SMSIdentifier, $data);
return $this->addSMSToQueueFromTemplate($recipient, $SMSIdentifier, $data, $guid, $sendAt, $priority);
}
private function addSMSToQueueFromTemplate($recipient, $SMSIdentifier, $data = [], $guid = null, $sendAt = null, $priority = self::SMS_PRIORITY_NORMAL)
{
$SMSTemplate = $this->SMSTemplateRepository->findSMSTemplateByIdentifier($SMSIdentifier);
if (!$SMSTemplate) {
return false;
}
$from = null;
$SMS = $this->addSMSToQueue(
$recipient,
$this->replaceVariables($SMSTemplate->getText(), $data),
$sendAt,
$guid,
$priority,
$from
);
return true;
}
protected function replaceVariables($text, array $data)
{
foreach ($data as $key => $value) {
$text = str_replace('%' . strtoupper($key) . '%', $value, $text);
}
return $text;
}
public function randomGuid()
{
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 12; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
public function addSMSToQueue($recipient, $txt, $sendAt = null, $guid = null, $priority = self::SMS_PRIORITY_NORMAL, $from = null)
{
if (!$sendAt) {
$sendAt = new \DateTime();
}
if (!$guid) {
$guid = $this->randomGuid();
}
if (!$from) {
$from = self::FROM;
}
$SMS = new SMS();
$SMS->setFromNumber($from);
$SMS->setSendAt($sendAt);
$SMS->setGuid($guid);
$SMS->setPriority($priority);
$SMS->setRecipient($recipient);
$SMS->setText($txt);
$this->entityManager->persist($SMS);
$this->entityManager->flush();
return $SMS;
}
public function sendSMSQueue($limit)
{
$data = $this->SMSRepository->findSMSsToSend($limit);
foreach ($data as $SMS) {
$this->sendSMS($SMS);
}
return true;
}
private function sendSMS(SMS $SMS)
{
$client = HttpClient::create();
$response = $client->request('POST', $this->endpoint . 'sms', [
'json' => [
'guid' => $SMS->getGuid(),
'number' => $SMS->getRecipient(),
'text' => $SMS->getText(),
'priority' => $SMS->getPriority(),
],
]);
$responseData = $response->toArray(false);
$SMS->setIsSentToQueue(true);
$SMS->setSentToQueueAt(new \DateTime());
if(!empty($responseData['error']['type'])) {
$SMS->setSystemNote($responseData['error']['type'].': '.$responseData['error']['info']);
}
$this->entityManager->persist($SMS);
$this->entityManager->flush();
}
public function checkSMSStatus()
{
$data = $this->SMSRepository->findSMSStatusFromQueue();
foreach ($data as $SMS) {
$this->checkSMS($SMS);
}
return true;
}
private function checkSMS(SMS $SMS)
{
$client = HttpClient::create();
$response = $client->request('GET', $this->endpoint . 'sms?guid='.$SMS->getGuid());
$responseData = $response->toArray(false);
if(!empty($responseData['data']['state']) && $responseData['data']['state']=='Sent') {
$SMS->setSentAt(new \DateTime($responseData['data']['sendingTime']));
$SMS->setIsSent(true);
}
$this->entityManager->persist($SMS);
$this->entityManager->flush();
}
}