vendor/ibexa/connect/src/bundle/EventSubscriber/FormBuilderSubscriber.php line 79

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;
  8. use Ibexa\Bundle\Connect\Message\WebhookRequest;
  9. use Ibexa\Bundle\Connect\MessageHandler\WebhookRequestHandler;
  10. use Ibexa\FormBuilder\Definition\FieldAttributeDefinitionBuilder;
  11. use Ibexa\FormBuilder\Event\FieldDefinitionEvent;
  12. use Ibexa\FormBuilder\Event\FormEvents;
  13. use Ibexa\FormBuilder\Event\FormSubmitEvent;
  14. use Ibexa\FormBuilder\Exception\FormFieldNotFoundException;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. final class FormBuilderSubscriber implements EventSubscriberInterface
  17. {
  18. private const ATTRIBUTE_IDENTIFIER = 'ibexa_connect_webhook_url';
  19. private WebhookRequestHandler $notificationHandler;
  20. public function __construct(WebhookRequestHandler $notificationHandler)
  21. {
  22. $this->notificationHandler = $notificationHandler;
  23. }
  24. public static function getSubscribedEvents(): array
  25. {
  26. return [
  27. FormEvents::FORM_SUBMIT => 'onFormSubmit',
  28. FormEvents::getFieldDefinitionEventName('button') => 'onButtonField',
  29. ];
  30. }
  31. public function onFormSubmit(FormSubmitEvent $event): void
  32. {
  33. try {
  34. $button = $event->getForm()->getFieldByIdentifier('button');
  35. } catch (FormFieldNotFoundException $e) {
  36. return;
  37. }
  38. if (!$button->hasAttribute(self::ATTRIBUTE_IDENTIFIER)) {
  39. return;
  40. }
  41. $url = $button->getAttributeValue(self::ATTRIBUTE_IDENTIFIER);
  42. if (!is_string($url) || empty($url)) {
  43. return;
  44. }
  45. $fields = $event->getForm()->getFields();
  46. $data = $event->getData();
  47. $requestData = [
  48. 'languageCode' => $data['languageCode'],
  49. 'contentId' => $data['contentId'],
  50. 'contentFieldId' => $data['contentFieldId'],
  51. 'fields' => [],
  52. ];
  53. foreach ($fields as $field) {
  54. $requestData['fields'][] = [
  55. 'field_type_identifier' => $field->getIdentifier(),
  56. 'field_name' => $field->getName(),
  57. 'value' => $data['fields'][$field->getId()] ?? null,
  58. ];
  59. }
  60. $notification = new WebhookRequest($url, $requestData);
  61. $this->notificationHandler->handle($notification);
  62. }
  63. public function onButtonField(FieldDefinitionEvent $event): void
  64. {
  65. $webhookAttribute = new FieldAttributeDefinitionBuilder();
  66. $webhookAttribute->setIdentifier(self::ATTRIBUTE_IDENTIFIER);
  67. $webhookAttribute->setName('Ibexa Connect Webhook URL');
  68. $webhookAttribute->setType('string');
  69. $definitionBuilder = $event->getDefinitionBuilder();
  70. $definitionBuilder->addAttribute($webhookAttribute->buildDefinition());
  71. }
  72. }