Files
rkm-conectasus-ci-sandbox/api/app/Services/Sms/SmsService.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

170 lines
4.6 KiB
PHP

<?php
namespace App\Services\Sms;
use App\Models\Intranet\Cliente;
use App\Models\Sms\SmsEnvio;
use App\Services\Service;
use App\Utils\StringUtil;
use DateTime;
use Illuminate\Support\Str;
use RuntimeException;
use Sms;
use SmsFacade;
use SmsResponse;
use App\Enums\Intranet\Sistema;
class SmsService extends Service
{
private const URL = 'https://api-rest.zenvia.com';
private const COUNTRY_PREFIX = '55';
private $cliente;
public function __construct()
{
require_once base_path() . '/app/Utils/Sms/autoload.php';
$query = Cliente::where('link', app('config')->get('database.default'))
->where('sistema_id', SISTEMA::CONECTASUS);
$this->cliente = $query->firstOrFail();
}
public function send(string $to, string $message, DateTime $schedule = null, int $aggregateId = null)
{
$this->checkIfProduction();
$sms = $this->getNewSmsInstance($to, $message, $schedule);
$response = $this->getFacade()->send($sms, $aggregateId);
return $this->saveEnvio($sms, $response, $aggregateId);
}
public function sendMultiple(array $smsList, int $aggregateId = null)
{
$this->checkIfProduction();
$smsInstances = [];
foreach ($smsList as $sms)
{
$smsInstances[] = $this->getNewSmsInstance($sms->to, $sms->message, $sms->schedule ?? null);
}
$response = $this->getFacade()->sendMultiple($smsInstances, $aggregateId);
$envios = collect();
foreach ($response as $i => $responseItem) {
$envios->push($this->saveEnvio($smsInstances[$i], $responseItem, $aggregateId));
}
return $envios;
}
private function getNewSmsInstance(string $to, string $message, DateTime $schedule = null)
{
$sms = new Sms();
$sms->setId(uniqid());
$sms->setTo(self::COUNTRY_PREFIX . $to);
$sms->setMsg(StringUtil::getWithoutAccents($message));
$sms->setCallbackOption(Sms::CALLBACK_NONE);
if (!is_null($schedule)) {
$sms->setSchedule($schedule->format('Y-m-d\TH:i:s'));
}
return $sms;
}
private function saveEnvio(Sms $sms, SmsResponse $response, int $aggregateId = null)
{
return SmsEnvio::create([
'to' => $sms->getTo(),
'message' => $sms->getMsg(),
'sms_id' => $sms->getId(),
'number_of_parts' => $this->getNumberOfParts($sms->getMsg()),
'schedule' => $sms->getSchedule(),
'aggregated_id' => $aggregateId,
'status_code' => $response->getStatusCode(),
'status_description' => $response->getStatusDescription(),
'detail_code' => $response->getDetailCode(),
'detail_description' => $response->getDetailDescription(),
]);
}
private function getNumberOfParts(string $message)
{
$length = Str::length($message);
if ($length <= 160) {
return 1;
}
else if ($length > 160 && $length <= 304) {
return 2;
}
else if ($length > 304 && $length <= 456) {
return 3;
}
else if ($length > 456 && $length <= 608) {
return 4;
}
}
public function cancel(int $smsEnvioId)
{
$this->checkIfProduction();
$smsEnvio = SmsEnvio::findOrFail($smsEnvioId);
if ($smsEnvio->number_of_parts === 1) {
$response = $this->getFacade()->cancel($smsEnvio->sms_id);
$this->saveCancelamento($smsEnvio, 1, $response);
}
for ($part = 1; $part <= $smsEnvio->number_of_parts; $part++) {
$smsPartId = $smsEnvio->sms_id . '_' . Str::padLeft($part, 3, '0');
$response = $this->getFacade()->cancel($smsPartId);
$this->saveCancelamento($smsEnvio, $part, $response);
}
return $smsEnvio->cancelamentos()->get();
}
private function saveCancelamento(SmsEnvio $smsEnvio, int $part, SmsResponse $response)
{
return $smsEnvio->cancelamentos()->create([
'part' => $part,
'status_code' => $response->getStatusCode(),
'status_description' => $response->getStatusDescription(),
'detail_code' => $response->getDetailCode(),
'detail_description' => $response->getDetailDescription(),
]);
}
private function checkIfProduction()
{
if (!app()->environment('production')) {
throw new RuntimeException();
}
}
private function getFacade()
{
return new SmsFacade($this->cliente->sms_usuario, $this->cliente->sms_senha, self::URL);
}
}