vendor/ibexa/personalization/src/lib/PageBlock/Event/Subscriber/PersonalizationBlocksSubscriber.php line 50

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\Personalization\PageBlock\Event\Subscriber;
  8. use Ibexa\Contracts\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionEvents;
  9. use Ibexa\FieldTypePage\FieldType\Page\Block\Definition\Event\BlockDefinitionEvent;
  10. use Ibexa\Personalization\Security\Service\SecurityServiceInterface;
  11. use Ibexa\Personalization\Service\Setting\SettingServiceInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. final class PersonalizationBlocksSubscriber implements EventSubscriberInterface
  14. {
  15. private const BLOCK_IDENTIFIER_PERSONALIZED = 'personalized';
  16. private const BLOCK_IDENTIFIER_DYNAMIC_TARGETING = 'dynamic_targeting';
  17. private const BLOCK_IDENTIFIER_LAST_VIEWED = 'last_viewed';
  18. private const BLOCK_IDENTIFIER_LAST_PURCHASED = 'last_purchased';
  19. private const BLOCK_IDENTIFIER_RECENTLY_ADDED = 'recently_added';
  20. private const BLOCK_IDENTIFIER_BESTSELLERS = 'bestsellers';
  21. private SecurityServiceInterface $securityService;
  22. private SettingServiceInterface $settingService;
  23. public function __construct(
  24. SecurityServiceInterface $securityService,
  25. SettingServiceInterface $settingService
  26. ) {
  27. $this->securityService = $securityService;
  28. $this->settingService = $settingService;
  29. }
  30. public static function getSubscribedEvents(): array
  31. {
  32. return [
  33. BlockDefinitionEvents::getBlockDefinitionEventName(self::BLOCK_IDENTIFIER_PERSONALIZED) => 'onBlockDefinition',
  34. BlockDefinitionEvents::getBlockDefinitionEventName(self::BLOCK_IDENTIFIER_DYNAMIC_TARGETING) => 'onBlockDefinition',
  35. BlockDefinitionEvents::getBlockDefinitionEventName(self::BLOCK_IDENTIFIER_LAST_VIEWED) => 'onBlockDefinition',
  36. BlockDefinitionEvents::getBlockDefinitionEventName(self::BLOCK_IDENTIFIER_LAST_PURCHASED) => 'onBlockDefinition',
  37. BlockDefinitionEvents::getBlockDefinitionEventName(self::BLOCK_IDENTIFIER_RECENTLY_ADDED) => 'onBlockDefinition',
  38. BlockDefinitionEvents::getBlockDefinitionEventName(self::BLOCK_IDENTIFIER_BESTSELLERS) => 'onBlockDefinition',
  39. ];
  40. }
  41. public function onBlockDefinition(BlockDefinitionEvent $blockDefinitionEvent): void
  42. {
  43. if (empty($this->settingService->getInstallationKey()) || !$this->hasCredentials()) {
  44. $blockDefinitionEvent->getDefinition()->setVisible(false);
  45. }
  46. }
  47. private function hasCredentials(): bool
  48. {
  49. $customerId = $this->securityService->getCurrentCustomerId();
  50. return !empty($customerId)
  51. && !empty($this->settingService->getLicenceKeyByCustomerId($customerId));
  52. }
  53. }