You are here

final class AppCacheByOwner in Apigee Edge 8

Default cache store for apps of a specific owner.

The owner could be a developer or a company.

See the interface definition for more details.

All developers and companies have a dedicated instance from this cache. (Therefore app names are unique as cache ids here.)

@internal Do not create an instance from this directly. Always use the factory.

Hierarchy

Expanded class hierarchy of AppCacheByOwner

File

src/Entity/Controller/Cache/AppCacheByOwner.php, line 39

Namespace

Drupal\apigee_edge\Entity\Controller\Cache
View source
final class AppCacheByOwner implements AppCacheByOwnerInterface {

  /**
   * Indicates whether all entities in the cache or not.
   *
   * @var bool
   */
  private $allEntitiesInCache = FALSE;

  /**
   * The app cache service that stores app by their app id (UUID).
   *
   * @var \Drupal\apigee_edge\Entity\Controller\Cache\AppCacheInterface
   */
  private $appCache;

  /**
   * Dedicated cache instance that stores a specific owner app names.
   *
   * This cache is used by the getEntityIds() method on developer- and company
   * app controllers.
   *
   * @var \Drupal\apigee_edge\Entity\Controller\Cache\EntityIdCacheInterface
   */
  private $appNameCache;

  /**
   * Developer id (UUID), email address or a company's company name.
   *
   * @var string
   */
  private $owner;

  /**
   * AppCacheByAppOwner constructor.
   *
   * @param string $owner
   *   Developer id (UUID), email address or a company's company name.
   * @param \Drupal\apigee_edge\Entity\Controller\Cache\AppCacheInterface $app_cache
   *   The app cache service that stores app by their app id (UUID).
   * @param \Drupal\apigee_edge\Entity\Controller\Cache\AppNameCacheByOwnerFactoryInterface $app_name_cache_by_owner
   *   Dedicated cache instance that stores a specific owner app names.
   */
  public function __construct(string $owner, AppCacheInterface $app_cache, AppNameCacheByOwnerFactoryInterface $app_name_cache_by_owner) {
    $this->appCache = $app_cache;
    $this->appNameCache = $app_name_cache_by_owner
      ->getAppNameCache($owner);
    $this->owner = $owner;
  }

  /**
   * {@inheritdoc}
   */
  public function saveEntities(array $entities) : void {
    $this->appCache
      ->saveEntities($entities);

    // $entity->id() returns app names so this is fine.
    $this->appNameCache
      ->saveEntities($entities);
  }

  /**
   * {@inheritdoc}
   */
  public function removeEntities(array $ids) : void {
    $app_ids = $this
      ->getAppIdsByAppNames($ids);
    $all_app_entities = $this
      ->getEntities();
    $this->appCache
      ->removeEntities($app_ids);
    $this->appNameCache
      ->removeIds($ids);
    if (count($app_ids) === count($all_app_entities)) {
      $this
        ->allEntitiesInCache(FALSE);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getEntities(array $ids = []) : array {

    // If $ids is empty all entities should be returned.
    if (empty($ids)) {
      $apps = $this->appCache
        ->getAppsByOwner($this->owner);
      return $apps ?? [];
    }
    return $this
      ->getAppsByAppNames($ids);
  }

  /**
   * {@inheritdoc}
   */
  public function getEntity(string $id) : ?EntityInterface {
    $entities = $this
      ->getEntities([
      $id,
    ]);
    return $entities ? reset($entities) : NULL;
  }

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

  /**
   * {@inheritdoc}
   */
  public function isAllEntitiesInCache() : bool {
    return $this->allEntitiesInCache;
  }

  /**
   * Returns the app ids from the app cache for the given owner and app names.
   *
   * @param array $names
   *   Array of app names.
   *
   * @return array
   *   Array of app ids (UUIDs).
   */
  private function getAppIdsByAppNames(array $names) : array {
    return array_map(function (AppInterface $app) {
      return $app
        ->getAppId();
    }, $this
      ->getAppsByAppNames($names));
  }

  /**
   * Returns the apps from the app cache for the given owner and app names.
   *
   * @param array $names
   *   Array of app names.
   *
   * @return \Apigee\Edge\Api\Management\Entity\AppInterface[]
   *   Array of apps.
   */
  private function getAppsByAppNames(array $names) : array {
    $apps = [];
    $apps_by_owner = $this->appCache
      ->getAppsByOwner($this->owner);

    // There is nothing to invalidate.
    if ($apps_by_owner === NULL) {
      return $apps;
    }
    return array_filter($apps_by_owner, function (AppInterface $app) use ($names) {
      return in_array($app
        ->getName(), $names);
    });
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AppCacheByOwner::$allEntitiesInCache private property Indicates whether all entities in the cache or not.
AppCacheByOwner::$appCache private property The app cache service that stores app by their app id (UUID).
AppCacheByOwner::$appNameCache private property Dedicated cache instance that stores a specific owner app names.
AppCacheByOwner::$owner private property Developer id (UUID), email address or a company's company name.
AppCacheByOwner::allEntitiesInCache public function Changes whether all entities in the cache or not. Overrides EntityCacheInterface::allEntitiesInCache
AppCacheByOwner::getAppIdsByAppNames private function Returns the app ids from the app cache for the given owner and app names.
AppCacheByOwner::getAppsByAppNames private function Returns the apps from the app cache for the given owner and app names.
AppCacheByOwner::getEntities public function Returns entities from the cache. Overrides EntityCacheInterface::getEntities
AppCacheByOwner::getEntity public function Returns an entity from the cache by its id. Overrides EntityCacheInterface::getEntity
AppCacheByOwner::isAllEntitiesInCache public function Returns whether all entities in cache or not. Overrides EntityCacheInterface::isAllEntitiesInCache
AppCacheByOwner::removeEntities public function Removes entities from the cache by their ids. Overrides EntityCacheInterface::removeEntities
AppCacheByOwner::saveEntities public function Saves entities to the cache. Overrides EntityCacheInterface::saveEntities
AppCacheByOwner::__construct public function AppCacheByAppOwner constructor.