WebhooksTestSubscriber.php in Webhooks 8
File
tests/modules/webhooks_test/src/EventSubscriber/WebhooksTestSubscriber.php
View source
<?php
namespace Drupal\webhooks_test\EventSubscriber;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\webhooks\Event\ReceiveEvent;
use Drupal\webhooks\Event\SendEvent;
use Drupal\webhooks\Event\WebhookEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\State\State;
class WebhooksTestSubscriber implements EventSubscriberInterface {
protected $messenger;
protected $state;
public function __construct(MessengerInterface $messenger, State $state) {
$this->messenger = $messenger;
$this->state = $state;
}
public function onWebhookSend(SendEvent $event) {
$this->state
->set(__FUNCTION__, TRUE);
$webhook = $event
->getWebhook();
$webhook_config = $event
->getWebhookConfig();
$this->state
->set(__FUNCTION__ . '_webhook', $webhook);
$this->state
->set(__FUNCTION__ . '_webhook_config', $webhook_config);
$this->messenger
->addStatus(print_r($webhook_config, TRUE));
$this->messenger
->addStatus(print_r($webhook, TRUE));
}
public function onWebhookReceive(ReceiveEvent $event) {
$this->state
->set(__FUNCTION__, TRUE);
$webhook = $event
->getWebhook();
$webhook_config = $event
->getWebhookConfig();
$this->state
->set(__FUNCTION__ . '_webhook', $webhook);
$this->state
->set(__FUNCTION__ . '_webhook_config', $webhook_config);
$this->messenger
->addStatus(print_r($webhook_config, TRUE));
$this->messenger
->addStatus(print_r($webhook, TRUE));
}
public static function getSubscribedEvents() {
return [
WebhookEvents::SEND => [
'onWebhookSend',
],
WebhookEvents::RECEIVE => [
'onWebhookReceive',
],
];
}
}