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

CPS-IT / mailqueue / 12913146826

22 Jan 2025 04:55PM UTC coverage: 0.0%. Remained the same
12913146826

Pull #81

github

web-flow
Merge e5e2b937e into 131584b2a
Pull Request #81: [TASK] Enforce HTTP methods in module controller

0 of 1 new or added line in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

0 of 706 relevant lines covered (0.0%)

0.0 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
 * Copyright (C) 2024 Elias Häußler <e.haeussler@familie-redlich.de>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace CPSIT\Typo3Mailqueue\Controller;
25

26
use CPSIT\Typo3Mailqueue\Configuration;
27
use CPSIT\Typo3Mailqueue\Enums;
28
use CPSIT\Typo3Mailqueue\Exception;
29
use CPSIT\Typo3Mailqueue\Mail;
30
use CPSIT\Typo3Mailqueue\Traits;
31
use Psr\Http\Message;
32
use Symfony\Component\Mailer;
33
use TYPO3\CMS\Backend;
34
use TYPO3\CMS\Core;
35
use TYPO3\CMS\Fluid;
36

37
/**
38
 * MailqueueModuleController
39
 *
40
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
41
 * @license GPL-2.0-or-later
42
 */
43
final class MailqueueModuleController
44
{
45
    use Traits\TranslatableTrait;
46
    use Core\Http\AllowedMethodsTrait;
47

48
    private readonly Core\Information\Typo3Version $typo3Version;
49

50
    public function __construct(
×
51
        private readonly Configuration\ExtensionConfiguration $extensionConfiguration,
52
        private readonly Core\Imaging\IconFactory $iconFactory,
53
        private readonly Backend\Template\ModuleTemplateFactory $moduleTemplateFactory,
54
        private readonly Core\Mail\Mailer $mailer,
55
        private readonly Backend\Routing\UriBuilder $uriBuilder,
56
        private readonly Core\Context\Context $context,
57
    ) {
58
        $this->typo3Version = new Core\Information\Typo3Version();
×
59
    }
60

61
    public function __invoke(Message\ServerRequestInterface $request): Message\ResponseInterface
×
62
    {
NEW
63
        $this->assertAllowedHttpMethod($request, 'GET', 'POST');
×
64
        $template = $this->moduleTemplateFactory->create($request);
×
65
        $transport = $this->mailer->getTransport();
×
66
        $page = $this->resolvePageIdFromRequest($request);
×
67
        $sendId = $request->getQueryParams()['send'] ?? null;
×
68
        $deleteId = $request->getQueryParams()['delete'] ?? null;
×
69

70
        // Force redirect when page selector was used
71
        if ($request->getMethod() === 'POST' && !isset($request->getQueryParams()['page'])) {
×
72
            return new Core\Http\RedirectResponse(
×
73
                $this->uriBuilder->buildUriFromRoute('system_mailqueue', ['page' => $page]),
×
74
            );
×
75
        }
76

77
        if ($transport instanceof Mail\Transport\QueueableTransport) {
×
78
            $templateVariables = $this->resolveTemplateVariables($transport, $page, $sendId, $deleteId);
×
79
        } else {
80
            $templateVariables = [
×
81
                'unsupportedTransport' => $this->getTransportFromMailConfiguration(),
×
82
                'typo3Version' => $this->typo3Version->getMajorVersion(),
×
83
            ];
×
84
        }
85

86
        // @todo Remove once support for TYPO3 v11 is dropped
87
        $templateVariables['layout'] = $this->typo3Version->getMajorVersion() < 12 ? 'Default' : 'Module';
×
88

89
        if (Core\Utility\ExtensionManagementUtility::isLoaded('lowlevel')) {
×
90
            $this->addLinkToConfigurationModule($template);
×
91
        }
92

93
        // @todo Remove once support for TYPO3 v11 is dropped
94
        if ($this->typo3Version->getMajorVersion() < 12) {
×
95
            return $this->renderLegacyTemplate($template, $templateVariables);
×
96
        }
UNCOV
97
        return $template
×
98
            ->assignMultiple($templateVariables)
×
99
            ->renderResponse('List')
×
100
        ;
×
101
    }
102

103
    private function resolvePageIdFromRequest(Message\ServerRequestInterface $request): int
×
104
    {
105
        $pageId = $request->getQueryParams()['page'] ?? null;
×
106

107
        if (\is_numeric($pageId)) {
×
108
            return (int)$pageId;
×
109
        }
110

111
        if (!\is_array($request->getParsedBody())) {
×
112
            return 1;
×
113
        }
114

115
        return (int)($request->getParsedBody()['page'] ?? 1);
×
116
    }
117

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

143
        if (is_string($sendId)) {
×
144
            $sendResult = $this->sendMail($transport, $sendId);
×
145
        }
146

147
        if (is_string($deleteId)) {
×
148
            $deleteResult = $this->deleteMail($transport, $deleteId);
×
149
        }
150

151
        foreach ($transport->getMailQueue() as $mailQueueItem) {
×
152
            if ($mailQueueItem->date !== null) {
×
153
                $longestPendingInterval = max($longestPendingInterval, $now - $mailQueueItem->date->getTimestamp());
×
154
            }
155

156
            if ($mailQueueItem->state === Enums\MailState::Failed) {
×
157
                $failing = true;
×
158
            }
159
        }
160

161
        $queue = $transport->getMailQueue()->get();
×
162
        $paginator = new Core\Pagination\ArrayPaginator(
×
163
            $queue,
×
164
            $currentPageNumber,
×
165
            $this->extensionConfiguration->getItemsPerPage(),
×
166
        );
×
167
        $pagination = new Core\Pagination\SimplePagination($paginator);
×
168

169
        return [
×
170
            'delayThreshold' => $this->extensionConfiguration->getQueueDelayThreshold(),
×
171
            'deleteResult' => $deleteResult,
×
172
            'failing' => $failing,
×
173
            'longestPendingInterval' => $longestPendingInterval,
×
174
            'pagination' => $pagination,
×
175
            'paginator' => $paginator,
×
176
            'queue' => $queue,
×
177
            'sendResult' => $sendResult,
×
178
        ];
×
179
    }
180

181
    private function sendMail(Mail\Transport\QueueableTransport $transport, string $queueItemId): Enums\MailState
×
182
    {
183
        $mailQueueItem = $this->getMailById($transport, $queueItemId);
×
184

185
        if ($mailQueueItem === null) {
×
186
            return Enums\MailState::AlreadySent;
×
187
        }
188

189
        try {
190
            $transport->dequeue($mailQueueItem, $this->mailer->getRealTransport());
×
191
        } catch (Mailer\Exception\TransportExceptionInterface) {
×
192
            return Enums\MailState::Failed;
×
193
        }
194

195
        return Enums\MailState::Sent;
×
196
    }
197

198
    private function deleteMail(Mail\Transport\QueueableTransport $transport, string $queueItemId): bool
×
199
    {
200
        $mailQueueItem = $this->getMailById($transport, $queueItemId);
×
201

202
        if ($mailQueueItem === null) {
×
203
            return false;
×
204
        }
205

206
        return $transport->delete($mailQueueItem);
×
207
    }
208

209
    private function getMailById(
×
210
        Mail\Transport\QueueableTransport $transport,
211
        string $queueItemId,
212
    ): ?Mail\Queue\MailQueueItem {
213
        foreach ($transport->getMailQueue() as $mailQueueItem) {
×
214
            if ($mailQueueItem->id === $queueItemId) {
×
215
                return $mailQueueItem;
×
216
            }
217
        }
218

219
        return null;
×
220
    }
221

222
    private function addLinkToConfigurationModule(Backend\Template\ModuleTemplate $template): void
×
223
    {
224
        $buttonBar = $template->getDocHeaderComponent()->getButtonBar();
×
225

226
        $button = $buttonBar->makeLinkButton();
×
227
        $button->setHref(
×
228
            (string)$this->uriBuilder->buildUriFromRoute(
×
229
                'system_config',
×
230
                ['node' => ['MAIL' => 1], 'tree' => 'confVars'],
×
231
            )
×
232
        );
×
233
        $button->setIcon($this->iconFactory->getIcon('actions-cog', Core\Imaging\Icon::SIZE_SMALL));
×
234
        $button->setTitle(self::translate('button.config'));
×
235
        $button->setShowLabelText(true);
×
236

237
        $buttonBar->addButton($button);
×
238
    }
239

240
    /**
241
     * @throws Exception\MailTransportIsNotConfigured
242
     */
243
    private function getTransportFromMailConfiguration(): string
×
244
    {
245
        $mailConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL'] ?? null;
×
246

247
        if (!\is_array($mailConfiguration)) {
×
248
            throw new Exception\MailTransportIsNotConfigured();
×
249
        }
250

251
        $spoolType = $mailConfiguration['transport_spool_type'] ?? null;
×
252

253
        if (is_string($spoolType) && trim($spoolType) !== '') {
×
254
            return $spoolType;
×
255
        }
256

257
        $transport = $mailConfiguration['transport'] ?? null;
×
258

259
        if (is_string($transport) && trim($transport) !== '') {
×
260
            return $transport;
×
261
        }
262

263
        throw new Exception\MailTransportIsNotConfigured();
×
264
    }
265

266
    /**
267
     * @todo Remove once support for TYPO3 v11 is dropped
268
     *
269
     * @param array<string, mixed> $templateVariables
270
     */
271
    private function renderLegacyTemplate(
×
272
        Backend\Template\ModuleTemplate $template,
273
        array $templateVariables,
274
    ): Core\Http\HtmlResponse {
275
        $view = Core\Utility\GeneralUtility::makeInstance(Fluid\View\StandaloneView::class);
×
276
        $view->setTemplateRootPaths(['EXT:mailqueue/Resources/Private/Templates/']);
×
277
        $view->setPartialRootPaths(['EXT:mailqueue/Resources/Private/Partials/']);
×
278
        $view->setLayoutRootPaths(['EXT:mailqueue/Resources/Private/Layouts/']);
×
279
        $view->setTemplate('List');
×
280
        $view->assignMultiple($templateVariables);
×
281

282
        $template->setContent($view->render());
×
283

284
        return new Core\Http\HtmlResponse($template->renderContent());
×
285
    }
286
}
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