vendor/ibexa/dashboard/src/lib/EventSubscriber/DashboardSubscriber.php line 71

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Dashboard\EventSubscriber;
  8. use Ibexa\Bundle\Dashboard\Controller\DashboardController;
  9. use Ibexa\Contracts\AdminUi\Notification\TranslatableNotificationHandlerInterface;
  10. use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
  11. use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
  12. use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
  13. use Ibexa\Contracts\Core\Repository\LocationService;
  14. use Ibexa\Contracts\Core\Repository\Values\Content\Location;
  15. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  16. use Ibexa\Dashboard\Exception\MissingDashboardLocationException;
  17. use Ibexa\User\UserSetting\UserSettingService;
  18. use JMS\TranslationBundle\Annotation\Desc;
  19. use Psr\Log\LoggerAwareTrait;
  20. use Psr\Log\LoggerInterface;
  21. use Psr\Log\NullLogger;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpKernel\Event\RequestEvent;
  25. use Symfony\Component\HttpKernel\HttpKernelInterface;
  26. use Symfony\Component\HttpKernel\KernelEvents;
  27. use Twig\Environment;
  28. final class DashboardSubscriber implements EventSubscriberInterface
  29. {
  30. use LoggerAwareTrait;
  31. private const STATIC_DASHBOARD_ROUTE = 'ibexa.dashboard';
  32. private LocationService $locationService;
  33. private UserSettingService $userSettingService;
  34. private Environment $twig;
  35. private TranslatableNotificationHandlerInterface $notificationHandler;
  36. private ConfigResolverInterface $configResolver;
  37. public function __construct(
  38. LocationService $locationService,
  39. UserSettingService $userSettingService,
  40. Environment $twig,
  41. TranslatableNotificationHandlerInterface $notificationHandler,
  42. ConfigResolverInterface $configResolver,
  43. ?LoggerInterface $logger = null
  44. ) {
  45. $this->locationService = $locationService;
  46. $this->userSettingService = $userSettingService;
  47. $this->twig = $twig;
  48. $this->notificationHandler = $notificationHandler;
  49. $this->configResolver = $configResolver;
  50. $this->logger = $logger ?? new NullLogger();
  51. }
  52. public static function getSubscribedEvents(): array
  53. {
  54. return [
  55. KernelEvents::REQUEST => ['onKernelRequest', 0],
  56. ];
  57. }
  58. public function onKernelRequest(RequestEvent $event): void
  59. {
  60. if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
  61. return;
  62. }
  63. $request = $event->getRequest();
  64. if ($request->get('_route') !== self::STATIC_DASHBOARD_ROUTE) {
  65. return;
  66. }
  67. try {
  68. $dashboardLocation = $this->getDashboardLocation();
  69. } catch (MissingDashboardLocationException $e) {
  70. $this->logger->error($e->getMessage());
  71. $this->notificationHandler->error(
  72. /** @Desc("The dashboard could not be loaded") */
  73. 'dashboard.error.unable_to_load',
  74. [],
  75. 'ibexa_dashboard'
  76. );
  77. $response = new Response();
  78. $response->setContent(
  79. $this->twig->render('@ibexadesign/dashboard/dashboard_error.html.twig')
  80. );
  81. $event->setResponse($response);
  82. return;
  83. }
  84. $request->attributes->set('contentId', $dashboardLocation->getContentInfo()->getId());
  85. $request->attributes->set('locationId', $dashboardLocation->id);
  86. $event->getRequest()->attributes->set(
  87. '_controller',
  88. DashboardController::class . '::dashboardAction'
  89. );
  90. }
  91. /**
  92. * @return \Ibexa\Contracts\Core\Repository\Values\Content\Location
  93. */
  94. private function getDashboardLocation(): Location
  95. {
  96. try {
  97. try {
  98. $activeDashboard = $this->userSettingService->getUserSetting('active_dashboard')->value;
  99. $location = $this->locationService->loadLocationByRemoteId($activeDashboard);
  100. } catch (NotFoundException|UnauthorizedException|InvalidArgumentException $e) {
  101. $defaultDashboardRemoteId = $this->configResolver->getParameter('dashboard.default_dashboard_remote_id');
  102. $location = $this->locationService->loadLocationByRemoteId($defaultDashboardRemoteId);
  103. }
  104. } catch (NotFoundException|UnauthorizedException $e) {
  105. throw new MissingDashboardLocationException('The dashboard could not be loaded. ' . $e->getMessage());
  106. }
  107. return $location;
  108. }
  109. }