src/Util/SMSUtil.php line 186

Open in your IDE?
  1. <?php
  2. namespace App\Util;
  3. use App\Entity\SMS;
  4. use App\Repository\SMSRepository;
  5. use App\Repository\SMSTemplateRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Swift_Attachment;
  8. use Symfony\Component\HttpClient\HttpClient;
  9. class SMSUtil
  10. {
  11.     protected $endpoint 'http://vpn.czech-kiosk.cz:46500/api/';
  12.     const NEW_ACCESS_COMMANDER_CODE 'new-access-commander-code';
  13.     const NEW_ACCESS_COMMANDER_CODE_EN 'new-access-commander-code-en';
  14.     const SMS_PRIORITY_NORMAL 'Normal';
  15.     const SMS_PRIORITY_HIGH 'High';
  16.     const FROM '+420739000002';
  17.     /**
  18.      * @var SMSRepository
  19.      */
  20.     protected $SMSRepository;
  21.     /**
  22.      * @var EntityManagerInterface
  23.      */
  24.     protected $entityManager;
  25.     /**
  26.      * @var SMSTemplateRepository
  27.      */
  28.     protected $SMSTemplateRepository;
  29.     private $smsDataUtil;
  30.     public function __construct(
  31.         SMSRepository $SMSRepository,
  32.         EntityManagerInterface $entityManager,
  33.         SMSTemplateRepository $SMSTemplateRepository,
  34.         SMSDataUtil $SMSDataUtil
  35.     )
  36.     {
  37.         $this->SMSRepository $SMSRepository;
  38.         $this->entityManager $entityManager;
  39.         $this->SMSTemplateRepository $SMSTemplateRepository;
  40.         $this->smsDataUtil $SMSDataUtil;
  41.     }
  42.     public function transformDataByTemplate($SMSIdentifier$data)
  43.     {
  44.         switch ($SMSIdentifier) {
  45.             case self::NEW_ACCESS_COMMANDER_CODE:
  46.                 return $this->smsDataUtil->transform(SMSDataUtil::SMS_DATA_TRANSFORM_ACCESS_COMMANDER$data);
  47.                 break;
  48.         }
  49.         return [];
  50.     }
  51.     public function addSMSToQueueFromTemplateWithDataTransform($recipient$SMSIdentifier$data$guid null$sendAt null$priority self::SMS_PRIORITY_NORMAL)
  52.     {
  53.         $data $this->transformDataByTemplate($SMSIdentifier$data);
  54.         return $this->addSMSToQueueFromTemplate($recipient$SMSIdentifier$data$guid$sendAt$priority);
  55.     }
  56.     private function addSMSToQueueFromTemplate($recipient$SMSIdentifier$data = [], $guid null$sendAt null$priority self::SMS_PRIORITY_NORMAL)
  57.     {
  58.         $SMSTemplate $this->SMSTemplateRepository->findSMSTemplateByIdentifier($SMSIdentifier);
  59.         if (!$SMSTemplate) {
  60.             return false;
  61.         }
  62.         $from null;
  63.         $SMS $this->addSMSToQueue(
  64.             $recipient,
  65.             $this->replaceVariables($SMSTemplate->getText(), $data),
  66.             $sendAt,
  67.             $guid,
  68.             $priority,
  69.             $from
  70.         );
  71.         return true;
  72.     }
  73.     protected function replaceVariables($text, array $data)
  74.     {
  75.         foreach ($data as $key => $value) {
  76.             $text str_replace('%' strtoupper($key) . '%'$value$text);
  77.         }
  78.         return $text;
  79.     }
  80.     public function randomGuid()
  81.     {
  82.         $alphabet 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  83.         $pass = array(); //remember to declare $pass as an array
  84.         $alphaLength strlen($alphabet) - 1//put the length -1 in cache
  85.         for ($i 0$i 12$i++) {
  86.             $n rand(0$alphaLength);
  87.             $pass[] = $alphabet[$n];
  88.         }
  89.         return implode($pass); //turn the array into a string
  90.     }
  91.     public function addSMSToQueue($recipient$txt$sendAt null$guid null$priority self::SMS_PRIORITY_NORMAL$from null)
  92.     {
  93.         if (!$sendAt) {
  94.             $sendAt = new \DateTime();
  95.         }
  96.         if (!$guid) {
  97.             $guid $this->randomGuid();
  98.         }
  99.         if (!$from) {
  100.             $from self::FROM;
  101.         }
  102.         $SMS = new SMS();
  103.         $SMS->setFromNumber($from);
  104.         $SMS->setSendAt($sendAt);
  105.         $SMS->setGuid($guid);
  106.         $SMS->setPriority($priority);
  107.         $SMS->setRecipient($recipient);
  108.         $SMS->setText($txt);
  109.         $this->entityManager->persist($SMS);
  110.         $this->entityManager->flush();
  111.         return $SMS;
  112.     }
  113.     public function sendSMSQueue($limit)
  114.     {
  115.         $data $this->SMSRepository->findSMSsToSend($limit);
  116.         foreach ($data as $SMS) {
  117.             $this->sendSMS($SMS);
  118.         }
  119.         return true;
  120.     }
  121.     private function sendSMS(SMS $SMS)
  122.     {
  123.         $client HttpClient::create();
  124.         $response $client->request('POST'$this->endpoint 'sms', [
  125.             'json' => [
  126.                 'guid' => $SMS->getGuid(),
  127.                 'number' => $SMS->getRecipient(),
  128.                 'text' => $SMS->getText(),
  129.                 'priority' => $SMS->getPriority(),
  130.             ],
  131.         ]);
  132.         $responseData $response->toArray(false);
  133.         $SMS->setIsSentToQueue(true);
  134.         $SMS->setSentToQueueAt(new \DateTime());
  135.         if(!empty($responseData['error']['type'])) {
  136.             $SMS->setSystemNote($responseData['error']['type'].': '.$responseData['error']['info']);
  137.         }
  138.         $this->entityManager->persist($SMS);
  139.         $this->entityManager->flush();
  140.     }
  141.     public function checkSMSStatus()
  142.     {
  143.         $data $this->SMSRepository->findSMSStatusFromQueue();
  144.         foreach ($data as $SMS) {
  145.             $this->checkSMS($SMS);
  146.         }
  147.         return true;
  148.     }
  149.     private function checkSMS(SMS $SMS)
  150.     {
  151.         $client HttpClient::create();
  152.         $response $client->request('GET'$this->endpoint 'sms?guid='.$SMS->getGuid());
  153.         $responseData $response->toArray(false);
  154.         if(!empty($responseData['data']['state']) && $responseData['data']['state']=='Sent') {
  155.             $SMS->setSentAt(new \DateTime($responseData['data']['sendingTime']));
  156.             $SMS->setIsSent(true);
  157.         }
  158.         $this->entityManager->persist($SMS);
  159.         $this->entityManager->flush();
  160.     }
  161. }