You are here

public function PullIntent::execute in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/PullIntent.php \Drupal\cms_content_sync\PullIntent::execute()
  2. 2.0.x src/PullIntent.php \Drupal\cms_content_sync\PullIntent::execute()

Pull the provided entity.

Return value

bool

Throws

Exception\SyncException

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

Overrides SyncIntent::execute

File

src/PullIntent.php, line 256

Class

PullIntent

Namespace

Drupal\cms_content_sync

Code

public function execute() {
  $pull = $this->pool
    ->getNewestTimestamp($this->entityType, $this->uuid, true);
  if (!$pull) {
    if (SyncIntent::ACTION_UPDATE == $this->action) {
      $this->action = SyncIntent::ACTION_CREATE;
    }
  }
  elseif (SyncIntent::ACTION_CREATE == $this->action) {
    $this->action = SyncIntent::ACTION_UPDATE;
  }
  $pull = time();
  $config = $this->flow
    ->getEntityTypeConfig($this->entityType, $this->bundle);
  $handler = $this->flow
    ->getEntityTypeHandler($config);
  self::entityHasBeenPulledFromRemoteSite($this->entityType, $this->uuid, true);
  $result = $handler
    ->pull($this);
  \Drupal::logger('cms_content_sync')
    ->info('@not PULL @action @entity_type:@bundle @uuid @reason: @message<br>Flow: @flow_id | Pool: @pool_id', [
    '@reason' => $this->reason,
    '@action' => $this->action,
    '@entity_type' => $this->entityType,
    '@bundle' => $this->bundle,
    '@uuid' => $this->uuid,
    '@not' => $result ? '' : 'NO',
    '@message' => $result ? t('The entity has been pulled.') : t('The entity handler denied to pull this entity.'),
    '@flow_id' => $this
      ->getFlow()
      ->id(),
    '@pool_id' => $this
      ->getPool()
      ->id(),
  ]);

  // Don't save entity_status entity if entity wasn't pulled anyway.
  if (!$result) {
    return false;
  }

  // Need to save after setting timestamp to prevent exception.
  $this->entity_status
    ->setLastPull($pull);
  $this->pool
    ->setTimestamp($this->entityType, $this->uuid, $pull, true);
  $this->entity_status
    ->isDeleted(SyncIntent::ACTION_DELETE == $this->action);
  $this->entity_status
    ->save();
  if (SyncIntent::ACTION_DELETE == $this->action) {
    $this->pool
      ->markDeleted($this->entityType, $this->uuid);
  }
  $entity = $this
    ->getEntity();

  // Dispatch EntityExport event to give other modules the possibility to react on it.
  // Ignore deleted entities.
  if ($entity) {
    \Drupal::service('event_dispatcher')
      ->dispatch(AfterEntityPull::EVENT_NAME, new AfterEntityPull($entity, $this));
  }

  // Handle Extended Entity Import logging.
  $settings = ContentSyncSettings::getInstance();
  if ($settings
    ->getExtendedEntityImportLogging()) {
    $url = null;
    if ($entity && $entity
      ->hasLinkTemplate('canonical') && !$entity instanceof FieldCollectionItem) {
      $url = $entity
        ->toUrl('canonical', [
        'absolute' => true,
      ])
        ->toString(true)
        ->getGeneratedUrl();
    }
    $serializer = \Drupal::service('serializer');
    $data = $serializer
      ->serialize($this
      ->getOperation()
      ->getResponseBody($url), 'json', [
      'plugin_id' => 'entity',
    ]);
    \Drupal::logger('cms_content_sync_entity_import_log')
      ->debug('%entity_type - %uuid <br>Data: <br><pre><code>%data</code></pre>', [
      '%entity_type' => $entity
        ->getEntityTypeId(),
      '%uuid' => $entity
        ->uuid(),
      '%data' => $data,
    ]);
  }
  $this
    ->resolveMissingDependencies();
  return true;
}