SmsMessage.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Message;
  11. use Symfony\Component\Notifier\Exception\LogicException;
  12. use Symfony\Component\Notifier\Notification\Notification;
  13. use Symfony\Component\Notifier\Notification\SmsNotificationInterface;
  14. use Symfony\Component\Notifier\Recipient\Recipient;
  15. use Symfony\Component\Notifier\Recipient\SmsRecipientInterface;
  16. /**
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. *
  19. * @experimental in 5.1
  20. */
  21. final class SmsMessage implements MessageInterface
  22. {
  23. private $transport;
  24. private $subject;
  25. private $phone;
  26. public function __construct(string $phone, string $subject)
  27. {
  28. $this->subject = $subject;
  29. $this->phone = $phone;
  30. }
  31. public static function fromNotification(Notification $notification, Recipient $recipient): self
  32. {
  33. if (!$recipient instanceof SmsRecipientInterface) {
  34. throw new LogicException(sprintf('To send a SMS message, "%s" should implement "%s" or the recipient should implement "%s".', get_debug_type($notification), SmsNotificationInterface::class, SmsRecipientInterface::class));
  35. }
  36. return new self($recipient->getPhone(), $notification->getSubject());
  37. }
  38. /**
  39. * @return $this
  40. */
  41. public function phone(string $phone): self
  42. {
  43. $this->phone = $phone;
  44. return $this;
  45. }
  46. public function getPhone(): string
  47. {
  48. return $this->phone;
  49. }
  50. public function getRecipientId(): string
  51. {
  52. return $this->phone;
  53. }
  54. /**
  55. * @return $this
  56. */
  57. public function subject(string $subject): self
  58. {
  59. $this->subject = $subject;
  60. return $this;
  61. }
  62. public function getSubject(): string
  63. {
  64. return $this->subject;
  65. }
  66. /**
  67. * @return $this
  68. */
  69. public function transport(string $transport): self
  70. {
  71. $this->transport = $transport;
  72. return $this;
  73. }
  74. public function getTransport(): ?string
  75. {
  76. return $this->transport;
  77. }
  78. public function getOptions(): ?MessageOptionsInterface
  79. {
  80. return null;
  81. }
  82. }