You are here

public function ContentHubWebhookController::receiveWebhook in Acquia Content Hub 8

Same name and namespace in other branches
  1. 8.2 src/Controller/ContentHubWebhookController.php \Drupal\acquia_contenthub\Controller\ContentHubWebhookController::receiveWebhook()

Process a webhook.

Return value

\Acquia\ContentHubClient\hmacv1\ResponseSigner|null|\Symfony\Component\HttpFoundation\Response The response object or null.

1 string reference to 'ContentHubWebhookController::receiveWebhook'
acquia_contenthub.routing.yml in ./acquia_contenthub.routing.yml
acquia_contenthub.routing.yml

File

src/Controller/ContentHubWebhookController.php, line 131

Class

ContentHubWebhookController
Controller for Content Hub Imported Entities.

Namespace

Drupal\acquia_contenthub\Controller

Code

public function receiveWebhook() {

  // Obtain the headers.
  $webhook = $this->request
    ->getContent();
  $response = NULL;
  $logger = $this->loggerFactory
    ->get('acquia_contenthub');
  if (!$this
    ->validateWebhookSignature($this->request)) {
    $ip_address = $this->request
      ->getClientIp();
    $logger
      ->debug('Webhook [from IP = @IP] rejected (Signatures do not match): @whook', [
      '@IP' => $ip_address,
      '@whook' => print_r($webhook, TRUE),
    ]);
    return new Response('');
  }
  $log_msg = 'Webhook landing: ';
  $context = [
    '@webhook' => print_r($webhook, TRUE),
  ];
  if ($webhook = Json::decode($webhook)) {
    $log_msg .= '(Request ID: @request_id - Entity: @uuid.) ';
    $context += [
      '@request_id' => $webhook['requestid'] ?? 'NOT_FOUND',
      '@uuid' => $webhook['uuid'],
    ];

    // Verification process successful!
    // Now we can process the webhook.
    if (isset($webhook['status'])) {
      switch ($webhook['status']) {
        case 'successful':
          $response = $this
            ->processWebhook($webhook);
          break;
        case 'pending':
          $response = $this
            ->registerWebhook($webhook);
          break;
        case 'shared_secret_regenerated':
          $response = $this
            ->updateSharedSecret($webhook);
          break;
        default:

          // If any other webhook we are not processing then just display
          // the response.
          $response = new Response('');
          break;
      }
    }
  }

  // Notify about the arrival of the webhook request.
  $logger
    ->debug($log_msg . '@webhook', $context);
  return $response;
}