Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

MailjetSMS.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. class MailjetSMS
  7. {
  8. protected $client;
  9. protected $parameterBag;
  10. protected $mailUtils ;
  11. protected $utils ;
  12. public function __construct(HttpClientInterface $client, ParameterBagInterface $parameterBag, UtilsManager $utilsManager)
  13. {
  14. $this->client = $client;
  15. $this->parameterBag = $parameterBag;
  16. $this->mailUtils = $utilsManager->getMailUtils() ;
  17. $this->utils = $utilsManager->getUtils() ;
  18. }
  19. public function send($user, $message)
  20. {
  21. if($user) {
  22. $phone = $this->utils->formatPhoneNumber($user->getPhone()) ;
  23. if($this->parameterBag->get('mailjet.dev.redirect.active') == 1) {
  24. $this->mailUtils->send([
  25. MailUtils::SUBJECT => 'Notification par SMS à '.$phone,
  26. MailUtils::TO_EMAIL => $user->getEmail(),
  27. MailUtils::CONTENT_TEMPLATE => 'mail/notification',
  28. MailUtils::CONTENT_DATA => [
  29. 'message' => $message
  30. ],
  31. ]);
  32. return true ;
  33. }
  34. else {
  35. $token = $this->parameterBag->get('mailjet.sms.token');
  36. $from = $this->parameterBag->get('mailjet.sms.from');
  37. if ($token && strlen($token) > 0) {
  38. $response = $this->client->request(
  39. 'POST',
  40. 'https://api.mailjet.com/v4/sms-send',
  41. [
  42. 'headers' => [
  43. 'Authorization' => 'Bearer ' . $token,
  44. ],
  45. 'json' => [
  46. 'From' => $from,
  47. 'To' => $phone,
  48. 'Text' => $message,
  49. ]
  50. ]
  51. );
  52. $statusCode = $response->getStatusCode();
  53. if ($statusCode == 200) {
  54. $content = $response->getContent();
  55. $content = $response->toArray();
  56. return $content;
  57. } else {
  58. // log
  59. }
  60. }
  61. else {
  62. throw new \ErrorException('Le token SMS Mailjet n\'est pas défini.');
  63. }
  64. }
  65. }
  66. return false;
  67. }
  68. }