Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MailjetSmsUtils.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. use Twig\Environment;
  7. class MailjetSmsUtils
  8. {
  9. const TO_USER = 'to-user' ;
  10. const CONTENT_MESSAGE = 'content-message' ;
  11. const CONTENT_TEMPLATE = 'content-template' ;
  12. const CONTENT_DATA = 'content-data' ;
  13. protected $client;
  14. protected $parameterBag;
  15. protected $mailUtils ;
  16. protected $utils ;
  17. protected $templating ;
  18. public function __construct(
  19. HttpClientInterface $client,
  20. ParameterBagInterface $parameterBag,
  21. MailUtils $mailUtils,
  22. Utils $utils,
  23. Environment $templating
  24. ) {
  25. $this->client = $client;
  26. $this->parameterBag = $parameterBag;
  27. $this->mailUtils = $mailUtils ;
  28. $this->utils = $utils ;
  29. $this->templating = $templating ;
  30. }
  31. public function send($params = [])
  32. {
  33. $user = isset($params[self::TO_USER]) ? $params[self::TO_USER] : null ;
  34. if($user) {
  35. $phone = $this->utils->formatPhoneNumber($user->getPhone()) ;
  36. $message = '' ;
  37. if(isset($params[self::CONTENT_MESSAGE])) {
  38. $message = $params[self::CONTENT_MESSAGE] ;
  39. }
  40. elseif(isset($params[self::CONTENT_TEMPLATE])) {
  41. $template = $params[self::CONTENT_TEMPLATE] ;
  42. $paramsTemplate = [] ;
  43. if(isset($params[self::CONTENT_DATA]) && is_array($params[self::CONTENT_DATA])) {
  44. $paramsTemplate = $params[self::CONTENT_DATA] ;
  45. }
  46. $message = $this->templating->render($template, $paramsTemplate) ;
  47. }
  48. if($this->parameterBag->get('mailjet.dev.redirect.active') == 1) {
  49. $this->mailUtils->send([
  50. MailUtils::SUBJECT => 'Notification par SMS à '.$phone,
  51. MailUtils::TO_EMAIL => $user->getEmail(),
  52. MailUtils::CONTENT_TEMPLATE => 'mail/notification',
  53. MailUtils::CONTENT_DATA => [
  54. 'message' => $message
  55. ],
  56. ]);
  57. return true ;
  58. }
  59. else {
  60. $token = $this->parameterBag->get('mailjet.sms.token');
  61. $from = $this->parameterBag->get('mailjet.sms.from');
  62. if ($token && strlen($token) > 0) {
  63. $response = $this->client->request(
  64. 'POST',
  65. 'https://api.mailjet.com/v4/sms-send',
  66. [
  67. 'headers' => [
  68. 'Authorization' => 'Bearer ' . $token,
  69. ],
  70. 'json' => [
  71. 'From' => $from,
  72. 'To' => $phone,
  73. 'Text' => $message,
  74. ]
  75. ]
  76. );
  77. $statusCode = $response->getStatusCode();
  78. if ($statusCode == 200) {
  79. $content = $response->getContent();
  80. $content = $response->toArray();
  81. return $content;
  82. } else {
  83. // log
  84. }
  85. }
  86. else {
  87. throw new \ErrorException('Le token SMS Mailjet n\'est pas défini.');
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. }