vendor/ibexa/dashboard/src/lib/EventSubscriber/DashboardTypeSubscriber.php line 45

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\Contracts\Core\Repository\ContentTypeService;
  9. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  10. use Ibexa\Dashboard\Specification\IsDashboardContentType;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Routing\RouterInterface;
  16. final class DashboardTypeSubscriber implements EventSubscriberInterface
  17. {
  18.     private ContentTypeService $contentTypeService;
  19.     private ConfigResolverInterface $configResolver;
  20.     private RouterInterface $router;
  21.     public function __construct(
  22.         ContentTypeService $contentTypeService,
  23.         ConfigResolverInterface $configResolver,
  24.         RouterInterface $router
  25.     ) {
  26.         $this->contentTypeService $contentTypeService;
  27.         $this->configResolver $configResolver;
  28.         $this->router $router;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             KernelEvents::REQUEST => ['onKernelController'],
  34.         ];
  35.     }
  36.     public function onKernelController(RequestEvent $event): void
  37.     {
  38.         if (!$this->isContentTypeRedirect($event)) {
  39.             return;
  40.         }
  41.         $dashboardTypeIdentifier $this->configResolver->getParameter(
  42.             IsDashboardContentType::CONTENT_TYPE_IDENTIFIER_PARAM_NAME
  43.         );
  44.         $dashboardType $this->contentTypeService->loadContentTypeByIdentifier($dashboardTypeIdentifier);
  45.         $dashboardTypeGroups $dashboardType->getContentTypeGroups();
  46.         $dashboardTypeGroup reset($dashboardTypeGroups);
  47.         $contentTypeGroupId = (int)$event->getRequest()->get('contentTypeGroupId');
  48.         if ($dashboardTypeGroup === false || $dashboardTypeGroup->id !== $contentTypeGroupId) {
  49.             return;
  50.         }
  51.         $redirectUrl $this->router->generate('ibexa.dashboard.content_type');
  52.         $event->setResponse(new RedirectResponse($redirectUrl));
  53.     }
  54.     private function isContentTypeRedirect(RequestEvent $event): bool
  55.     {
  56.         return $event->getRequest()->get('_route') === 'ibexa.content_type.view'
  57.             || $event->getRequest()->get('_route') === 'ibexa.content_type_group.view';
  58.     }
  59. }