PreviewEntity.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_preview/src/EventSubscriber/HandleWebhook/PreviewEntity.php
View source
<?php
namespace Drupal\acquia_contenthub_preview\EventSubscriber\HandleWebhook;
use Acquia\ContentHubClient\CDF\CDFObject;
use Acquia\ContentHubClient\CDFDocument;
use Acquia\Hmac\ResponseSigner;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\ContentHubCommonActions;
use Drupal\acquia_contenthub\Event\HandleWebhookEvent;
use Drupal\Core\Url;
use GuzzleHttp\Psr7\Response;
use Laminas\Diactoros\ResponseFactory;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\StreamFactory;
use Laminas\Diactoros\UploadedFileFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PreviewEntity implements EventSubscriberInterface {
protected $common;
public function __construct(ContentHubCommonActions $common) {
$this->common = $common;
}
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::HANDLE_WEBHOOK][] = [
'onHandleWebhook',
1000,
];
return $events;
}
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();
}
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;
}
}