resq/src/Service/RisingTideGateway.php
2021-06-02 10:00:35 +00:00

110 lines
3 KiB
PHP

<?php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use DateTime;
use App\Entity\SMSMessage;
class RisingTideGateway
{
const SERVER_URL = 'https://xdngw01.wyrls.net/documents';
// entity manager
protected $em;
// rising tide parameters
protected $user;
protected $pass;
protected $usage_type;
protected $shortcode;
protected $dr_url;
public function __construct(EntityManagerInterface $em, $user, $pass, $usage_type, $shortcode, $dr_url)
{
$this->em = $em;
$this->user = $user;
$this->pass = $pass;
$this->usage_type = $usage_type;
$this->shortcode = $shortcode;
$this->dr_url = $dr_url;
}
public function sendSMS($mobile_num, $mask, $message)
{
$headers = [
'Content-Type: application/vnd.net.wyrls.Document-v3+json'
];
$sms = new SMSMessage();
$sms->setFrom($this->shortcode)
->setFromAlias($mask)
->setTo($mobile_num)
->setMessage($message)
->setStatus('sent');
$this->em->persist($sms);
$this->em->flush();
$date = $sms->getDateCreate();
// $date = new DateTime();
$date_string = $date->format('Y-m-d') . 'T' . $date->format('H:m:s');
$data = [
'id' => $sms->getID(),
'from' => $this->shortcode,
'from_alias' => $mask,
'to' => $mobile_num,
'content_type' => 'text/plain',
'body' => $message,
'date' => $date_string,
'usagetype' => $this->usage_type,
'delivery_receipt_url' => $this->dr_url,
];
error_log(print_r($data, true));
$data_json = json_encode($data);
$userpwd = $this->user . ':' . $this->pass;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, self::SERVER_URL);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $userpwd);
$result = curl_exec($curl);
error_log('error_no - ' . curl_errno($curl));
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
error_log($http_code);
curl_close($curl);
error_log($result);
}
public function validatePhoneNumber($mobile)
{
// check valid number
$num = trim($mobile);
// should be 10 digits
if (strlen($num) != 10)
return false;
// should start with '9'
if ($num[0] != '9')
return false;
// should be numeric
if (!is_numeric($num))
return false;
// should not be 9900000000
if ($num == '9900000000')
return false;
return true;
}
}