Merge branch 'develop' into trunk
This commit is contained in:
commit
6fb04f45ce
1 changed files with 70 additions and 0 deletions
70
app/Lib/LDC/MailjetSms.php
Normal file
70
app/Lib/LDC/MailjetSms.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue