You are here

public function PullEntity::post in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Plugin/rest/resource/PullEntity.php \Drupal\cms_content_sync\Plugin\rest\resource\PullEntity::post()
  2. 2.0.x src/Plugin/rest/resource/PullEntity.php \Drupal\cms_content_sync\Plugin\rest\resource\PullEntity::post()

Responds to entity POST requests.

Return value

\Symfony\Component\HttpFoundation\Response -

Throws

\Exception

File

src/Plugin/rest/resource/PullEntity.php, line 333

Class

PullEntity
Provides entity interfaces for Content Sync, allowing the manual pull dashboard to query preview entities and to pull them to the site.

Namespace

Drupal\cms_content_sync\Plugin\rest\resource

Code

public function post(string $pool_id, string $entity_type_name, string $bundle_name, string $shared_entity_id) {
  $pool = Pool::getAll()[$pool_id];
  if (!$pool) {
    return new ResourceResponse([
      'message' => "Unknown pool ID {$pool_id}.",
    ], self::CODE_NOT_FOUND);
  }
  $preview_item = null;
  foreach (Flow::getAll() as $flow) {
    foreach ($flow
      ->getEntityTypeConfig() as $definition) {
      if (!$flow
        ->canPullEntity($definition['entity_type_name'], $definition['bundle_name'], PullIntent::PULL_MANUALLY)) {
        continue;
      }
      if ($definition['entity_type_name'] != $entity_type_name) {
        continue;
      }
      if ($definition['bundle_name'] != $bundle_name) {
        continue;
      }
      if (Pool::POOL_USAGE_ALLOW != $definition['import_pools'][$pool->id]) {
        continue;
      }
      try {
        $preview_item = $pool
          ->getClient()
          ->getSyndicationService()
          ->pullSingle($flow->id, $entity_type_name, $bundle_name, $shared_entity_id)
          ->fromPool($pool->id)
          ->manually(true)
          ->execute()
          ->getPullDashboardSearchResultItem();
        break 2;
      } catch (SyncCoreException $e) {
        return new ResourceResponse([
          'message' => 'Failed to pull entity: ' . $e
            ->getMessage(),
        ], self::CODE_UNEXPECTED_ERROR);
      }
    }
  }
  if (!$preview_item) {
    return new ResourceResponse([
      'message' => "Missing flow for pool {$pool_id}.",
    ], self::CODE_NOT_FOUND);
  }
  $data = array_merge($preview_item
    ->toArray(), $this
    ->getPreviewItemData($preview_item
    ->getType(), $preview_item
    ->getBundle(), $preview_item
    ->getId()));

  // Entity was ignored by the Sync Core for some reason or the update was rejected by Drupal.
  if (empty($data['last_import'])) {
    return new ResourceResponse([
      'message' => 'Failed to pull entity. Pull was triggered but there\'s no new status entity.',
    ], self::CODE_UNEXPECTED_ERROR);
  }
  return new ResourceResponse($data);
}