69 lines
1.8 KiB
PHP
69 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Lib\LDC;
|
|
|
|
use GuzzleHttp\Client;
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
class MailjetSms
|
|
{
|
|
/** @var Application $app */
|
|
protected $app;
|
|
|
|
/** @var array $config */
|
|
protected $config = [];
|
|
|
|
public function __construct(Application $app)
|
|
{
|
|
$this->app = $app;
|
|
$this->loadDefaultOptions();
|
|
}
|
|
|
|
protected function loadDefaultOptions(): MailjetSms
|
|
{
|
|
$this->config = $this->app['config']->get('services.mailjet');
|
|
return $this;
|
|
}
|
|
|
|
protected function buildHttpClient(): Client
|
|
{
|
|
return new Client([
|
|
'allow_redirects' => false,
|
|
'connect_timeout' => 3,
|
|
'verify' => false,
|
|
'headers' => [
|
|
'User-Agent' => 'LDCMailjetSms/1.0',
|
|
'Accept' => 'application/json',
|
|
'Content-Type' => 'application/json',
|
|
'Authorization' => sprintf('Bearer %s', $this->config['sms_token']),
|
|
],
|
|
'timeout' => 5,
|
|
]);
|
|
}
|
|
|
|
public function send(string $message, string $to, $from = null): ResponseInterface
|
|
{
|
|
$from = $from ?? $this->config['sms_from'];
|
|
$client = $this->buildHttpClient();
|
|
|
|
try {
|
|
$response = $client->post($this->config['sms_endpoint'], [
|
|
'json' => [
|
|
'Text' => $message,
|
|
'From' => $from,
|
|
'To' => $to,
|
|
]
|
|
]);
|
|
} catch (\GuzzleHttp\Exception\RequestException $ex) {
|
|
Log::debug('error');
|
|
Log::debug((string)$ex->getResponse()->getBody());
|
|
throw $ex;
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
}
|
|
}
|