vendor/ibexa/product-catalog/src/bundle/EventSubscriber/AbstractLocalViewSubscriber.php line 43

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\Bundle\ProductCatalog\EventSubscriber;
  8. use Ibexa\Bundle\AdminUi\IbexaAdminUiBundle;
  9. use Ibexa\Core\MVC\Symfony\Event\PreContentViewEvent;
  10. use Ibexa\Core\MVC\Symfony\MVCEvents;
  11. use Ibexa\Core\MVC\Symfony\SiteAccess\SiteAccessServiceInterface;
  12. use Ibexa\Core\MVC\Symfony\View\View;
  13. use Ibexa\ProductCatalog\Config\ConfigProviderInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. abstract class AbstractLocalViewSubscriber implements EventSubscriberInterface
  16. {
  17. private SiteAccessServiceInterface $siteAccessService;
  18. private ConfigProviderInterface $configProvider;
  19. public function __construct(
  20. SiteAccessServiceInterface $siteAccessService,
  21. ConfigProviderInterface $configProvider
  22. ) {
  23. $this->siteAccessService = $siteAccessService;
  24. $this->configProvider = $configProvider;
  25. }
  26. /**
  27. * @return array<string,string>
  28. */
  29. final public static function getSubscribedEvents(): array
  30. {
  31. return [
  32. MVCEvents::PRE_CONTENT_VIEW => 'onPreContentView',
  33. ];
  34. }
  35. final public function onPreContentView(PreContentViewEvent $event): void
  36. {
  37. $view = $event->getContentView();
  38. if ($this->supports($view) && $this->isAdminSiteAccess() && $this->isLocal()) {
  39. $this->configureView($view);
  40. }
  41. }
  42. /**
  43. * Returns true if given $view is supported by subscriber.
  44. */
  45. abstract protected function supports(View $view): bool;
  46. abstract protected function configureView(View $view): void;
  47. /**
  48. * Returns true if current product catalog implementation is editable.
  49. */
  50. protected function isLocal(): bool
  51. {
  52. return $this->configProvider->getEngineType() === 'local';
  53. }
  54. /**
  55. * Returns true if current siteaccess is administrative.
  56. */
  57. private function isAdminSiteAccess(): bool
  58. {
  59. $currentSiteAccess = $this->siteAccessService->getCurrent();
  60. if ($currentSiteAccess === null) {
  61. return false;
  62. }
  63. // Workaround: for some reason value returned from SiteAccessServiceInterface::getCurrent() doesn't
  64. // contain groups
  65. $currentSiteAccess = $this->siteAccessService->get($currentSiteAccess->name);
  66. foreach ($currentSiteAccess->groups as $group) {
  67. if ($group->getName() === IbexaAdminUiBundle::ADMIN_GROUP_NAME) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. }