vendor/ibexa/dashboard/src/lib/EventSubscriber/PublishDashboardSubscriber.php line 44

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\Events\Content\PublishVersionEvent;
  9. use Ibexa\Contracts\Core\Repository\Values\Content\Location;
  10. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  11. use Ibexa\Dashboard\Specification\IsDashboardContentType;
  12. use Ibexa\Dashboard\Specification\IsPredefinedDashboard;
  13. use Ibexa\User\UserSetting\UserSettingService;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. final class PublishDashboardSubscriber implements EventSubscriberInterface
  16. {
  17.     private ConfigResolverInterface $configResolver;
  18.     private UserSettingService $userSettingService;
  19.     public function __construct(
  20.         ConfigResolverInterface $configResolver,
  21.         UserSettingService $userSettingService
  22.     ) {
  23.         $this->configResolver $configResolver;
  24.         $this->userSettingService $userSettingService;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             PublishVersionEvent::class => ['onPublishDashboard'],
  30.         ];
  31.     }
  32.     /**
  33.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
  34.      * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  35.      */
  36.     public function onPublishDashboard(PublishVersionEvent $event): void
  37.     {
  38.         $contentInfo $event->getVersionInfo()->getContentInfo();
  39.         $contentType $contentInfo->getContentType();
  40.         $location $contentInfo->getMainLocation();
  41.         if ($location === null || !(new IsDashboardContentType($this->configResolver))->isSatisfiedBy($contentType)) {
  42.             return;
  43.         }
  44.         if ($this->isPredefinedDashboard($location)) {
  45.             return;
  46.         }
  47.         $activeDashboardRemoteId $this->userSettingService->getUserSetting('active_dashboard')->value;
  48.         if ($location->remoteId === $activeDashboardRemoteId) {
  49.             return;
  50.         }
  51.         $this->userSettingService->setUserSetting('active_dashboard'$location->remoteId);
  52.     }
  53.     private function isPredefinedDashboard(Location $location): bool
  54.     {
  55.         $predefinedContainerRemoteId $this->configResolver->getParameter(
  56.             'dashboard.predefined_container_remote_id'
  57.         );
  58.         return (new IsPredefinedDashboard($predefinedContainerRemoteId))->isSatisfiedBy($location);
  59.     }
  60. }