webhooks-8.x-1.x-dev/modules/webhook/src/Controller/WebhookTokenController.php

modules/webhook/src/Controller/WebhookTokenController.php
<?php

declare(strict_types=1);

namespace Drupal\webhook\Controller;

use Adbar\Dot;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Returns responses for Webhook routes.
 */
final class WebhookTokenController extends ControllerBase {

  /**
   * The controller constructor.
   */
  public function __construct(
    private readonly RouteMatchInterface $routeMatch,
  ) {}

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container): self {
    return new self(
      $container->get('current_route_match'),
    );
  }

  /**
   * Builds the response.
   */
  public function __invoke(): array {
    $webhook = $this->getCurrentWebhook();
    $data = [
      'headers' => $webhook ? $this->getCurrentWebhook()->getHeaders() : '',
      'payload' => $webhook ? $this->getCurrentWebhook()->getPayload() : '',
    ];

    $dot = new Dot($data);
    $build['tokens']['payload'] = [
      '#theme' => 'item_list',
      '#list_type' => 'ul',
      '#items' => array_keys($dot->flatten()),
    ];
    $build['#attached']['library'][] = 'webhook/backend';
    return $build;
  }

  /**
   * Gets current webhook entity.
   */
  public function getCurrentWebhook() {
    /** @var \Drupal\webhook\Entity\WebhookType $webhook_type */
    $webhook_type = $this->routeMatch->getParameter('webhook_type');
    $entity_query = $this->entityTypeManager()->getStorage('webhook')
      ->getQuery();
    $entity_query
      ->condition('bundle', $webhook_type)
      ->accessCheck(FALSE)
      ->sort('created', 'DESC')
      ->range(0, 1);
    if ($ids = $entity_query->execute()) {
      /** @var \Drupal\webhook\Entity\Webhook */
      return $this->entityTypeManager
        ->getStorage('webhook')
        ->load(reset($ids));
    }
    return NULL;
  }

}

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

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