Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

101 linhas
4.4KB

  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. return $response ;
  78. }
  79. else {
  80. throw new \ErrorException('Le token SMS Mailjet n\'est pas défini.');
  81. }
  82. }
  83. }
  84. return false;
  85. }
  86. }