Ver código fonte

MailjetSMS : redirection vers email en environnement dev

develop
Guillaume 3 anos atrás
pai
commit
a74e24c4bf
1 arquivos alterados com 68 adições e 47 exclusões
  1. +68
    -47
      ShopBundle/Services/MailjetSMS.php

+ 68
- 47
ShopBundle/Services/MailjetSMS.php Ver arquivo

@@ -1,6 +1,6 @@
<?php

namespace Lc\ShopBundle\Services ;
namespace Lc\ShopBundle\Services;

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
@@ -8,53 +8,74 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;

class MailjetSMS
{
protected $client ;
protected $parameterBag ;

public function __construct(HttpClientInterface $client, ParameterBagInterface $parameterBag)
{
$this->client = $client ;
$this->parameterBag = $parameterBag ;
}

public function send($to, $message)
{
$token = $this->parameterBag->get('mailjet.sms.token') ;
$from = $this->parameterBag->get('mailjet.sms.from') ;

if($token && strlen($token) > 0) {
$response = $this->client->request(
'POST',
'https://api.mailjet.com/v4/sms-send',
[
'headers' => [
'Authorization' => 'Bearer '.$token,
//'Content-Type' => 'application/json',
],
'json' => [
'From' => $from,
'To' => $to,
'Text' => $message,
]
]
);

$statusCode = $response->getStatusCode();

if($statusCode == 200) {
$content = $response->getContent();
$content = $response->toArray();
return $content ;
}
else {
// log
}

return false ;
protected $client;
protected $parameterBag;
protected $mailUtils ;
protected $utils ;

public function __construct(HttpClientInterface $client, ParameterBagInterface $parameterBag, UtilsManager $utilsManager)
{
$this->client = $client;
$this->parameterBag = $parameterBag;
$this->mailUtils = $utilsManager->getMailUtils() ;
$this->utils = $utilsManager->getUtils() ;
}
else {
throw new \ErrorException('Le token SMS Mailjet n\'est pas défini.') ;

public function send($user, $message)
{

if($user) {
$phone = $this->utils->formatPhoneNumber($user->getPhone()) ;

if($this->parameterBag->get('mailjet.dev.redirect.active') == 1) {
$this->mailUtils->send([
MailUtils::SUBJECT => 'Notification par SMS à '.$phone,
MailUtils::TO_EMAIL => $user->getEmail(),
MailUtils::CONTENT_TEMPLATE => 'mail/notification',
MailUtils::CONTENT_DATA => [
'message' => $message
],
]);

return true ;
}
else {
$token = $this->parameterBag->get('mailjet.sms.token');
$from = $this->parameterBag->get('mailjet.sms.from');

if ($token && strlen($token) > 0) {
$response = $this->client->request(
'POST',
'https://api.mailjet.com/v4/sms-send',
[
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
'json' => [
'From' => $from,
'To' => $phone,
'Text' => $message,
]
]
);

$statusCode = $response->getStatusCode();

if ($statusCode == 200) {
$content = $response->getContent();
$content = $response->toArray();
return $content;
} else {
// log
}
}
else {
throw new \ErrorException('Le token SMS Mailjet n\'est pas défini.');
}
}
}

return false;
}

}
}

Carregando…
Cancelar
Salvar