BatchMessage.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * Copyright (C) 2013 Mailgun
  5. *
  6. * This software may be modified and distributed under the terms
  7. * of the MIT license. See the LICENSE file for details.
  8. */
  9. namespace Mailgun\Message;
  10. use Mailgun\Api\Message;
  11. use Mailgun\Message\Exceptions\MissingRequiredParameter;
  12. use Mailgun\Message\Exceptions\RuntimeException;
  13. use Mailgun\Message\Exceptions\TooManyRecipients;
  14. use Psr\Http\Client\ClientExceptionInterface;
  15. /**
  16. * This class is used for batch sending. See the official documentation (link below)
  17. * for usage instructions.
  18. *
  19. * @see https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Message/README.md
  20. */
  21. class BatchMessage extends MessageBuilder
  22. {
  23. /**
  24. * @var array
  25. */
  26. private array $batchRecipientAttributes = [];
  27. /**
  28. * @var bool
  29. */
  30. private bool $autoSend;
  31. /**
  32. * @var array
  33. */
  34. private array $messageIds = [];
  35. /**
  36. * @var string
  37. */
  38. private string $domain;
  39. /**
  40. * @var Message
  41. */
  42. private Message $api;
  43. /**
  44. * @param Message $messageApi
  45. * @param string $domain
  46. * @param bool $autoSend
  47. */
  48. public function __construct(Message $messageApi, string $domain, bool $autoSend)
  49. {
  50. $this->api = $messageApi;
  51. $this->domain = $domain;
  52. $this->autoSend = $autoSend;
  53. }
  54. /**
  55. * @param string $headerName
  56. * @param string $address
  57. * @param array $variables {
  58. * id?:string
  59. * full_name?: string,
  60. * first?: string,
  61. * last?: string,
  62. * @return MessageBuilder
  63. * @throws MissingRequiredParameter
  64. * @throws TooManyRecipients|ClientExceptionInterface
  65. */
  66. protected function addRecipient(string $headerName, string $address, array $variables): MessageBuilder
  67. {
  68. if (array_key_exists($headerName, $this->counters['recipients'])) {
  69. if (self::RECIPIENT_COUNT_LIMIT === $this->counters['recipients'][$headerName]) {
  70. if (false === $this->autoSend) {
  71. throw TooManyRecipients::whenAutoSendDisabled();
  72. }
  73. $this->finalize();
  74. }
  75. }
  76. parent::addRecipient($headerName, $address, $variables);
  77. if (array_key_exists($headerName, $this->counters['recipients']) && !array_key_exists('id', $variables)) {
  78. $variables['id'] = $headerName.'_'.$this->counters['recipients'][$headerName];
  79. }
  80. if ($variables) {
  81. $this->batchRecipientAttributes[$address] = $variables;
  82. }
  83. return $this;
  84. }
  85. /**
  86. * @throws RuntimeException
  87. * @throws MissingRequiredParameter|ClientExceptionInterface
  88. */
  89. public function finalize(): void
  90. {
  91. $message = $this->message;
  92. if (empty($this->domain)) {
  93. throw new RuntimeException('You must call BatchMessage::setDomain before sending messages.');
  94. }
  95. if (empty($message['from'])) {
  96. throw MissingRequiredParameter::create('from');
  97. }
  98. if (empty($message['to'])) {
  99. throw MissingRequiredParameter::create('to');
  100. }
  101. if (empty($message['subject'])) {
  102. throw MissingRequiredParameter::create('subject');
  103. }
  104. if (empty($message['text']) && empty($message['html']) && empty($message['template'])) {
  105. throw MissingRequiredParameter::create('text", "html" or "template');
  106. }
  107. $message['recipient-variables'] = json_encode($this->batchRecipientAttributes, JSON_FORCE_OBJECT);
  108. $response = $this->api->send($this->domain, $message);
  109. $this->batchRecipientAttributes = [];
  110. $this->counters['recipients']['to'] = 0;
  111. $this->counters['recipients']['cc'] = 0;
  112. $this->counters['recipients']['bcc'] = 0;
  113. unset($this->message['to']);
  114. $this->messageIds[] = $response->getId();
  115. }
  116. /**
  117. * @return string[]
  118. */
  119. public function getMessageIds(): array
  120. {
  121. return $this->messageIds;
  122. }
  123. }