View source
<?php
namespace Drupal\avatars;
use Drupal\avatars\Entity\AvatarCacheInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Psr\Log\LoggerInterface;
class AvatarKitLocalCache implements AvatarKitLocalCacheInterface {
protected $avatarCacheStorage;
protected $fileStorage;
protected $logger;
protected $downloadUtility;
public function __construct(EntityTypeManagerInterface $entityTypeManager, LoggerInterface $logger, AvatarKitDownloadUtilityInterface $downloadUtility) {
$this->avatarCacheStorage = $entityTypeManager
->getStorage('avatars_avatar_cache');
$this->fileStorage = $entityTypeManager
->getStorage('file');
$this->logger = $logger;
$this->downloadUtility = $downloadUtility;
}
public function getLocalCache(string $service_id, EntityAvatarIdentifierInterface $identifier) : ?AvatarCacheInterface {
$hashed = $identifier
->getHashed();
$ids = $this->avatarCacheStorage
->getQuery()
->condition('avatar_service', $service_id)
->condition('identifier', $hashed)
->execute();
if ($ids) {
$id = reset($ids);
return $this->avatarCacheStorage
->load($id);
}
return NULL;
}
public function cacheLocalFileEntity(string $service_id, string $uri, EntityAvatarIdentifierInterface $identifier) : ?AvatarCacheInterface {
$identifier_hash = $identifier
->getHashed();
$files = $this->fileStorage
->loadByProperties([
'uri' => $uri,
]);
if (!$files) {
return NULL;
}
$file = reset($files);
$avatar_cache = $this->avatarCacheStorage
->create([
'avatar_service' => $service_id,
'identifier' => $identifier_hash,
'avatar' => $file,
]);
$avatar_cache
->save();
return $avatar_cache;
}
public function cacheRemote(string $service_id, string $uri, EntityAvatarIdentifierInterface $identifier) : ?AvatarCacheInterface {
$entity = $identifier
->getEntity();
$identifier_hash = $identifier
->getHashed();
$file = NULL;
try {
$response = $this->downloadUtility
->get($uri);
} catch (\Exception $e) {
$log_args = [
'@service' => $service_id,
'@entity_type' => $entity
->getEntityTypeId(),
'@entity_id' => $entity
->id(),
'@message' => $e
->getMessage(),
];
$this->logger
->debug('Failed to download @service avatar for @entity_type #@entity_id. This failure is probably acceptable. Message is: @message', $log_args);
}
if (isset($response)) {
try {
$filepath = $this
->avatarFileName($service_id, $identifier);
$file = $this->downloadUtility
->createFile($response, $filepath);
} catch (\Exception $e) {
$this->logger
->error('Failed to create avatar file: @exception', [
'@exception' => $e
->getMessage(),
]);
}
}
if (!$file) {
return NULL;
}
$avatar_cache = $this->avatarCacheStorage
->create([
'avatar_service' => $service_id,
'identifier' => $identifier_hash,
'avatar' => $file,
]);
$avatar_cache
->save();
return $avatar_cache;
}
public function cacheEmpty(string $service_id, ?string $uri, EntityAvatarIdentifierInterface $identifier) : AvatarCacheInterface {
$identifier_hash = $identifier
->getHashed();
$avatar_cache = $this->avatarCacheStorage
->create([
'avatar_service' => $service_id,
'identifier' => $identifier_hash,
'avatar' => NULL,
]);
$avatar_cache
->save();
return $avatar_cache;
}
public function invalidateCaches(EntityInterface $entity) : void {
}
protected function avatarFileName(string $service_id, EntityAvatarIdentifierInterface $identifier) : string {
$name = $identifier
->getEntity()
->id();
return \file_create_filename($name, 'public://avatar_kit/' . $service_id . '/identifier/');
}
}