ReExport.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_publisher/src/EventSubscriber/HandleWebhook/ReExport.php
View source
<?php
namespace Drupal\acquia_contenthub_publisher\EventSubscriber\HandleWebhook;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\HandleWebhookEvent;
use Drupal\acquia_contenthub_publisher\PublisherActions;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ReExport implements EventSubscriberInterface {
use HandleWebhookTrait;
protected $publisherActions;
protected $entityRepository;
protected $channel;
public function __construct(PublisherActions $publisher_actions, EntityRepositoryInterface $entity_repository, LoggerChannelFactoryInterface $logger_factory) {
$this->publisherActions = $publisher_actions;
$this->entityRepository = $entity_repository;
$this->channel = $logger_factory
->get('acquia_contenthub_publisher');
}
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['uuid']) || 'republish' !== $payload['crud'] || $payload['initiator'] === $client_uuid || empty($payload['cdf'])) {
return;
}
$uuid = $payload['cdf']['uuid'];
$type = $payload['cdf']['type'];
$dependencies = $payload['cdf']['dependencies'] ?? [];
try {
$entity = $this->entityRepository
->loadEntityByUuid($type, $uuid);
} catch (\Exception $exception) {
$entity = FALSE;
}
if (!$entity) {
$body = sprintf('The entity "%s:%s" could not be found and thus cannot be re-exported from a webhook request by origin = %s.', $type, $uuid, $payload['initiator']);
$this->channel
->error($body);
$response = $this
->getResponse($event, $body, 404);
}
else {
$this->publisherActions
->reExportEntityFull($entity, $dependencies);
$body = sprintf('Entity "%s/%s" successfully enqueued for export from webhook UUID = %s by origin = %s.', $type, $uuid, $payload['uuid'], $payload['initiator']);
$this->channel
->info($body);
$response = $this
->getResponse($event, $body, 200);
}
$event
->setResponse($response);
$event
->stopPropagation();
}
}
Classes
Name |
Description |
ReExport |
Re-exports an entity and all its dependencies on a webhook request. |