Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DashboardWebController
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
2 / 2
7
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%
32 / 32
100.00% covered (success)
100.00%
1 / 1
6
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\Dashboard\Presentation\Web;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Core\Engine\Domain\Model\PermissionContext;
12use App\Core\Instance\Application\Service\InstanceContextManagerInterface;
13use App\Modules\Dashboard\Application\Service\DashboardWidgetsServiceInterface;
14use Psr\Http\Message\ResponseFactoryInterface;
15use Psr\Http\Message\ResponseInterface;
16use Psr\Http\Message\ServerRequestInterface;
17use Twig\Environment as TwigEnvironment;
18
19/**
20 * Dashboard Web Controller.
21 *
22 * Serves main dashboard overview page with configurable calendar widgets
23 * and quick event modal support.
24 *
25 * @package App\Modules\Dashboard\Presentation\Web
26 */
27final readonly class DashboardWebController
28{
29    /**
30     * DashboardWebController constructor.
31     *
32     * @param DashboardWidgetsServiceInterface     $widgetsService         Dashboard widgets service.
33     * @param TwigEnvironment                     $twig                   Twig template engine.
34     * @param ResponseFactoryInterface            $responseFactory        PSR-7 Response factory.
35     * @param InstanceContextManagerInterface|null $instanceContextManager Context manager.
36     */
37    public function __construct(
38        private DashboardWidgetsServiceInterface $widgetsService,
39        private TwigEnvironment $twig,
40        private ResponseFactoryInterface $responseFactory,
41        private ?InstanceContextManagerInterface $instanceContextManager = null,
42    ) {
43    }
44
45    /**
46     * Renders main dashboard page.
47     *
48     * @param ServerRequestInterface $request PSR-7 server request.
49     * @param PermissionContext      $context Security context.
50     * @return ResponseInterface HTML response.
51     */
52    public function actionIndex(
53        ServerRequestInterface $request,
54        PermissionContext      $context
55    ): ResponseInterface {
56        $userId = $context->actorUserId;
57        $activeKeys = $this->widgetsService->getActiveWidgetKeys($userId);
58        $inactive = $this->widgetsService->getInactiveWidgets($userId, $context);
59
60        $currentEvents = in_array(DashboardWidgetsServiceInterface::WIDGET_CURRENT, $activeKeys, true)
61            ? $this->widgetsService->getCurrentCalendarEvents($context, 5)
62            : [];
63        $currentTotalCount = in_array(DashboardWidgetsServiceInterface::WIDGET_CURRENT, $activeKeys, true)
64            ? $this->widgetsService->getCurrentCalendarEventsCount($context)
65            : 0;
66
67        $overdueEvents = in_array(DashboardWidgetsServiceInterface::WIDGET_OVERDUE, $activeKeys, true)
68            ? $this->widgetsService->getOverdueCalendarEvents($context, 5)
69            : [];
70        $overdueTotalCount = in_array(DashboardWidgetsServiceInterface::WIDGET_OVERDUE, $activeKeys, true)
71            ? $this->widgetsService->getOverdueCalendarEventsCount($context)
72            : 0;
73
74        $isRemote = $this->instanceContextManager !== null && $this->instanceContextManager->isRemote();
75        $activeInstance = $this->instanceContextManager?->getActiveInstance();
76
77        $viewData = [
78            'active_keys'         => $activeKeys,
79            'inactive_widgets'    => $inactive,
80            'current_events'      => $currentEvents,
81            'current_total_count' => $currentTotalCount,
82            'overdue_events'      => $overdueEvents,
83            'overdue_total_count' => $overdueTotalCount,
84            'is_remote'           => $isRemote,
85            'active_instance'     => $activeInstance,
86            'request'             => $request,
87        ];
88
89        $html = $this->twig->render('dashboard/index.twig', $viewData);
90
91        $response = $this->responseFactory->createResponse(200);
92        $response->getBody()->write($html);
93
94        return $response->withHeader('Content-Type', 'text/html; charset=utf-8');
95    }
96}