You are here

class PreviewEntity in Acquia Content Hub 8.2

Same name in this branch
  1. 8.2 modules/acquia_contenthub_preview/src/Controller/PreviewEntity.php \Drupal\acquia_contenthub_preview\Controller\PreviewEntity
  2. 8.2 modules/acquia_contenthub_preview/src/EventSubscriber/HandleWebhook/PreviewEntity.php \Drupal\acquia_contenthub_preview\EventSubscriber\HandleWebhook\PreviewEntity

Previews entities across content as a service sites.

@package Drupal\acquia_contenthub_preview\EventSubscriber\HandleWebhook

Hierarchy

  • class \Drupal\acquia_contenthub_preview\EventSubscriber\HandleWebhook\PreviewEntity implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of PreviewEntity

1 string reference to 'PreviewEntity'
acquia_contenthub_preview.services.yml in modules/acquia_contenthub_preview/acquia_contenthub_preview.services.yml
modules/acquia_contenthub_preview/acquia_contenthub_preview.services.yml
1 service uses PreviewEntity
acquia_contenthub_preview.webhook.preview in modules/acquia_contenthub_preview/acquia_contenthub_preview.services.yml
Drupal\acquia_contenthub_preview\EventSubscriber\HandleWebhook\PreviewEntity

File

modules/acquia_contenthub_preview/src/EventSubscriber/HandleWebhook/PreviewEntity.php, line 26

Namespace

Drupal\acquia_contenthub_preview\EventSubscriber\HandleWebhook
View source
class PreviewEntity implements EventSubscriberInterface {

  /**
   * The common actions object.
   *
   * @var \Drupal\acquia_contenthub\ContentHubCommonActions
   */
  protected $common;

  /**
   * PreviewEntity constructor.
   *
   * @param \Drupal\acquia_contenthub\ContentHubCommonActions $common
   *   The common actions object.
   */
  public function __construct(ContentHubCommonActions $common) {
    $this->common = $common;
  }

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

  /**
   * Handles webhook events.
   *
   * @param \Drupal\acquia_contenthub\Event\HandleWebhookEvent $event
   *   The HandleWebhookEvent object.
   *
   * @throws \Exception
   */
  public function onHandleWebhook(HandleWebhookEvent $event) : void {
    $payload = $event
      ->getPayload();
    $client = $event
      ->getClient();
    $settings = $client
      ->getSettings();
    $client_uuid = $settings
      ->getUuid();
    if ('successful' !== $payload['status'] || empty($payload['preview']) || 'preview' !== $payload['crud'] || $payload['initiator'] === $client_uuid || empty($payload['cdf'])) {
      return;
    }
    $cdf = $payload['cdf']['entities'];
    $document = new CDFDocument();
    foreach ($cdf as $object) {
      $cdfObject = CDFObject::fromArray($object);
      $document
        ->addCdfEntity($cdfObject);
    }
    $this->common
      ->importEntityCdfDocument($document);
    $url = Url::fromRoute('acquia_contenthub_preview.preview', [
      'uuid' => $payload['preview'],
    ], [
      'absolute' => TRUE,
      'base_url' => $event
        ->getRequest()
        ->getSchemeAndHttpHost(),
    ]);
    $response = $this
      ->getResponse($event, '<iframe src="' . $url
      ->toString() . '" width="1165" height="500"></iframe>');
    $event
      ->setResponse($response);
    $event
      ->stopPropagation();
  }

  /**
   * Handles webhook response.
   *
   * @param \Drupal\acquia_contenthub\Event\HandleWebhookEvent $event
   *   Handle webhook event.
   * @param string $body
   *   Body of request.
   *
   * @return \Psr\Http\Message\ResponseInterface
   *   Returns signed response.
   */
  protected function getResponse(HandleWebhookEvent $event, string $body) {
    $response = new Response(200, [], $body);
    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);
    return $signedResponse;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PreviewEntity::$common protected property The common actions object.
PreviewEntity::getResponse protected function Handles webhook response.
PreviewEntity::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
PreviewEntity::onHandleWebhook public function Handles webhook events.
PreviewEntity::__construct public function PreviewEntity constructor.