You are here

class WebhookSubscriber in Webhooks 8

Webhook event subscriber.

Hierarchy

Expanded class hierarchy of WebhookSubscriber

1 string reference to 'WebhookSubscriber'
webhook.services.yml in modules/webhook/webhook.services.yml
modules/webhook/webhook.services.yml
1 service uses WebhookSubscriber
webhook.event_subscriber in modules/webhook/webhook.services.yml
Drupal\webhook\EventSubscriber\WebhookSubscriber

File

modules/webhook/src/EventSubscriber/WebhookSubscriber.php, line 16

Namespace

Drupal\webhook\EventSubscriber
View source
class WebhookSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Constructs event subscriber.
   *
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(MessengerInterface $messenger) {
    $this->messenger = $messenger;
  }

  /**
   * Webhook send event handler.
   *
   * @param \Drupal\webhooks\Event\SendEvent $event
   *   Response event.
   */
  public function onWebhookSend(SendEvent $event) {
  }

  /**
   * Webhook receive event handler.
   *
   * @param \Drupal\webhooks\Event\ReceiveEvent $event
   *   Response event.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  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();
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      WebhookEvents::SEND => [
        'onWebhookSend',
      ],
      WebhookEvents::RECEIVE => [
        'onWebhookReceive',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WebhookSubscriber::$messenger protected property The messenger.
WebhookSubscriber::getSubscribedEvents public static function
WebhookSubscriber::onWebhookReceive public function Webhook receive event handler.
WebhookSubscriber::onWebhookSend public function Webhook send event handler.
WebhookSubscriber::__construct public function Constructs event subscriber.