PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Notifier/Bridge/Octopush/OctopushTransport.php

https://github.com/FabienD/symfony
PHP | 97 lines | 68 code | 18 blank | 11 comment | 2 complexity | a596bf0d127919776360d66e0d53ea55 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Notifier\Bridge\Octopush;
  11. use Symfony\Component\Notifier\Exception\TransportException;
  12. use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
  13. use Symfony\Component\Notifier\Message\MessageInterface;
  14. use Symfony\Component\Notifier\Message\SentMessage;
  15. use Symfony\Component\Notifier\Message\SmsMessage;
  16. use Symfony\Component\Notifier\Transport\AbstractTransport;
  17. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  19. use Symfony\Contracts\HttpClient\HttpClientInterface;
  20. /**
  21. * @author Aurélien Martin <pro@aurelienmartin.com>
  22. */
  23. final class OctopushTransport extends AbstractTransport
  24. {
  25. protected const HOST = 'www.octopush-dm.com';
  26. private string $userLogin;
  27. private string $apiKey;
  28. private string $from;
  29. private string $type;
  30. public function __construct(string $userLogin, string $apiKey, string $from, string $type, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
  31. {
  32. $this->userLogin = $userLogin;
  33. $this->apiKey = $apiKey;
  34. $this->from = $from;
  35. $this->type = $type;
  36. parent::__construct($client, $dispatcher);
  37. }
  38. public function __toString(): string
  39. {
  40. return sprintf('octopush://%s?from=%s&type=%s', $this->getEndpoint(), $this->from, $this->type);
  41. }
  42. public function supports(MessageInterface $message): bool
  43. {
  44. return $message instanceof SmsMessage;
  45. }
  46. protected function doSend(MessageInterface $message): SentMessage
  47. {
  48. if (!$message instanceof SmsMessage) {
  49. throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
  50. }
  51. $endpoint = sprintf('https://%s/api/sms/json', $this->getEndpoint());
  52. $response = $this->client->request('POST', $endpoint, [
  53. 'headers' => [
  54. 'content_type' => 'multipart/form-data',
  55. ],
  56. 'body' => [
  57. 'user_login' => $this->userLogin,
  58. 'api_key' => $this->apiKey,
  59. 'sms_text' => $message->getSubject(),
  60. 'sms_recipients' => $message->getPhone(),
  61. 'sms_sender' => $this->from,
  62. 'sms_type' => $this->type,
  63. ],
  64. ]);
  65. try {
  66. $statusCode = $response->getStatusCode();
  67. } catch (TransportExceptionInterface $e) {
  68. throw new TransportException('Could not reach the remote Octopush server.', $response, 0, $e);
  69. }
  70. if (200 !== $statusCode) {
  71. $error = $response->toArray(false);
  72. throw new TransportException('Unable to send the SMS: '.$error['error_code'], $response);
  73. }
  74. $success = $response->toArray(false);
  75. $sentMessage = new SentMessage($message, (string) $this);
  76. $sentMessage->setMessageId($success['ticket']);
  77. return $sentMessage;
  78. }
  79. }