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.

SmsFactorNotification.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Lc\CaracoleBundle\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. $this->client = $client;
  26. $this->parameterBag = $parameterBag;
  27. $this->mailMailjetNotification = $mailMailjetNotification ;
  28. $this->stringComponent = $stringComponent ;
  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->stringComponent->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->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. }
  59. else {
  60. $token = $this->parameterBag->get('smsfactor.token');
  61. $from = $this->parameterBag->get('smsfactor.from');
  62. if ($token && strlen($token) > 0) {
  63. $response = $this->client->request(
  64. 'GET',
  65. 'https://api.smsfactor.com/send',
  66. [
  67. 'headers' => [
  68. 'Authorization' => 'Bearer ' . $token,
  69. 'Content-Type' => 'application/json; charset=utf-8',
  70. 'Accept' => 'application/json'
  71. ],
  72. 'query' => [
  73. 'sender' => $from,
  74. 'to' => $phone,
  75. 'text' => $message,
  76. ],
  77. ]
  78. );
  79. return $response ;
  80. }
  81. else {
  82. throw new \ErrorException('Le token SMS SmsFactor n\'est pas défini.');
  83. }
  84. }
  85. }
  86. return false;
  87. }
  88. }