vendor/symfony/mailer/Transport/AbstractTransport.php line 56

Open in your IDE?
  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\Mailer\Transport;
  11. use Psr\EventDispatcher\EventDispatcherInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Psr\Log\NullLogger;
  14. use Symfony\Component\Mailer\Envelope;
  15. use Symfony\Component\Mailer\Event\MessageEvent;
  16. use Symfony\Component\Mailer\SentMessage;
  17. use Symfony\Component\Mime\Address;
  18. use Symfony\Component\Mime\RawMessage;
  19. /**
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. abstract class AbstractTransport implements TransportInterface
  23. {
  24. private ?EventDispatcherInterface $dispatcher;
  25. private LoggerInterface $logger;
  26. private float $rate = 0;
  27. private float $lastSent = 0;
  28. public function __construct(EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
  29. {
  30. $this->dispatcher = $dispatcher;
  31. $this->logger = $logger ?? new NullLogger();
  32. }
  33. /**
  34. * Sets the maximum number of messages to send per second (0 to disable).
  35. *
  36. * @return $this
  37. */
  38. public function setMaxPerSecond(float $rate): static
  39. {
  40. if (0 >= $rate) {
  41. $rate = 0;
  42. }
  43. $this->rate = $rate;
  44. $this->lastSent = 0;
  45. return $this;
  46. }
  47. public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
  48. {
  49. $message = clone $message;
  50. $envelope = null !== $envelope ? clone $envelope : Envelope::create($message);
  51. if (null !== $this->dispatcher) {
  52. $event = new MessageEvent($message, $envelope, (string) $this);
  53. $this->dispatcher->dispatch($event);
  54. $envelope = $event->getEnvelope();
  55. $message = $event->getMessage();
  56. }
  57. $message = new SentMessage($message, $envelope);
  58. $this->doSend($message);
  59. $this->checkThrottling();
  60. return $message;
  61. }
  62. abstract protected function doSend(SentMessage $message): void;
  63. /**
  64. * @param Address[] $addresses
  65. *
  66. * @return string[]
  67. */
  68. protected function stringifyAddresses(array $addresses): array
  69. {
  70. return array_map(function (Address $a) {
  71. return $a->toString();
  72. }, $addresses);
  73. }
  74. protected function getLogger(): LoggerInterface
  75. {
  76. return $this->logger;
  77. }
  78. private function checkThrottling()
  79. {
  80. if (0 == $this->rate) {
  81. return;
  82. }
  83. $sleep = (1 / $this->rate) - (microtime(true) - $this->lastSent);
  84. if (0 < $sleep) {
  85. $this->logger->debug(sprintf('Email transport "%s" sleeps for %.2f seconds', __CLASS__, $sleep));
  86. usleep($sleep * 1000000);
  87. }
  88. $this->lastSent = microtime(true);
  89. }
  90. }