AbstractTransport.php 2.9 KB

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