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.

102 linhas
3.7KB

  1. <?php
  2. namespace Lc\SovBundle\Notification;
  3. use Lc\SovBundle\Component\StringComponent;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. use Symfony\Contracts\HttpClient\HttpClientInterface;
  6. use Twig\Environment;
  7. class SmsFactorNotification
  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 HttpClientInterface $client;
  14. protected ParameterBagInterface $parameterBag;
  15. protected MailMailjetNotification $mailMailjetNotification;
  16. protected StringComponent $stringComponent;
  17. protected Environment $templating;
  18. public function __construct(
  19. HttpClientInterface $client,
  20. ParameterBagInterface $parameterBag,
  21. MailMailjetNotification $mailMailjetNotification,
  22. StringComponent $stringComponent,
  23. Environment $templating
  24. )
  25. {
  26. $this->client = $client;
  27. $this->parameterBag = $parameterBag;
  28. $this->mailMailjetNotification = $mailMailjetNotification;
  29. $this->stringComponent = $stringComponent;
  30. $this->templating = $templating;
  31. }
  32. public function send($params = [])
  33. {
  34. $user = isset($params[self::TO_USER]) ? $params[self::TO_USER] : null;
  35. if ($user) {
  36. $phone = $this->stringComponent->formatPhoneNumber($user->getPhone());
  37. $message = '';
  38. if (isset($params[self::CONTENT_MESSAGE])) {
  39. $message = $params[self::CONTENT_MESSAGE];
  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->mailMailjetNotification->send([
  50. MailMailjetNotification::SUBJECT => 'Notification par SMS à ' . $phone,
  51. MailMailjetNotification::TO_EMAIL => $user->getEmail(),
  52. MailMailjetNotification::CONTENT_TEMPLATE => 'mail/notification',
  53. MailMailjetNotification::CONTENT_DATA => [
  54. 'message' => $message
  55. ],
  56. ]);
  57. return true;
  58. } else {
  59. $token = $this->parameterBag->get('smsfactor.token');
  60. $from = $this->parameterBag->get('smsfactor.from');
  61. if ($token && strlen($token) > 0) {
  62. $response = $this->client->request(
  63. 'GET',
  64. 'https://api.smsfactor.com/send',
  65. [
  66. 'headers' => [
  67. 'Authorization' => 'Bearer ' . $token,
  68. 'Content-Type' => 'application/json; charset=utf-8',
  69. 'Accept' => 'application/json'
  70. ],
  71. 'query' => [
  72. 'sender' => $from,
  73. 'to' => $phone,
  74. 'text' => $message,
  75. ],
  76. ]
  77. );
  78. return $response;
  79. } else {
  80. throw new \ErrorException('Le token SMS SmsFactor n\'est pas défini.');
  81. }
  82. }
  83. }
  84. return false;
  85. }
  86. }