You are here

class ImportUpdateAssets in Acquia Content Hub 8.2

Imports and updates assets.

@package Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook

Hierarchy

  • class \Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook\ImportUpdateAssets implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ImportUpdateAssets

1 file declares its use of ImportUpdateAssets
ImportUpdateAssetsTest.php in tests/src/Kernel/ImportUpdateAssetsTest.php
1 string reference to 'ImportUpdateAssets'
acquia_contenthub_subscriber.services.yml in modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
1 service uses ImportUpdateAssets
acquia_contenthub_subscriber.handle_webhook.import_assets in modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook\ImportUpdateAssets

File

modules/acquia_contenthub_subscriber/src/EventSubscriber/HandleWebhook/ImportUpdateAssets.php, line 21

Namespace

Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook
View source
class ImportUpdateAssets implements EventSubscriberInterface {

  /**
   * The queue object.
   *
   * @var \Drupal\Core\Queue\QueueInterface
   */
  protected $queue;

  /**
   * Event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $dispatcher;

  /**
   * The subscription tracker.
   *
   * @var \Drupal\acquia_contenthub_subscriber\SubscriberTracker
   */
  protected $tracker;

  /**
   * The acquia_contenthub logger channel.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $channel;

  /**
   * Acquia ContentHub Admin Settings Config.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * Client CDF object.
   *
   * @var \Acquia\ContentHubClient\CDF\ClientCDFObject
   */
  protected $clientCDFObject;

  /**
   * ImportUpdateAssets constructor.
   *
   * @param \Drupal\Core\Queue\QueueFactory $queue
   *   The queue factory.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
   *   Event dispatcher.
   * @param \Drupal\acquia_contenthub_subscriber\SubscriberTracker $tracker
   *   The subscription tracker.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger_channel
   *   The logger channel factory.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  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');
  }

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

  /**
   * Handles webhook events.
   *
   * @param \Drupal\acquia_contenthub\Event\HandleWebhookEvent $event
   *   The HandleWebhookEvent object.
   *
   * @throws \Exception
   */
  public function onHandleWebhook(HandleWebhookEvent $event) {

    // @todo Would be nice to have one place with statuses list - $payload['status'].
    // @todo The same regarding $payload['crud'] and supported types ($asset['type']).
    $payload = $event
      ->getPayload();
    $client = $event
      ->getClient();

    // Nothing to do or log here.
    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()) {

      // Only log if we're trying to update something other than client objects.
      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;
      }

      // Add entities to interest list.
      $client
        ->addEntitiesToInterestList($client
        ->getSettings()
        ->getWebhook('uuid'), $uuids);

      // Update Client CDF metrics.
      $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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImportUpdateAssets::$channel protected property The acquia_contenthub logger channel.
ImportUpdateAssets::$clientCDFObject protected property Client CDF object.
ImportUpdateAssets::$config protected property Acquia ContentHub Admin Settings Config.
ImportUpdateAssets::$dispatcher protected property Event dispatcher.
ImportUpdateAssets::$queue protected property The queue object.
ImportUpdateAssets::$tracker protected property The subscription tracker.
ImportUpdateAssets::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ImportUpdateAssets::onHandleWebhook public function Handles webhook events.
ImportUpdateAssets::__construct public function ImportUpdateAssets constructor.