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

CPS-IT / mailqueue / 12903461206

22 Jan 2025 07:39AM UTC coverage: 0.0%. Remained the same
12903461206

Pull #82

github

web-flow
Merge 15cc61044 into 131584b2a
Pull Request #82: [TASK] Update PHPStan packages to v2 (major)

0 of 8 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

0 of 711 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

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

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

60
    public function __invoke(Message\ServerRequestInterface $request): Message\ResponseInterface
×
61
    {
62
        $template = $this->moduleTemplateFactory->create($request);
×
63
        $transport = $this->mailer->getTransport();
×
64
        $page = $this->resolvePageIdFromRequest($request);
×
65
        /** @var string|null $sendId */
66
        $sendId = $request->getQueryParams()['send'] ?? null;
×
67
        /** @var string|null $deleteId */
UNCOV
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
        }
97

98
        return $template
×
99
            ->assignMultiple($templateVariables)
×
100
            ->renderResponse('List')
×
101
        ;
×
102
    }
103

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

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

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

NEW
116
        $pageId = $request->getParsedBody()['page'] ?? 1;
×
117

NEW
118
        if (\is_numeric($pageId)) {
×
NEW
119
            return (int)$pageId;
×
120
        }
121

NEW
122
        return 1;
×
123
    }
124

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

150
        if (is_string($sendId)) {
×
151
            $sendResult = $this->sendMail($transport, $sendId);
×
152
        }
153

154
        if (is_string($deleteId)) {
×
155
            $deleteResult = $this->deleteMail($transport, $deleteId);
×
156
        }
157

158
        foreach ($transport->getMailQueue() as $mailQueueItem) {
×
159
            if ($mailQueueItem->date !== null) {
×
160
                $longestPendingInterval = max($longestPendingInterval, $now - $mailQueueItem->date->getTimestamp());
×
161
            }
162

163
            if ($mailQueueItem->state === Enums\MailState::Failed) {
×
164
                $failing = true;
×
165
            }
166
        }
167

168
        $queue = $transport->getMailQueue()->get();
×
169
        $paginator = new Core\Pagination\ArrayPaginator(
×
170
            $queue,
×
171
            $currentPageNumber,
×
172
            $this->extensionConfiguration->getItemsPerPage(),
×
173
        );
×
174
        $pagination = new Core\Pagination\SimplePagination($paginator);
×
175

176
        return [
×
177
            'delayThreshold' => $this->extensionConfiguration->getQueueDelayThreshold(),
×
178
            'deleteResult' => $deleteResult,
×
179
            'failing' => $failing,
×
180
            'longestPendingInterval' => $longestPendingInterval,
×
181
            'pagination' => $pagination,
×
182
            'paginator' => $paginator,
×
183
            'queue' => $queue,
×
184
            'sendResult' => $sendResult,
×
185
        ];
×
186
    }
187

188
    private function sendMail(Mail\Transport\QueueableTransport $transport, string $queueItemId): Enums\MailState
×
189
    {
190
        $mailQueueItem = $this->getMailById($transport, $queueItemId);
×
191

192
        if ($mailQueueItem === null) {
×
193
            return Enums\MailState::AlreadySent;
×
194
        }
195

196
        try {
197
            $transport->dequeue($mailQueueItem, $this->mailer->getRealTransport());
×
198
        } catch (Mailer\Exception\TransportExceptionInterface) {
×
199
            return Enums\MailState::Failed;
×
200
        }
201

202
        return Enums\MailState::Sent;
×
203
    }
204

205
    private function deleteMail(Mail\Transport\QueueableTransport $transport, string $queueItemId): bool
×
206
    {
207
        $mailQueueItem = $this->getMailById($transport, $queueItemId);
×
208

209
        if ($mailQueueItem === null) {
×
210
            return false;
×
211
        }
212

213
        return $transport->delete($mailQueueItem);
×
214
    }
215

216
    private function getMailById(
×
217
        Mail\Transport\QueueableTransport $transport,
218
        string $queueItemId,
219
    ): ?Mail\Queue\MailQueueItem {
220
        foreach ($transport->getMailQueue() as $mailQueueItem) {
×
221
            if ($mailQueueItem->id === $queueItemId) {
×
222
                return $mailQueueItem;
×
223
            }
224
        }
225

226
        return null;
×
227
    }
228

229
    private function addLinkToConfigurationModule(Backend\Template\ModuleTemplate $template): void
×
230
    {
231
        $buttonBar = $template->getDocHeaderComponent()->getButtonBar();
×
232

233
        $button = $buttonBar->makeLinkButton();
×
234
        $button->setHref(
×
235
            (string)$this->uriBuilder->buildUriFromRoute(
×
236
                'system_config',
×
237
                ['node' => ['MAIL' => 1], 'tree' => 'confVars'],
×
238
            )
×
239
        );
×
240
        $button->setIcon($this->iconFactory->getIcon('actions-cog', Core\Imaging\Icon::SIZE_SMALL));
×
241
        $button->setTitle(self::translate('button.config'));
×
242
        $button->setShowLabelText(true);
×
243

244
        $buttonBar->addButton($button);
×
245
    }
246

247
    /**
248
     * @throws Exception\MailTransportIsNotConfigured
249
     */
250
    private function getTransportFromMailConfiguration(): string
×
251
    {
252
        $mailConfiguration = $GLOBALS['TYPO3_CONF_VARS']['MAIL'] ?? null;
×
253

254
        if (!\is_array($mailConfiguration)) {
×
255
            throw new Exception\MailTransportIsNotConfigured();
×
256
        }
257

258
        $spoolType = $mailConfiguration['transport_spool_type'] ?? null;
×
259

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

264
        $transport = $mailConfiguration['transport'] ?? null;
×
265

266
        if (is_string($transport) && trim($transport) !== '') {
×
267
            return $transport;
×
268
        }
269

270
        throw new Exception\MailTransportIsNotConfigured();
×
271
    }
272

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

289
        $template->setContent($view->render());
×
290

291
        return new Core\Http\HtmlResponse($template->renderContent());
×
292
    }
293
}
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