|
- <?php
-
- namespace Lc\ShopBundle\Services ;
-
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\HttpFoundation\ParameterBag;
- use Symfony\Contracts\HttpClient\HttpClientInterface;
-
- class MailjetSMS
- {
- protected $client ;
- protected $parameterBag ;
-
- public function __construct(HttpClientInterface $client, ParameterBagInterface $parameterBag)
- {
- $this->client = $client ;
- $this->parameterBag = $parameterBag ;
- }
-
- public function send($to, $message)
- {
- $token = $this->parameterBag->get('mailjet.token_sms') ;
-
- if($token && strlen($token) > 0) {
- $response = $this->client->request(
- 'POST',
- 'https://api.mailjet.com/v4/sms-send',
- [
- 'headers' => [
- 'Authorization' => 'Bearer '.$token,
- 'Content-Type' => 'application/json',
- ],
- 'body' => [
- 'Text' => $message,
- 'To' => $to,
- 'From' => 'MJPilot'
- ]
- ]
- );
-
- $statusCode = $response->getStatusCode();
-
- if($statusCode == 200) {
- $content = $response->getContent();
- $content = $response->toArray();
- return $content ;
- }
- else {
- // log
- }
-
- return false ;
- }
- else {
- throw new \ErrorException('Le token SMS Mailjet n\'est pas défini.') ;
- }
-
- }
- }
|