WebhookSubscriber.php in Webhooks 8
File
modules/webhook/src/EventSubscriber/WebhookSubscriber.php
View source
<?php
namespace Drupal\webhook\EventSubscriber;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\webhook\Entity\Webhook;
use Drupal\webhooks\Event\ReceiveEvent;
use Drupal\webhooks\Event\SendEvent;
use Drupal\webhooks\Event\WebhookEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class WebhookSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
protected $messenger;
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
public function onWebhookSend(SendEvent $event) {
}
public function onWebhookReceive(ReceiveEvent $event) {
$webhook = Webhook::create([
'title' => $this
->t('Webhook @uuid', [
'@uuid' => $event
->getWebhook()
->getUuid(),
]),
'headers' => json_encode($event
->getWebhook()
->getHeaders()),
'payload' => json_encode($event
->getWebhook()
->getPayload()),
'created' => time(),
]);
$webhook
->save();
}
public static function getSubscribedEvents() {
return [
WebhookEvents::SEND => [
'onWebhookSend',
],
WebhookEvents::RECEIVE => [
'onWebhookReceive',
],
];
}
}