No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

82 líneas
3.6KB

  1. <?php
  2. namespace Lc\ShopBundle\Services;
  3. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  4. use Symfony\Component\HttpFoundation\ParameterBag;
  5. use Symfony\Contracts\HttpClient\HttpClientInterface;
  6. class MailjetSMS
  7. {
  8. protected $client;
  9. protected $parameterBag;
  10. protected $mailUtils ;
  11. protected $utils ;
  12. public function __construct(HttpClientInterface $client, ParameterBagInterface $parameterBag, UtilsManager $utilsManager)
  13. {
  14. $this->client = $client;
  15. $this->parameterBag = $parameterBag;
  16. $this->mailUtils = $utilsManager->getMailUtils() ;
  17. $this->utils = $utilsManager->getUtils() ;
  18. }
  19. public function send($user, $message)
  20. {
  21. if($user) {
  22. $phone = $this->utils->formatPhoneNumber($user->getPhone()) ;
  23. if($this->parameterBag->get('mailjet.dev.redirect.active') == 1) {
  24. $this->mailUtils->send([
  25. MailUtils::SUBJECT => 'Notification par SMS à '.$phone,
  26. MailUtils::TO_EMAIL => $user->getEmail(),
  27. MailUtils::CONTENT_TEMPLATE => 'mail/notification',
  28. MailUtils::CONTENT_DATA => [
  29. 'message' => $message
  30. ],
  31. ]);
  32. return true ;
  33. }
  34. else {
  35. $token = $this->parameterBag->get('mailjet.sms.token');
  36. $from = $this->parameterBag->get('mailjet.sms.from');
  37. if ($token && strlen($token) > 0) {
  38. $response = $this->client->request(
  39. 'POST',
  40. 'https://api.mailjet.com/v4/sms-send',
  41. [
  42. 'headers' => [
  43. 'Authorization' => 'Bearer ' . $token,
  44. ],
  45. 'json' => [
  46. 'From' => $from,
  47. 'To' => $phone,
  48. 'Text' => $message,
  49. ]
  50. ]
  51. );
  52. $statusCode = $response->getStatusCode();
  53. if ($statusCode == 200) {
  54. $content = $response->getContent();
  55. $content = $response->toArray();
  56. return $content;
  57. } else {
  58. // log
  59. }
  60. }
  61. else {
  62. throw new \ErrorException('Le token SMS Mailjet n\'est pas défini.');
  63. }
  64. }
  65. }
  66. return false;
  67. }
  68. }