AvatarKitEntityHandler.php in Avatar Kit 8.2        
                          
                  
                        
  
  
  
  
  
File
  src/AvatarKitEntityHandler.php
  
    View source  
  <?php
namespace Drupal\avatars;
use Drupal\avatars\Entity\AvatarCacheInterface;
use Drupal\avatars\Exception\AvatarKitEntityAvatarIdentifierException;
use Drupal\avatars\Plugin\Avatars\Service\AvatarKitServiceInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class AvatarKitEntityHandler implements AvatarKitEntityHandlerInterface {
  
  protected $serviceStorage;
  
  protected $entityLocalCache;
  
  protected $preferenceManager;
  
  public function __construct(EntityTypeManagerInterface $entityTypeManager, AvatarKitLocalCache $entityLocalCache, AvatarKitEntityPreferenceManagerInterface $preferenceManager) {
    $this->serviceStorage = $entityTypeManager
      ->getStorage('avatars_service');
    $this->entityLocalCache = $entityLocalCache;
    $this->preferenceManager = $preferenceManager;
  }
  
  public function findFirst(EntityInterface $entity) : ?AvatarCacheInterface {
    $all = $this
      ->findAll($entity);
    $current = $all
      ->current();
    return $current;
  }
  
  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;
      }
      
      $cache = $this->entityLocalCache
        ->getLocalCache($service_id, $identifier);
      if ($cache) {
        
        if ($cache
          ->getAvatar()) {
          (yield $service_id => $cache);
        }
        continue;
      }
      
      $uri = $service_plugin
        ->getAvatar($identifier);
      $args = [
        $service_id,
        $uri,
        $identifier,
      ];
      if ($uri) {
        
        $plugin_supports_local = $service_plugin
          ->getPluginDefinition()['files'] ?? FALSE;
        if ($plugin_supports_local) {
          $cache = $this->entityLocalCache
            ->cacheLocalFileEntity(...$args);
          if ($cache) {
            (yield $service_id => $cache);
            continue;
          }
        }
        
        $cache = $cache ?? $this->entityLocalCache
          ->cacheRemote(...$args);
        if ($cache) {
          (yield $service_id => $cache);
          continue;
        }
      }
      
      $this->entityLocalCache
        ->cacheEmpty(...$args);
    }
  }
  
  public static function createEntityIdentifier(AvatarKitServiceInterface $service, EntityInterface $entity) : EntityAvatarIdentifierInterface {
    $identifier = $service
      ->createIdentifier();
    $new_identifier = new EntityAvatarIdentifierProxy($identifier);
    $new_identifier
      ->setEntity($entity);
    return $new_identifier;
  }
}