[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Mail / MemorySpool.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Mail;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
20 use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
21 use Symfony\Component\Mailer\SentMessage;
22 use Symfony\Component\Mailer\Transport\AbstractTransport;
23 use Symfony\Component\Mailer\Transport\TransportInterface;
24 use TYPO3\CMS\Core\SingletonInterface;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 /**
28 * Because TYPO3 doesn't offer a terminate signal or hook,
29 * and taking in account the risk that extensions do some redirects or even exit,
30 * we simply use the destructor of a singleton class which should be pretty much
31 * at the end of a request.
32 *
33 * To have only one memory spool per request seems to be more appropriate anyway.
34 *
35 * @internal This class is experimental and subject to change!
36 */
37 class MemorySpool extends AbstractTransport implements SingletonInterface, LoggerAwareInterface, DelayedTransportInterface
38 {
39 use LoggerAwareTrait;
40
41 /**
42 * @var SentMessage[]
43 */
44 protected $queuedMessages;
45
46 /**
47 * Maximum number of retries when the real transport has failed.
48 *
49 * @var int
50 */
51 protected $retries = 3;
52
53 /**
54 * Sends out the messages in the memory
55 */
56 public function __destruct()
57 {
58 $mailer = GeneralUtility::makeInstance(Mailer::class);
59 try {
60 $this->flushQueue($mailer->getRealTransport());
61 } catch (TransportExceptionInterface $exception) {
62 $this->logger->error('An Exception occurred while flushing email queue: ' . $exception->getMessage());
63 }
64 }
65
66 /**
67 * @inheritdoc
68 */
69 public function flushQueue(TransportInterface $transport): int
70 {
71 if (!$this->queuedMessages) {
72 return 0;
73 }
74
75 $retries = $this->retries;
76 $message = null;
77 $count = 0;
78 while ($retries--) {
79 try {
80 while ($message = array_pop($this->queuedMessages)) {
81 $transport->send($message->getMessage(), $message->getEnvelope());
82 $count++;
83 }
84 } catch (TransportExceptionInterface $exception) {
85 if ($retries && $message) {
86 // re-queue the message at the end of the queue to give a chance
87 // to the other messages to be sent, in case the failure was due to
88 // this message and not just the transport failing
89 array_unshift($this->queuedMessages, $message);
90
91 // wait half a second before we try again
92 usleep(500000);
93 } else {
94 throw $exception;
95 }
96 }
97 }
98 return $count;
99 }
100
101 /**
102 * Stores a message in the queue.
103 * @param SentMessage $message
104 */
105 protected function doSend(SentMessage $message): void
106 {
107 $this->queuedMessages[] = $message;
108 }
109 }