2021-08-04 09:46:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Lib\LDC;
|
|
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
|
|
|
|
|
class MailjetSms
|
|
|
|
|
{
|
|
|
|
|
/** @var Application $app */
|
|
|
|
|
protected $app;
|
2022-09-21 11:17:58 +00:00
|
|
|
|
2021-08-04 09:46:42 +02:00
|
|
|
/** @var array $config */
|
|
|
|
|
protected $config = [];
|
|
|
|
|
|
2022-09-21 11:17:58 +00:00
|
|
|
/**
|
|
|
|
|
* MailjetSms
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app
|
|
|
|
|
*/
|
2021-08-04 09:46:42 +02:00
|
|
|
public function __construct(Application $app)
|
|
|
|
|
{
|
|
|
|
|
$this->app = $app;
|
|
|
|
|
$this->loadDefaultOptions();
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 11:17:58 +00:00
|
|
|
/**
|
|
|
|
|
* loadDefaultOptions
|
|
|
|
|
*
|
|
|
|
|
* @return \App\Lib\LDC\MailjetSms
|
|
|
|
|
*/
|
|
|
|
|
protected function loadDefaultOptions(): \App\Lib\LDC\MailjetSms
|
2021-08-04 09:46:42 +02:00
|
|
|
{
|
|
|
|
|
$this->config = $this->app['config']->get('services.mailjet');
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 11:17:58 +00:00
|
|
|
/**
|
|
|
|
|
* buildHttpClient
|
|
|
|
|
*
|
|
|
|
|
* @return \GuzzleHttp\Client
|
|
|
|
|
*/
|
|
|
|
|
protected function buildHttpClient(): \GuzzleHttp\Client
|
2021-08-04 09:46:42 +02:00
|
|
|
{
|
|
|
|
|
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,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 11:17:58 +00:00
|
|
|
/**
|
|
|
|
|
* send sms
|
|
|
|
|
*
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @param string $to
|
|
|
|
|
* @param string|null $from
|
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
|
* @throws \GuzzleHttp\Exception\RequestException If Mailsms send service call failed.
|
|
|
|
|
*/
|
|
|
|
|
public function send(string $message, string $to, ?string $from = null): ResponseInterface
|
2021-08-04 09:46:42 +02:00
|
|
|
{
|
2022-09-21 11:17:58 +00:00
|
|
|
$from = $from ?? $this->config['sms_from'];
|
2021-08-04 09:46:42 +02:00
|
|
|
$client = $this->buildHttpClient();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$response = $client->post($this->config['sms_endpoint'], [
|
|
|
|
|
'json' => [
|
|
|
|
|
'Text' => $message,
|
|
|
|
|
'From' => $from,
|
|
|
|
|
'To' => $to,
|
|
|
|
|
]
|
|
|
|
|
]);
|
2022-09-21 11:17:58 +00:00
|
|
|
} catch (\GuzzleHttp\Exception\RequestException $e) {
|
|
|
|
|
report($e);
|
|
|
|
|
throw $e;
|
2021-08-04 09:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
}
|