You are here

class RegisterWebhook in Acquia Content Hub 8.2

Responsible for handling site registration webhook responses.

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\HandleWebhook\RegisterWebhook implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RegisterWebhook

1 string reference to 'RegisterWebhook'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses RegisterWebhook
acquia_contenthub.handle_webhook.register in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\HandleWebhook\RegisterWebhook

File

src/EventSubscriber/HandleWebhook/RegisterWebhook.php, line 22

Namespace

Drupal\acquia_contenthub\EventSubscriber\HandleWebhook
View source
class RegisterWebhook implements EventSubscriberInterface {

  /**
   * The acquia_contenthub logger channel.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $channel;

  /**
   * RegisterWebhook constructor.
   *
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   The logger channel factory.
   */
  public function __construct(LoggerChannelFactoryInterface $logger_factory) {
    $this->channel = $logger_factory
      ->get('acquia_contenthub');
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::HANDLE_WEBHOOK][] = 'onHandleWebhook';
    return $events;
  }

  /**
   * The method called for the AcquiaContentHubEvents::HANDLE_WEBHOOK event.
   *
   * @param \Drupal\acquia_contenthub\Event\HandleWebhookEvent $event
   *   The dispatched event.
   */
  public function onHandleWebhook(HandleWebhookEvent $event) {
    $payload = $event
      ->getPayload();
    if ($payload['status'] == 'pending') {
      $client = $event
        ->getClient();
      $uuid = isset($payload['uuid']) ? $payload['uuid'] : FALSE;
      if ($uuid && $payload['publickey'] == $client
        ->getSettings()
        ->getApiKey()) {
        $response = new Response();
        if (class_exists(DiactorosFactory::class)) {
          $httpMessageFactory = new DiactorosFactory();
        }
        else {
          $httpMessageFactory = new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory());
        }
        $psr7_request = $httpMessageFactory
          ->createRequest($event
          ->getRequest());
        $signer = new ResponseSigner($event
          ->getKey(), $psr7_request);
        $signedResponse = $signer
          ->signResponse($response);
        $event
          ->setResponse($signedResponse);
        return;
      }
      else {
        $ip_address = $event
          ->getRequest()
          ->getClientIp();
        $message = new FormattableMarkup('Webhook [from IP = @IP] rejected (initiator and/or publickey do not match local settings): @whook', [
          '@IP' => $ip_address,
          '@whook' => print_r($payload, TRUE),
        ]);
        $this->channel
          ->debug($message);
        $event
          ->setResponse(new Response());
        return;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RegisterWebhook::$channel protected property The acquia_contenthub logger channel.
RegisterWebhook::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RegisterWebhook::onHandleWebhook public function The method called for the AcquiaContentHubEvents::HANDLE_WEBHOOK event.
RegisterWebhook::__construct public function RegisterWebhook constructor.