You are here

public function RegisterWebhook::onHandleWebhook in Acquia Content Hub 8.2

The method called for the AcquiaContentHubEvents::HANDLE_WEBHOOK event.

Parameters

\Drupal\acquia_contenthub\Event\HandleWebhookEvent $event: The dispatched event.

File

src/EventSubscriber/HandleWebhook/RegisterWebhook.php, line 55

Class

RegisterWebhook
Responsible for handling site registration webhook responses.

Namespace

Drupal\acquia_contenthub\EventSubscriber\HandleWebhook

Code

public function onHandleWebhook(HandleWebhookEvent $event) {
  $payload = $event
    ->getPayload();
  if ($payload['status'] == 'pending') {
    $client = $event
      ->getClient();
    $uuid = isset($payload['uuid']) ? $payload['uuid'] : FALSE;
    if ($uuid && $payload['publickey'] == $client
      ->getSettings()
      ->getApiKey()) {
      $response = new Response();
      if (class_exists(DiactorosFactory::class)) {
        $httpMessageFactory = new DiactorosFactory();
      }
      else {
        $httpMessageFactory = new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory());
      }
      $psr7_request = $httpMessageFactory
        ->createRequest($event
        ->getRequest());
      $signer = new ResponseSigner($event
        ->getKey(), $psr7_request);
      $signedResponse = $signer
        ->signResponse($response);
      $event
        ->setResponse($signedResponse);
      return;
    }
    else {
      $ip_address = $event
        ->getRequest()
        ->getClientIp();
      $message = new FormattableMarkup('Webhook [from IP = @IP] rejected (initiator and/or publickey do not match local settings): @whook', [
        '@IP' => $ip_address,
        '@whook' => print_r($payload, TRUE),
      ]);
      $this->channel
        ->debug($message);
      $event
        ->setResponse(new Response());
      return;
    }
  }
}