View source
<?php
namespace Drupal\acquia_contenthub_subscriber\Plugin\QueueWorker;
use Drupal\acquia_contenthub\Client\ClientFactory;
use Drupal\acquia_contenthub\ContentHubCommonActions;
use Drupal\acquia_contenthub_subscriber\Exception\ContentHubImportException;
use Drupal\acquia_contenthub_subscriber\SubscriberTracker;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ContentHubImportQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {
protected $dispatcher;
protected $common;
protected $factory;
protected $client;
protected $tracker;
protected $achLoggerChannel;
protected $configFactory;
public function __construct(EventDispatcherInterface $dispatcher, ContentHubCommonActions $common, ClientFactory $factory, SubscriberTracker $tracker, LoggerChannelFactoryInterface $logger_factory, ConfigFactoryInterface $config_factory, array $configuration, $plugin_id, $plugin_definition) {
$this->common = $common;
if (!empty($this->common
->getUpdateDbStatus())) {
throw new \Exception("Site has pending database updates. Apply these updates before importing content.");
}
$this->dispatcher = $dispatcher;
$this->factory = $factory;
$this->tracker = $tracker;
$this->achLoggerChannel = $logger_factory
->get('acquia_contenthub_subscriber');
$this->configFactory = $config_factory;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('event_dispatcher'), $container
->get('acquia_contenthub_common_actions'), $container
->get('acquia_contenthub.client.factory'), $container
->get('acquia_contenthub_subscriber.tracker'), $container
->get('logger.factory'), $container
->get('config.factory'), $configuration, $plugin_id, $plugin_definition);
}
public function initializeClient() : void {
if (empty($this->client)) {
$this->client = $this->factory
->getClient();
}
}
public function processItem($data) : void {
$this
->initializeClient();
if (!$this->client) {
$this->achLoggerChannel
->error('Acquia Content Hub client cannot be initialized because connection settings are empty.');
return;
}
$settings = $this->factory
->getSettings();
$webhook = $settings
->getWebhook('uuid');
try {
$interests = $this->client
->getInterestsByWebhook($webhook);
} catch (\Exception $exception) {
$this->achLoggerChannel
->error(sprintf('Following error occurred while we were trying to get the interest list: %s', $exception
->getMessage()));
return;
}
$config = $this->configFactory
->get('acquia_contenthub.admin_settings');
$send_update = $config
->get('send_contenthub_updates') ?? TRUE;
$process_items = explode(', ', $data->uuids);
$uuids = array_intersect($process_items, $interests);
if (count($uuids) !== count($process_items)) {
$missing_uuids = array_diff($process_items, $uuids);
$this->achLoggerChannel
->info(sprintf('Skipped importing the following missing entities: %s. This occurs when entities are deleted at the Publisher before importing.', implode(', ', $missing_uuids)));
}
if (!$uuids) {
$this->achLoggerChannel
->info('There are no matching entities in the queues and the site interest list.');
return;
}
try {
$stack = $this->common
->importEntities(...$uuids);
$this->client = $this->factory
->getClient();
} catch (ContentHubImportException $e) {
$e_uuids = $e
->getUuids();
if (array_diff($uuids, $e_uuids) == array_diff($e_uuids, $uuids) && $e
->isEntitiesMissing()) {
if ($webhook) {
foreach ($uuids as $uuid) {
try {
if (!$this->tracker
->getEntityByRemoteIdAndHash($uuid)) {
if ($send_update) {
$this->client
->deleteInterest($uuid, $webhook);
}
$this->tracker
->delete($uuid);
$this->achLoggerChannel
->info(sprintf('The following entity was deleted from interest list and tracking table: %s', $uuid));
}
} catch (\Exception $ex) {
$this->achLoggerChannel
->error(sprintf('Entity deletion from tracking table and interest list failed. Entity: %s. Message: %s', $uuid, $ex
->getMessage()));
}
return;
}
}
}
else {
$this->achLoggerChannel
->error(sprintf('Import failed: %s.', $e
->getMessage()));
throw $e;
}
}
if ($webhook && $send_update) {
try {
$this->client
->addEntitiesToInterestList($webhook, array_keys($stack
->getDependencies()));
$this->achLoggerChannel
->info(sprintf('The following imported entities have been added to the interest list on Content Hub for webhook "%s": [%s].', $webhook, implode(', ', $uuids)));
} catch (\Exception $e) {
$this->achLoggerChannel
->error(sprintf('Error adding the following entities to the interest list for webhook "%s": [%s]. Error message: "%s".', $webhook, implode(', ', $uuids), $e
->getMessage()));
}
}
}
}