View source
<?php
namespace Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook;
use Acquia\ContentHubClient\CDF\ClientCDFObject;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\BuildClientCdfEvent;
use Drupal\acquia_contenthub\Event\HandleWebhookEvent;
use Drupal\acquia_contenthub_subscriber\SubscriberTracker;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Queue\QueueFactory;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ImportUpdateAssets implements EventSubscriberInterface {
protected $queue;
protected $dispatcher;
protected $tracker;
protected $channel;
protected $config;
protected $clientCDFObject;
public function __construct(QueueFactory $queue, EventDispatcherInterface $dispatcher, SubscriberTracker $tracker, LoggerChannelInterface $logger_channel, ConfigFactoryInterface $config_factory) {
$this->queue = $queue
->get('acquia_contenthub_subscriber_import');
$this->dispatcher = $dispatcher;
$this->tracker = $tracker;
$this->channel = $logger_channel;
$this->config = $config_factory
->get('acquia_contenthub.admin_settings');
}
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::HANDLE_WEBHOOK][] = 'onHandleWebhook';
return $events;
}
public function onHandleWebhook(HandleWebhookEvent $event) {
$payload = $event
->getPayload();
$client = $event
->getClient();
if ($payload['crud'] !== 'update') {
return;
}
if ($payload['status'] !== 'successful' || !isset($payload['assets']) || !count($payload['assets'])) {
$this->channel
->info('Payload will not be processed because it is not successful or it does not have assets.
Payload data: @payload', [
'@payload' => print_r($payload, TRUE),
]);
return;
}
if ($payload['initiator'] === $client
->getSettings()
->getUuid()) {
if ($payload['assets'][0]['type'] !== 'client') {
$this->channel
->info('Payload will not be processed because its initiator is the existing client.
Payload data: @payload', [
'@payload' => print_r($payload, TRUE),
]);
}
return;
}
$uuids = [];
$types = [
'drupal8_content_entity',
'drupal8_config_entity',
];
foreach ($payload['assets'] as $asset) {
$uuid = $asset['uuid'];
$type = $asset['type'];
if (!in_array($type, $types)) {
$this->channel
->info('Entity with UUID @uuid was not added to the import queue because it has an unsupported type: @type', [
'@uuid' => $uuid,
'@type' => $type,
]);
continue;
}
if ($this->tracker
->isTracked($uuid)) {
$status = $this->tracker
->getStatusByUuid($uuid);
if ($status === SubscriberTracker::AUTO_UPDATE_DISABLED) {
$this->channel
->info('Entity with UUID @uuid was not added to the import queue because it has auto update disabled.', [
'@uuid' => $uuid,
]);
continue;
}
}
$uuids[] = $uuid;
$this->tracker
->queue($uuid);
$this->channel
->info('Attempting to add entity with UUID @uuid to the import queue.', [
'@uuid' => $uuid,
]);
}
if ($uuids) {
$item = new \stdClass();
$item->uuids = implode(', ', $uuids);
$queue_id = $this->queue
->createItem($item);
if (empty($queue_id)) {
return;
}
$this->tracker
->setQueueItemByUuids($uuids, $queue_id);
$this->channel
->info('Entities with UUIDs @uuids added to the import queue and to the tracking table.', [
'@uuids' => print_r($uuids, TRUE),
]);
if (!($this->config
->get('send_contenthub_updates') ?? TRUE)) {
return;
}
$client
->addEntitiesToInterestList($client
->getSettings()
->getWebhook('uuid'), $uuids);
$settings = $client
->getSettings();
$event = new BuildClientCdfEvent(ClientCDFObject::create($settings
->getUuid(), [
'settings' => $settings
->toArray(),
]));
$this->dispatcher
->dispatch(AcquiaContentHubEvents::BUILD_CLIENT_CDF, $event);
$this->clientCDFObject = $event
->getCdf();
$client
->putEntities($this->clientCDFObject);
}
}
}