headless_cms-1.0.3/modules/headless_cms_notify_webhook/src/Plugin/HeadlessNotifyTransport/HeadlessNotifyWebhookTransport.php

modules/headless_cms_notify_webhook/src/Plugin/HeadlessNotifyTransport/HeadlessNotifyWebhookTransport.php
<?php

declare(strict_types=1);

namespace Drupal\headless_cms_notify_webhook\Plugin\HeadlessNotifyTransport;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\headless_cms_notify\Attribute\HeadlessNotifyTransport;
use Drupal\headless_cms_notify\NotifyMessage\HeadlessNotifyMessageInterface;
use Drupal\headless_cms_notify\Plugin\HeadlessNotifyTransport\HeadlessNotifyTransportBase;
use Drupal\headless_cms_notify\Plugin\HeadlessNotifyTransportPluginFormInterface;
use Drupal\headless_cms_notify_webhook\HeadlessNotifyWebhookService;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * A transport that sends messages to a webhook.
 */
#[HeadlessNotifyTransport(
  id: 'webhook',
  label: new TranslatableMarkup('Webhook'),
)]
class HeadlessNotifyWebhookTransport extends HeadlessNotifyTransportBase implements HeadlessNotifyTransportPluginFormInterface {

  public function __construct(
    protected readonly HeadlessNotifyWebhookService $webhookService,
    array $configuration,
    $plugin_id,
    $plugin_definition,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $container->get(HeadlessNotifyWebhookService::class),
      $configuration,
      $plugin_id,
      $plugin_definition,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'url' => '',
      'use_queue' => TRUE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = [];

    $form['url'] = [
      '#type' => 'textfield',
      '#title' => $this->t('URL'),
      '#description' => $this->t('The URL to send the message to.'),
      '#default_value' => $this->configuration['url'],
      '#required' => TRUE,
    ];

    $form['use_queue'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Use Queue'),
      '#description' => $this->t('Whether to use the queue to send webhook requests.'),
      '#default_value' => $this->configuration['use_queue'],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $url = $form_state->getValue('url');

    // Validate the URL.
    if (!filter_var($url, FILTER_VALIDATE_URL)) {
      $form_state->setErrorByName('url', $this->t('The URL is not valid.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['url'] = $form_state->getValue('url');
    $this->configuration['use_queue'] = (bool) $form_state->getValue('use_queue');
  }

  /**
   * {@inheritdoc}
   */
  public function send(HeadlessNotifyMessageInterface $message): void {
    $this->webhookService->send($this->configuration['url'], $message->toJson(), $this->configuration['use_queue'] ?? TRUE);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc