public function WebhooksService::receive in Webhooks 8
Receive a webhook.
Parameters
string $name: The machine name of a webhook.
Return value
\Drupal\webhooks\Webhook A webhook object.
Throws
\Drupal\webhooks\Exception\WebhookIncomingEndpointNotFoundException Thrown when the webhook endpoint is not found.
Overrides WebhookReceiverInterface::receive
File
- src/
WebhooksService.php, line 239
Class
- WebhooksService
- Class WebhookService.
Namespace
Drupal\webhooksCode
public function receive($name) {
// We only receive webhook requests when a webhook configuration exists
// with a matching machine name.
$query = $this->webhookStorage
->getQuery()
->condition('id', $name)
->condition('type', 'incoming')
->condition('status', 1);
$ids = $query
->execute();
if (!array_key_exists($name, $ids)) {
throw new WebhookIncomingEndpointNotFoundException($name);
}
$request = $this->requestStack
->getCurrentRequest();
$payload = $this
->decode($request
->getContent(), $request
->getContentType());
$webhook = new Webhook($payload, $request->headers
->all(), '', $request->headers
->get('Content-Type'));
$webhook
->setUuid($request->headers
->get('X-Drupal-Delivery'));
$webhook
->setEvent($request->headers
->get('X-Drupal-Event'));
/** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
$webhook_config = $this->webhookStorage
->load($name);
// Verify in both cases: the webhook_config contains a secret
// and/or the webhook contains a signature.
if ($webhook_config
->getSecret() || $webhook
->getSignature()) {
Webhook::verify($webhook_config
->getSecret(), $request
->getContent(), $webhook
->getSignature());
}
elseif ($webhook_config
->getSecret() || $webhook
->getToken()) {
Webhook::verifyToken($webhook_config
->getToken(), $webhook
->getToken());
}
if ($webhook_config
->isNonBlocking()) {
$this->queue
->createItem([
'id' => $name,
'webhook' => $webhook,
]);
}
else {
// Dispatch Webhook Receive event.
$this->eventDispatcher
->dispatch(WebhookEvents::RECEIVE, new ReceiveEvent($webhook_config, $webhook));
}
if (!$webhook
->getStatus()) {
$this->logger
->warning('Processing Failure. Subscriber %subscriber on Webhook %uuid for Event %event. Payload: @payload', [
'%subscriber' => $webhook_config
->id(),
'%uuid' => $webhook
->getUuid(),
'%event' => $webhook
->getEvent(),
'@payload' => $this
->encode($webhook
->getPayload(), $webhook
->getMimeSubType()),
'link' => Link::createFromRoute($this
->t('Edit Webhook'), 'entity.webhook_config.edit_form', [
'webhook_config' => $webhook_config
->id(),
])
->toString(),
]);
}
return $webhook;
}