You are here

trait HandleWebhookTrait in Acquia Content Hub 8.2

Trait with helper functions for handle webhook.

@package Drupal\acquia_contenthub_publisher

Hierarchy

  • trait \Drupal\acquia_contenthub_publisher\EventSubscriber\HandleWebhook\HandleWebhookTrait

File

modules/acquia_contenthub_publisher/src/EventSubscriber/HandleWebhook/HandleWebhookTrait.php, line 22

Namespace

Drupal\acquia_contenthub_publisher\EventSubscriber\HandleWebhook
View source
trait HandleWebhookTrait {

  /**
   * Handles webhook response.
   *
   * @param \Drupal\acquia_contenthub\Event\HandleWebhookEvent $event
   *   Handle webhook event.
   * @param string $body
   *   Body of request.
   * @param int $response_code
   *   The return response code.
   * @param \Symfony\Component\HttpFoundation\Response|null $response
   *   SymfonyResponse.
   *
   * @return \Psr\Http\Message\ResponseInterface
   *   Returns signed response.
   */
  protected function getResponse(HandleWebhookEvent $event, string $body, int $response_code, SymfonyResponse $response = NULL) : ResponseInterface {
    if (class_exists(DiactorosFactory::class)) {
      $http_message_factory = new DiactorosFactory();
    }
    else {
      $http_message_factory = new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory());
    }
    $psr7_request = $http_message_factory
      ->createRequest($event
      ->getRequest());
    $signer = new ResponseSigner($event
      ->getKey(), $psr7_request);
    if (!$response) {
      $response = new Response($response_code, [], $body);
    }
    else {
      $response = $http_message_factory
        ->createResponse($response);
    }
    return $signer
      ->signResponse($response);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HandleWebhookTrait::getResponse protected function Handles webhook response.