You are here

class Webhook in Fastly 8.3

Class Webhook.

@package Drupal\fastly\Services

Hierarchy

  • class \Drupal\fastly\Services\Webhook

Expanded class hierarchy of Webhook

7 files declare their use of Webhook
Api.php in src/Api.php
FastlySettingsForm.php in src/Form/FastlySettingsForm.php
ImageOptimizerForm.php in src/Form/ImageOptimizerForm.php
PurgeOptionsForm.php in src/Form/PurgeOptionsForm.php
StaleContentOptionsForm.php in src/Form/StaleContentOptionsForm.php

... See full list

1 string reference to 'Webhook'
fastly.services.yml in ./fastly.services.yml
fastly.services.yml
1 service uses Webhook
fastly.services.webhook in ./fastly.services.yml
Drupal\fastly\Services\Webhook

File

src/Services/Webhook.php, line 14

Namespace

Drupal\fastly\Services
View source
class Webhook {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * The Fastly logger channel.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The HTTP client.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * Connect timeout.
   *
   * @var string
   */
  protected $webhookConnectTimeout;

  /**
   * Webhook constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config.
   * @param \GuzzleHttp\ClientInterface $httpClient
   *   The HTTP client.
   * @param \Psr\Log\LoggerInterface $logger
   *   The Fastly logger channel.
   * @param string $webhookConnectTimeout
   *   The timeout for webhook connections.
   */
  public function __construct(ConfigFactoryInterface $configFactory, ClientInterface $httpClient, LoggerInterface $logger, $webhookConnectTimeout) {
    $this->config = $configFactory
      ->get('fastly.settings');
    $this->httpClient = $httpClient;
    $this->logger = $logger;
    $this->webhookConnectTimeout = $webhookConnectTimeout;
  }

  /**
   * Sends request to WebHookURL.
   *
   * @param string $message
   *   Webhook message, pass through t() or SafeMarkup::format first.
   * @param string $type
   *   Webhook type.
   *
   * @return mixed
   *   FALSE if webhook notification are disabled or an unsupported type.
   */
  public function sendWebHook($message, $type) {
    if (!$this->config
      ->get('webhook_enabled') || !in_array($type, $this->config
      ->get('webhook_notifications'))) {
      return FALSE;
    }
    $text = $message;
    $headers = [
      'Content-type: application/json',
    ];
    $body = [
      "text" => $text,
      "username" => "fastly-drupal-bot",
      "icon_emoji" => ":airplane:",
    ];
    $option = [
      "headers" => $headers,
      "connect_timeout" => $this->webhookConnectTimeout,
      "json" => $body,
    ];

    // @TODO: handle exceptions.
    $this->httpClient
      ->request("POST", $this->config
      ->get('webhook_url'), $option);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Webhook::$config protected property The config factory.
Webhook::$httpClient protected property The HTTP client.
Webhook::$logger protected property The Fastly logger channel.
Webhook::$webhookConnectTimeout protected property Connect timeout.
Webhook::sendWebHook public function Sends request to WebHookURL.
Webhook::__construct public function Webhook constructor.