vendor/ibexa/connect/src/bundle/EventSubscriber/PageBuilder/AbstractEnabledQueryParameterWebhookSubscriber.php line 75

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\Connect\EventSubscriber\PageBuilder;
  8. use Ibexa\Bundle\Connect\Event\PrePageBlockWebhookRequestEvent;
  9. use Ibexa\Bundle\Connect\EventSubscriber\PageBuilderPreRenderEventSubscriber;
  10. use Ibexa\Contracts\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionEvents;
  11. use Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockAttributeFactoryInterface;
  12. use Ibexa\FieldTypePage\FieldType\Page\Block\Definition\Event\BlockDefinitionEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. abstract class AbstractEnabledQueryParameterWebhookSubscriber implements EventSubscriberInterface
  15. {
  16.     private BlockAttributeFactoryInterface $blockAttributeFactory;
  17.     public function __construct(
  18.         BlockAttributeFactoryInterface $blockAttributeFactory
  19.     ) {
  20.         $this->blockAttributeFactory $blockAttributeFactory;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         $blockDefinitionEvent BlockDefinitionEvents::getBlockDefinitionEventName(
  25.             PageBuilderPreRenderEventSubscriber::IBEXA_CONNECT_BLOCK,
  26.         );
  27.         return [
  28.             PrePageBlockWebhookRequestEvent::class => 'onBlockRender',
  29.             $blockDefinitionEvent => 'onBlockDefinition',
  30.         ];
  31.     }
  32.     /**
  33.      * @phpstan-return scalar|null
  34.      */
  35.     abstract protected function resolveQueryParameter();
  36.     /** @phpstan-return non-empty-string */
  37.     abstract protected function getAttributeIdentifier(): string;
  38.     /** @phpstan-return non-empty-string */
  39.     abstract protected function getQueryParameterName(): string;
  40.     abstract protected function getAttributeName(): string;
  41.     final public function onBlockRender(PrePageBlockWebhookRequestEvent $event): void
  42.     {
  43.         $attributeIdentifier $this->getAttributeIdentifier();
  44.         $blockValue $event->getBlockValue();
  45.         $attribute $blockValue->getAttribute($attributeIdentifier);
  46.         if ($attribute === null) {
  47.             return;
  48.         }
  49.         $value $attribute->getValue();
  50.         // $value is "0" or "1". Leaving it loosely equal for future, proper mapping.
  51.         if (!$value) {
  52.             return;
  53.         }
  54.         $parameter $this->resolveQueryParameter();
  55.         if ($parameter !== null) {
  56.             $queryParameterName $this->getQueryParameterName();
  57.             $event->addQueryParameter($queryParameterName$parameter);
  58.         }
  59.     }
  60.     final public function onBlockDefinition(BlockDefinitionEvent $event): void
  61.     {
  62.         $attributeIdentifier $this->getAttributeIdentifier();
  63.         $configuration $event->getConfiguration();
  64.         $blockDefinition $event->getDefinition();
  65.         $attribute $this->blockAttributeFactory->create(
  66.             PageBuilderPreRenderEventSubscriber::IBEXA_CONNECT_BLOCK,
  67.             $attributeIdentifier,
  68.             [
  69.                 'name' => $this->getAttributeName(),
  70.                 'type' => 'checkbox',
  71.                 'category' => 'default',
  72.                 'validators' => [],
  73.             ],
  74.             $configuration,
  75.         );
  76.         $attributes $blockDefinition->getAttributes();
  77.         $attributes[$attributeIdentifier] = $attribute;
  78.         $blockDefinition->setAttributes($attributes);
  79.     }
  80. }