vendor/ibexa/dashboard/src/lib/EventSubscriber/DeleteDashboardSubscriber.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\Events\Trash\BeforeTrashEvent;
  9. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  10. use Ibexa\Core\Base\Exceptions\ForbiddenException;
  11. use Ibexa\Dashboard\Specification\IsDefaultDashboard;
  12. use JMS\TranslationBundle\Annotation\Desc;
  13. use JMS\TranslationBundle\Annotation\Ignore;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. final class DeleteDashboardSubscriber implements EventSubscriberInterface
  17. {
  18. private ConfigResolverInterface $configResolver;
  19. private TranslatorInterface $translator;
  20. public function __construct(
  21. ConfigResolverInterface $configResolver,
  22. TranslatorInterface $translator
  23. ) {
  24. $this->configResolver = $configResolver;
  25. $this->translator = $translator;
  26. }
  27. public static function getSubscribedEvents(): array
  28. {
  29. return [
  30. BeforeTrashEvent::class => ['onBeforeTrashEvent'],
  31. ];
  32. }
  33. /**
  34. * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ForbiddenException
  35. * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
  36. */
  37. public function onBeforeTrashEvent(BeforeTrashEvent $event): void
  38. {
  39. if (!(new IsDefaultDashboard($this->configResolver))->isSatisfiedBy($event->getLocation())) {
  40. return;
  41. }
  42. $message = $this->translator->trans(
  43. /** @Desc("The default dashboard cannot be sent to the trash") */
  44. 'dashboard.sent_to_trash.info',
  45. [],
  46. 'ibexa_dashboard'
  47. );
  48. throw new ForbiddenException(
  49. /** @Ignore */
  50. $message
  51. );
  52. }
  53. }