You are here

webhooks.module in Webhooks 8

Contains webhooks.module.

File

webhooks.module
View source
<?php

/**
 * @file
 * Contains webhooks.module.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
use Drupal\webhooks\Webhook;

/**
 * Implements hook_help().
 */
function webhooks_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.webhooks':
      $output = '';
      $output .= '<h3>' . t('Webhooks') . '</h3>';
      $output .= '<p>' . t('User-defined HTTP callbacks.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_entity_insert().
 */
function webhooks_entity_insert(EntityInterface $entity) {

  /** @var \Drupal\webhooks\WebhooksService $webhooks_service */
  $webhooks_service = \Drupal::service('webhooks.service');

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  $event = implode(':', [
    'entity',
    $entity
      ->getEntityType()
      ->id(),
    'create',
  ]);
  $user = User::load(\Drupal::currentUser()
    ->id());
  $webhook_configs = $webhooks_service
    ->loadMultipleByEvent($event);

  /** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
  foreach ($webhook_configs as $webhook_config) {
    $webhook = new Webhook([
      'event' => $event,
      'user' => $serializer
        ->normalize($user),
      'entity' => $serializer
        ->normalize($entity),
    ], [], $event, $webhook_config
      ->getContentType());
    $webhooks_service
      ->send($webhook_config, $webhook);
  }
}

/**
 * Implements hook_entity_update().
 */
function webhooks_entity_update(EntityInterface $entity) {

  /** @var \Drupal\webhooks\WebhooksService $webhooks_service */
  $webhooks_service = \Drupal::service('webhooks.service');

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  $event = implode(':', [
    'entity',
    $entity
      ->getEntityType()
      ->id(),
    'update',
  ]);
  $user = User::load(\Drupal::currentUser()
    ->id());
  $webhook_configs = $webhooks_service
    ->loadMultipleByEvent($event);

  /** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
  foreach ($webhook_configs as $webhook_config) {
    $webhook = new Webhook([
      'event' => $event,
      'user' => $serializer
        ->normalize($user),
      'entity' => $serializer
        ->normalize($entity),
    ], [], $event, $webhook_config
      ->getContentType());
    $webhooks_service
      ->send($webhook_config, $webhook);
  }
}

/**
 * Implements hook_entity_delete().
 */
function webhooks_entity_delete(EntityInterface $entity) {

  /** @var \Drupal\webhooks\WebhooksService $webhooks_service */
  $webhooks_service = \Drupal::service('webhooks.service');

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  $event = implode(':', [
    'entity',
    $entity
      ->getEntityType()
      ->id(),
    'delete',
  ]);
  $user = User::load(\Drupal::currentUser()
    ->id());
  $webhook_configs = $webhooks_service
    ->loadMultipleByEvent($event);

  /** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
  foreach ($webhook_configs as $webhook_config) {
    $webhook = new Webhook([
      'event' => $event,
      'user' => $serializer
        ->normalize($user),
      'entity' => $serializer
        ->normalize($entity),
    ], [], $event, $webhook_config
      ->getContentType());
    $webhooks_service
      ->send($webhook_config, $webhook);
  }
}

/**
 * Implements hook_cron().
 */
function webhooks_cron() {

  /** @var \Drupal\webhooks\WebhooksService $webhooks_service */
  $webhooks_service = \Drupal::service('webhooks.service');
  $event = implode(':', [
    'system',
    'cron',
  ]);
  $webhook_configs = $webhooks_service
    ->loadMultipleByEvent($event);

  /** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
  foreach ($webhook_configs as $webhook_config) {
    $webhook = new Webhook([
      'event' => $event,
    ], [], $event, $webhook_config
      ->getContentType());
    $webhooks_service
      ->send($webhook_config, $webhook);
  }
}

/**
 * Implements hook_file_download().
 */
function webhooks_file_download($uri) {

  /** @var \Drupal\webhooks\WebhooksService $webhooks_service */
  $webhooks_service = \Drupal::service('webhooks.service');

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  $event = implode(':', [
    'system',
    'file_download',
  ]);
  $webhook_configs = $webhooks_service
    ->loadMultipleByEvent($event);

  /** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
  foreach ($webhook_configs as $webhook_config) {
    $webhook = new Webhook([
      'event' => $event,
      'uri' => $serializer
        ->normalize($uri),
    ], [], $event, $webhook_config
      ->getContentType());
    $webhooks_service
      ->send($webhook_config, $webhook);
  }
}

/**
 * Implements hook_modules_installed().
 */
function webhooks_modules_installed($modules, $is_syncing) {

  /** @var \Drupal\webhooks\WebhooksService $webhooks_service */
  $webhooks_service = \Drupal::service('webhooks.service');

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  $event = implode(':', [
    'system',
    'modules_installed',
  ]);
  $webhook_configs = $webhooks_service
    ->loadMultipleByEvent($event);

  /** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
  foreach ($webhook_configs as $webhook_config) {
    $webhook = new Webhook([
      'event' => $event,
      'modules' => $serializer
        ->normalize($modules),
    ], [], $event, $webhook_config
      ->getContentType());
    $webhooks_service
      ->send($webhook_config, $webhook);
  }
}

/**
 * Implements hook_user_cancel().
 */
function webhooks_user_cancel($edit, UserInterface $account, $method) {

  /** @var \Drupal\webhooks\WebhooksService $webhooks_service */
  $webhooks_service = \Drupal::service('webhooks.service');

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  $event = implode(':', [
    'system',
    'user_cancel',
  ]);
  $user = User::load($account
    ->id());
  $webhook_configs = $webhooks_service
    ->loadMultipleByEvent($event);

  /** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
  foreach ($webhook_configs as $webhook_config) {
    $webhook = new Webhook([
      'event' => $event,
      'user' => $serializer
        ->normalize($user),
    ], [], $event, $webhook_config
      ->getContentType());
    $webhooks_service
      ->send($webhook_config, $webhook);
  }
}

/**
 * Implements hook_user_login().
 */
function webhooks_user_login(UserInterface $account) {

  /** @var \Drupal\webhooks\WebhooksService $webhooks_service */
  $webhooks_service = \Drupal::service('webhooks.service');

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  $event = implode(':', [
    'system',
    'user_login',
  ]);
  $user = User::load($account
    ->id());
  $webhook_configs = $webhooks_service
    ->loadMultipleByEvent($event);

  /** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
  foreach ($webhook_configs as $webhook_config) {
    $webhook = new Webhook([
      'event' => $event,
      'user' => $serializer
        ->normalize($user),
    ], [], $event, $webhook_config
      ->getContentType());
    $webhooks_service
      ->send($webhook_config, $webhook);
  }
}

/**
 * Implements hook_user_logout().
 */
function webhooks_user_logout(AccountInterface $account) {

  /** @var \Drupal\webhooks\WebhooksService $webhooks_service */
  $webhooks_service = \Drupal::service('webhooks.service');

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  $event = implode(':', [
    'system',
    'user_logout',
  ]);
  $user = User::load($account
    ->id());
  $webhook_configs = $webhooks_service
    ->loadMultipleByEvent($event);

  /** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
  foreach ($webhook_configs as $webhook_config) {
    $webhook = new Webhook([
      'event' => $event,
      'user' => $serializer
        ->normalize($user),
    ], [], $event, $webhook_config
      ->getContentType());
    $webhooks_service
      ->send($webhook_config, $webhook);
  }
}

/**
 * Implements hook_cache_flush().
 */
function webhooks_cache_flush() {

  /** @var \Drupal\webhooks\WebhooksService $webhooks_service */
  $webhooks_service = \Drupal::service('webhooks.service');
  $event = implode(':', [
    'system',
    'cache_flush',
  ]);
  $webhook_configs = $webhooks_service
    ->loadMultipleByEvent($event);

  /** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
  foreach ($webhook_configs as $webhook_config) {
    $webhook = new Webhook([
      'event' => $event,
    ], [], $event, $webhook_config
      ->getContentType());
    $webhooks_service
      ->send($webhook_config, $webhook);
  }
}