You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
4.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. use Twig\Environment;
  7. class SmsFactorUtils
  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('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. }