You are here

public function AvatarKitEntityHandler::findAll in Avatar Kit 8.2

Iterates through all avatar services for a user.

Downloads and caches the avatar locally, then produces an avatar cache for each.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Get the avatars for this entity.

Return value

\Generator|\Drupal\avatars\Entity\AvatarCacheInterface[] A generator where keys are service plugin ID's and values are avatar cache entities.

Overrides AvatarKitEntityHandlerInterface::findAll

1 call to AvatarKitEntityHandler::findAll()
AvatarKitEntityHandler::findFirst in src/AvatarKitEntityHandler.php
Find the first valid avatar for an entity.

File

src/AvatarKitEntityHandler.php, line 65

Class

AvatarKitEntityHandler
Downloads and caches avatars into entities.

Namespace

Drupal\avatars

Code

public function findAll(EntityInterface $entity) : \Generator {
  $service_ids = $this->preferenceManager
    ->getPreferences($entity);
  foreach ($this->serviceStorage
    ->loadMultipleGenerator($service_ids) as $service_id => $service_plugin) {
    try {
      $identifier = $this
        ->createEntityIdentifier($service_plugin, $entity);
    } catch (AvatarKitEntityAvatarIdentifierException $e) {
      continue;
    }

    // Check if the avatar for this entity service already exists.
    $cache = $this->entityLocalCache
      ->getLocalCache($service_id, $identifier);
    if ($cache) {

      // Yield if there is a file.
      if ($cache
        ->getAvatar()) {
        (yield $service_id => $cache);
      }
      continue;
    }

    // A new cache needs to be created:
    $uri = $service_plugin
      ->getAvatar($identifier);
    $args = [
      $service_id,
      $uri,
      $identifier,
    ];
    if ($uri) {

      // Try local.
      $plugin_supports_local = $service_plugin
        ->getPluginDefinition()['files'] ?? FALSE;
      if ($plugin_supports_local) {
        $cache = $this->entityLocalCache
          ->cacheLocalFileEntity(...$args);
        if ($cache) {
          (yield $service_id => $cache);
          continue;
        }
      }

      // Try remote.
      $cache = $cache ?? $this->entityLocalCache
        ->cacheRemote(...$args);
      if ($cache) {
        (yield $service_id => $cache);
        continue;
      }
    }

    // Else empty.
    // Don't yield the cache because it isn't considered *successful*.
    $this->entityLocalCache
      ->cacheEmpty(...$args);
  }
}