You are here

class EntityIdCache in Apigee Edge 8

Base definition of the entity id cache service used by controllers.

See the interface definition for more details.

Always create a dedicated instance from this for an entity type!

@internal

Hierarchy

Expanded class hierarchy of EntityIdCache

1 string reference to 'EntityIdCache'
apigee_edge.services.yml in ./apigee_edge.services.yml
apigee_edge.services.yml
1 service uses EntityIdCache
apigee_edge.controller.cache.entity_id_cache in ./apigee_edge.services.yml
Drupal\apigee_edge\Entity\Controller\Cache\EntityIdCache

File

src/Entity/Controller/Cache/EntityIdCache.php, line 34

Namespace

Drupal\apigee_edge\Entity\Controller\Cache
View source
class EntityIdCache implements EntityIdCacheInterface {

  /**
   * Stored entity ids.
   *
   * As an associative array where keys and values as the same.
   *
   * @var string[]
   */
  private $ids = [];

  /**
   * Indicates whether all entity ids in cache all or not.
   *
   * @var bool
   */
  private $allIdsInCache = FALSE;

  /**
   * {@inheritdoc}
   */
  public final function saveIds(array $ids) : void {

    // Store entity ids in an associative array because it is easier to work
    // with them.
    $this->ids += array_combine($ids, $ids);
  }

  /**
   * Allows to perform additional tasks after entity ids got saved to cache.
   *
   * @param array $ids
   *   Array of entity ids.
   */
  protected function doSaveIds(array $ids) {
  }

  /**
   * {@inheritdoc}
   */
  public final function saveEntities(array $entities) : void {
    $ids = array_map(function (EntityInterface $entity) {
      return $this
        ->getEntityId($entity);
    }, $entities);
    $this
      ->saveIds($ids);
  }

  /**
   * {@inheritdoc}
   */
  public final function removeIds(array $ids) : void {
    $this->ids = array_diff($this->ids, $ids);

    // If ids is empty now, reset the state. Cache can be marked as "complete"
    // still by calling the setter method if needed.
    if (empty($this->ids)) {
      $this->allIdsInCache = FALSE;
    }
    $this
      ->doRemoveIds($ids);
  }

  /**
   * Allows to perform additional tasks after entity ids got deleted from cache.
   *
   * @param array $ids
   *   Array of entity ids.
   */
  protected function doRemoveIds(array $ids) {
  }

  /**
   * {@inheritdoc}
   */
  public final function getIds() : array {

    // We return a non-associative array because this is what getEntityIds()
    // returns.
    return array_values($this->ids);
  }

  /**
   * {@inheritdoc}
   */
  public final function allIdsInCache(bool $all_ids_in_cache) : void {
    $this->allIdsInCache = $all_ids_in_cache;
  }

  /**
   * {@inheritdoc}
   */
  public final function isAllIdsInCache() : bool {
    return $this->allIdsInCache;
  }

  /**
   * Returns the unique id of an entity that getEntityIds() returns as well.
   *
   * @param \Apigee\Edge\Entity\EntityInterface $entity
   *   Entity object.
   *
   * @return string
   *   Unique id from the entity that getEntityIds() returns as well.
   */
  protected function getEntityId(EntityInterface $entity) : string {
    return $entity
      ->id();
  }

  /**
   * Prevents data stored in entity id cache from being serialized.
   */
  public function __sleep() {
    return [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityIdCache::$allIdsInCache private property Indicates whether all entity ids in cache all or not.
EntityIdCache::$ids private property Stored entity ids.
EntityIdCache::allIdsInCache final public function Changes whether all ids in the cache or not. Overrides EntityIdCacheInterface::allIdsInCache
EntityIdCache::doRemoveIds protected function Allows to perform additional tasks after entity ids got deleted from cache.
EntityIdCache::doSaveIds protected function Allows to perform additional tasks after entity ids got saved to cache.
EntityIdCache::getEntityId protected function Returns the unique id of an entity that getEntityIds() returns as well. 2
EntityIdCache::getIds final public function Returns cached ids. Overrides EntityIdCacheInterface::getIds
EntityIdCache::isAllIdsInCache final public function Returns whether all entity ids in cache or not. Overrides EntityIdCacheInterface::isAllIdsInCache
EntityIdCache::removeIds final public function Removes ids from the cache. Overrides EntityIdCacheInterface::removeIds
EntityIdCache::saveEntities final public function Adds entities to the cache. Overrides EntityIdCacheInterface::saveEntities
EntityIdCache::saveIds final public function Adds entity ids to the cache. Overrides EntityIdCacheInterface::saveIds
EntityIdCache::__sleep public function Prevents data stored in entity id cache from being serialized.