• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CPS-IT / mailqueue / 20021782204

08 Dec 2025 08:36AM UTC coverage: 16.692% (+16.7%) from 0.0%
20021782204

push

github

web-flow
Merge pull request #185 from CPS-IT/task/tests

109 of 653 relevant lines covered (16.69%)

0.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Classes/Controller/MailqueueModuleController.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "mailqueue".
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
namespace CPSIT\Typo3Mailqueue\Controller;
19

20
use CPSIT\Typo3Mailqueue\Configuration;
21
use CPSIT\Typo3Mailqueue\Enums;
22
use CPSIT\Typo3Mailqueue\Exception;
23
use CPSIT\Typo3Mailqueue\Mail;
24
use CPSIT\Typo3Mailqueue\Traits;
25
use Psr\Http\Message;
26
use Symfony\Component\Mailer;
27
use TYPO3\CMS\Backend;
28
use TYPO3\CMS\Core;
29

30
/**
31
 * MailqueueModuleController
32
 *
33
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
34
 * @license GPL-2.0-or-later
35
 */
36
#[Backend\Attribute\AsController]
37
final class MailqueueModuleController
38
{
39
    use Traits\TranslatableTrait;
40
    use Core\Http\AllowedMethodsTrait;
41

42
    public function __construct(
×
43
        private readonly Configuration\ExtensionConfiguration $extensionConfiguration,
44
        private readonly Core\Imaging\IconFactory $iconFactory,
45
        private readonly Backend\Template\ModuleTemplateFactory $moduleTemplateFactory,
46
        private readonly Core\Mail\Mailer $mailer,
47
        private readonly Backend\Routing\UriBuilder $uriBuilder,
48
        private readonly Core\Context\Context $context,
49
    ) {}
×
50

51
    public function __invoke(Message\ServerRequestInterface $request): Message\ResponseInterface
×
52
    {
53
        $page = $this->resolvePageIdFromRequest($request);
×
54

55
        // Force redirect when page selector was used
56
        if ($request->getMethod() === 'POST' && !isset($request->getQueryParams()['page'])) {
×
57
            return new Core\Http\RedirectResponse(
×
58
                $this->uriBuilder->buildUriFromRoute('system_mailqueue', ['page' => $page]),
×
59
            );
×
60
        }
61

62
        $this->assertAllowedHttpMethod($request, 'GET');
×
63

64
        $template = $this->moduleTemplateFactory->create($request);
×
65
        $transport = $this->mailer->getTransport();
×
66
        /** @var string|null $sendId */
67
        $sendId = $request->getQueryParams()['send'] ?? null;
×
68
        /** @var string|null $deleteId */
69
        $deleteId = $request->getQueryParams()['delete'] ?? null;
×
70

71
        if ($transport instanceof Mail\Transport\QueueableTransport) {
×
72
            $templateVariables = $this->resolveTemplateVariables($transport, $page, $sendId, $deleteId);
×
73
        } else {
74
            $templateVariables = [
×
75
                'unsupportedTransport' => $this->getTransportFromMailConfiguration(),
×
76
            ];
×
77
        }
78

79
        if (Core\Utility\ExtensionManagementUtility::isLoaded('lowlevel')) {
×
80
            $this->addLinkToConfigurationModule($template);
×
81
        }
82

83
        return $template
×
84
            ->assignMultiple($templateVariables)
×
85
            ->renderResponse('List')
×
86
        ;
×
87
    }
88

89
    private function resolvePageIdFromRequest(Message\ServerRequestInterface $request): int
×
90
    {
91
        $pageId = $request->getQueryParams()['page'] ?? null;
×
92

93
        if (\is_numeric($pageId)) {
×
94
            return (int)$pageId;
×
95
        }
96

97
        if (!\is_array($request->getParsedBody())) {
×
98
            return 1;
×
99
        }
100

101
        $pageId = $request->getParsedBody()['page'] ?? 1;
×
102

103
        if (\is_numeric($pageId)) {
×
104
            return (int)$pageId;
×
105
        }
106

107
        return 1;
×
108
    }
109

110
    /**
111
     * @return array{
112
     *     delayThreshold: positive-int,
113
     *     deleteResult: bool,
114
     *     failing: bool,
115
     *     longestPendingInterval: non-negative-int,
116
     *     pagination: Core\Pagination\SimplePagination,
117
     *     paginator: Core\Pagination\ArrayPaginator,
118
     *     queue: list<Mail\Queue\MailQueueItem>,
119
     *     sendResult: Enums\MailState|null,
120
     * }
121
     */
122
    private function resolveTemplateVariables(
×
123
        Mail\Transport\QueueableTransport $transport,
124
        int $currentPageNumber = 1,
125
        ?string $sendId = null,
126
        ?string $deleteId = null,
127
    ): array {
128
        $failing = false;
×
129
        $longestPendingInterval = 0;
×
130
        /** @var int $now */
131
        $now = $this->context->getPropertyFromAspect('date', 'timestamp');
×
132
        $sendResult = null;
×
133
        $deleteResult = false;
×
134

135
        if (is_string($sendId)) {
×
136
            $sendResult = $this->sendMail($transport, $sendId);
×
137
        }
138

139
        if (is_string($deleteId)) {
×
140
            $deleteResult = $this->deleteMail($transport, $deleteId);
×
141
        }
142

143
        foreach ($transport->getMailQueue() as $mailQueueItem) {
×
144
            if ($mailQueueItem->date !== null) {
×
145
                $longestPendingInterval = max($longestPendingInterval, $now - $mailQueueItem->date->getTimestamp());
×
146
            }
147

148
            if ($mailQueueItem->state === Enums\MailState::Failed) {
×
149
                $failing = true;
×
150
            }
151
        }
152

153
        $queue = $transport->getMailQueue()->get();
×
154
        $paginator = new Core\Pagination\ArrayPaginator(
×
155
            $queue,
×
156
            $currentPageNumber,
×
157
            $this->extensionConfiguration->itemsPerPage,
×
158
        );
×
159
        $pagination = new Core\Pagination\SimplePagination($paginator);
×
160

161
        return [
×
162
            'delayThreshold' => $this->extensionConfiguration->queueDelayThreshold,
×
163
            'deleteResult' => $deleteResult,
×
164
            'failing' => $failing,
×
165
            'longestPendingInterval' => $longestPendingInterval,
×
166
            'pagination' => $pagination,
×
167
            'paginator' => $paginator,
×
168
            'queue' => $queue,
×
169
            'sendResult' => $sendResult,
×
170
        ];
×
171
    }
172

173
    private function sendMail(Mail\Transport\QueueableTransport $transport, string $queueItemId): Enums\MailState
×
174
    {
175
        $mailQueueItem = $this->getMailById($transport, $queueItemId);
×
176

177
        if ($mailQueueItem === null) {
×
178
            return Enums\MailState::AlreadySent;
×
179
        }
180

181
        try {
182
            $transport->dequeue($mailQueueItem, $this->mailer->getRealTransport());
×
183
        } catch (Mailer\Exception\TransportExceptionInterface) {
×
184
            return Enums\MailState::Failed;
×
185
        }
186

187
        return Enums\MailState::Sent;
×
188
    }
189

190
    private function deleteMail(Mail\Transport\QueueableTransport $transport, string $queueItemId): bool
×
191
    {
192
        $mailQueueItem = $this->getMailById($transport, $queueItemId);
×
193

194
        if ($mailQueueItem === null) {
×
195
            return false;
×
196
        }
197

198
        return $transport->delete($mailQueueItem);
×
199
    }
200

201
    private function getMailById(
×
202
        Mail\Transport\QueueableTransport $transport,
203
        string $queueItemId,
204
    ): ?Mail\Queue\MailQueueItem {
205
        foreach ($transport->getMailQueue() as $mailQueueItem) {
×
206
            if ($mailQueueItem->id === $queueItemId) {
×
207
                return $mailQueueItem;
×
208
            }
209
        }
210

211
        return null;
×
212
    }
213

214
    private function addLinkToConfigurationModule(Backend\Template\ModuleTemplate $template): void
×
215
    {
216
        $buttonBar = $template->getDocHeaderComponent()->getButtonBar();
×
217

218
        $button = $buttonBar->makeLinkButton();
×
219
        $button->setHref(
×
220
            (string)$this->uriBuilder->buildUriFromRoute(
×
221
                'system_config',
×
222
                ['node' => ['MAIL' => 1], 'tree' => 'confVars'],
×
223
            )
×
224
        );
×
225
        $button->setIcon($this->iconFactory->getIcon('actions-cog', Core\Imaging\Icon::SIZE_SMALL));
×
226
        $button->setTitle(self::translate('button.config'));
×
227
        $button->setShowLabelText(true);
×
228

229
        $buttonBar->addButton($button);
×
230
    }
231

232
    /**
233
     * @throws Exception\MailTransportIsNotConfigured
234
     */
235
    private function getTransportFromMailConfiguration(): string
×
236
    {
237
        $mailConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL'] ?? null;
×
238

239
        if (!\is_array($mailConfiguration)) {
×
240
            throw new Exception\MailTransportIsNotConfigured();
×
241
        }
242

243
        $spoolType = $mailConfiguration['transport_spool_type'] ?? null;
×
244

245
        if (is_string($spoolType) && trim($spoolType) !== '') {
×
246
            return $spoolType;
×
247
        }
248

249
        $transport = $mailConfiguration['transport'] ?? null;
×
250

251
        if (is_string($transport) && trim($transport) !== '') {
×
252
            return $transport;
×
253
        }
254

255
        throw new Exception\MailTransportIsNotConfigured();
×
256
    }
257
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc