Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
WebmailWebController
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
2 / 2
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 actionIndex
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Modules\Mail\Presentation\Web;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Modules\Mail\Application\Service\WebmailAuthService;
12use App\Modules\Mail\Domain\Repository\MailRepositoryInterface;
13use Psr\Http\Message\ResponseFactoryInterface;
14use Psr\Http\Message\ResponseInterface;
15use Psr\Http\Message\ServerRequestInterface;
16use Twig\Environment as TwigEnvironment;
17use Yiisoft\User\CurrentUser;
18
19/**
20 * Webmail Full-Page Web Controller.
21 *
22 * Serves the primary /mail desktop interface: verifies user mailbox bindings,
23 * renders login/connect state or the 3-column Roundcube Elastic workspace shell.
24 *
25 * @package App\Modules\Mail\Presentation\Web
26 */
27final readonly class WebmailWebController
28{
29    /**
30     * WebmailWebController constructor.
31     *
32     * @param TwigEnvironment          $twig            Twig template engine.
33     * @param CurrentUser              $currentUser     Authenticated identity service.
34     * @param ResponseFactoryInterface $responseFactory PSR-17 response factory.
35     * @param WebmailAuthService       $authService     Webmail authentication service.
36     * @param MailRepositoryInterface  $mailRepo        Mail repository.
37     */
38    public function __construct(
39        private TwigEnvironment $twig,
40        private CurrentUser $currentUser,
41        private ResponseFactoryInterface $responseFactory,
42        private WebmailAuthService $authService,
43        private MailRepositoryInterface $mailRepo
44    ) {
45    }
46
47    /**
48     * Handles GET /mail.
49     *
50     * @param ServerRequestInterface $request PSR-7 Server request.
51     * @return ResponseInterface Rendered HTML response.
52     */
53    public function actionIndex(ServerRequestInterface $request): ResponseInterface
54    {
55        $userId = (int) $this->currentUser->getId();
56        $mailboxes = $this->authService->getUserMailboxes($userId);
57
58        $params = $request->getQueryParams();
59        $selectedMailboxId = isset($params['mailbox_id']) ? (int) $params['mailbox_id'] : null;
60
61        $activeMailbox = null;
62        if ($mailboxes !== []) {
63            if ($selectedMailboxId !== null) {
64                foreach ($mailboxes as $mbox) {
65                    if ($mbox->id === $selectedMailboxId) {
66                        $activeMailbox = $mbox;
67                        break;
68                    }
69                }
70            }
71            if ($activeMailbox === null) {
72                $activeMailbox = $mailboxes[0];
73            }
74        }
75
76        $activeFolder = trim((string) ($params['folder'] ?? ''));
77        if ($activeFolder === '') {
78            $activeFolder = 'INBOX';
79        }
80        $activeMailboxId = $activeMailbox !== null ? $activeMailbox->id : 0;
81        $isStandalone = ($params['mode'] ?? '') === 'standalone';
82
83        $publicServers = $this->mailRepo->findAllActiveServers();
84        $html = $this->twig->render('mail/webmail/index.twig', [
85            'is_standalone'     => $isStandalone,
86            'mailboxes'         => $mailboxes,
87            'active_mailbox'    => $activeMailbox,
88            'active_mailbox_id' => $activeMailboxId,
89            'mailbox_id'        => $activeMailboxId,
90            'active_folder'     => $activeFolder,
91            'folder'            => $activeFolder,
92            'folder_tree'       => [],
93            'public_servers'    => $publicServers,
94            'has_mailboxes'     => $mailboxes !== [],
95            'layout_mode'       => (string) ($params['layout'] ?? '3col'),
96            'current_user_id'   => $userId,
97        ]);
98
99        $response = $this->responseFactory->createResponse(200)
100            ->withHeader('Content-Type', 'text/html; charset=utf-8');
101        $response->getBody()->write($html);
102
103        return $response;
104    }
105}