You are here

class SyncCoreBatchCollection in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/SyncCoreBatchCollection.php \Drupal\cms_content_sync\SyncCoreBatchCollection
  2. 2.0.x src/SyncCoreBatchCollection.php \Drupal\cms_content_sync\SyncCoreBatchCollection

Class SyncCoreBatchCollection.

Collect a number of requests to be executed as a batch operation.

Hierarchy

Expanded class hierarchy of SyncCoreBatchCollection

File

src/SyncCoreBatchCollection.php, line 10

Namespace

Drupal\cms_content_sync
View source
class SyncCoreBatchCollection {
  protected $operations = [];

  /**
   * Get an existing item by ID.
   *
   * @param $id
   * @param $type
   */
  public function get($id, $type) {
    foreach ($this->operations as $operation) {
      if ($operation['type'] !== $type) {
        continue;
      }
      if ($operation['item']['id'] !== $id) {
        continue;
      }
      return $operation['item'];
    }
    return null;
  }

  /**
   * Add or overwrite an item if one exists with the given ID.
   *
   * @param array  $item
   * @param string $type
   *
   * @return $this
   */
  public function add($item, $type) {

    // If an item with that ID already exists, we overwrite it.
    foreach ($this->operations as &$operation) {
      if ($operation['type'] !== $type) {
        continue;
      }
      if ($operation['item']['id'] !== $item['id']) {
        continue;
      }
      $operation['item'] = $item;
      return $this;
    }
    $this->operations[] = [
      'type' => $type,
      'item' => $item,
    ];
    return $this;
  }

  /**
   * @param array $items
   * @param bool  $prepend
   */
  public function addMultiple($items, $prepend = false) {
    $this->operations = $prepend ? array_merge($items, $this->operations) : array_merge($this->operations, $items);
  }

  /**
   * @return array
   */
  public function getOperations() {
    return $this->operations;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SyncCoreBatchCollection::$operations protected property
SyncCoreBatchCollection::add public function Add or overwrite an item if one exists with the given ID.
SyncCoreBatchCollection::addMultiple public function
SyncCoreBatchCollection::get public function Get an existing item by ID.
SyncCoreBatchCollection::getOperations public function