Merge branch 'develop' into trunk

This commit is contained in:
Vincent BRIDIER 2021-08-04 09:47:06 +02:00
commit 6fb04f45ce

View file

@ -0,0 +1,70 @@
<?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 ' . $ex->getMessage());
if (!is_null($ex->getResponse())){
Log::debug((string) $ex->getResponse()->getBody());
}
throw $ex;
}
return $response;
}
}