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.

60 lines
1.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. class MailjetSMS
  7. {
  8. protected $client ;
  9. protected $parameterBag ;
  10. public function __construct(HttpClientInterface $client, ParameterBagInterface $parameterBag)
  11. {
  12. $this->client = $client ;
  13. $this->parameterBag = $parameterBag ;
  14. }
  15. public function send($to, $message)
  16. {
  17. $token = $this->parameterBag->get('mailjet.token_sms') ;
  18. if($token && strlen($token) > 0) {
  19. $response = $this->client->request(
  20. 'POST',
  21. 'https://api.mailjet.com/v4/sms-send',
  22. [
  23. 'headers' => [
  24. 'Authorization' => 'Bearer '.$token,
  25. 'Content-Type' => 'application/json',
  26. ],
  27. 'body' => [
  28. 'Text' => $message,
  29. 'To' => $to,
  30. 'From' => 'MJPilot'
  31. ]
  32. ]
  33. );
  34. $statusCode = $response->getStatusCode();
  35. if($statusCode == 200) {
  36. $content = $response->getContent();
  37. $content = $response->toArray();
  38. return $content ;
  39. }
  40. else {
  41. // log
  42. }
  43. return false ;
  44. }
  45. else {
  46. throw new \ErrorException('Le token SMS Mailjet n\'est pas défini.') ;
  47. }
  48. }
  49. }