feat(preview): expose availability and handle missing services
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -29,6 +29,29 @@ final readonly class PreviewManager
|
||||
) {
|
||||
}
|
||||
|
||||
/** @return array{enabled:bool,storage:bool,generation:bool} */
|
||||
public function availability(string $tenantId): array
|
||||
{
|
||||
$tenant = $this->tenants->fetchById($tenantId);
|
||||
if ($tenant === null) {
|
||||
return ['enabled' => false, 'storage' => false, 'generation' => false];
|
||||
}
|
||||
|
||||
$configuration = $tenant->getConfiguration()->preview();
|
||||
$enabled = $configuration->enabled();
|
||||
$storage = $enabled
|
||||
&& $tenant->getConfiguration()->stores()->store($configuration->store()) !== null;
|
||||
$generation = $storage && $this->previewProviders() !== [];
|
||||
|
||||
if ($enabled && !$storage) {
|
||||
$this->logger->debug('Preview storage is not configured', ['tenantId' => $tenantId]);
|
||||
} elseif ($enabled && $storage && !$generation) {
|
||||
$this->logger->debug('No preview generation provider is available', ['tenantId' => $tenantId]);
|
||||
}
|
||||
|
||||
return compact('enabled', 'storage', 'generation');
|
||||
}
|
||||
|
||||
public function fetch(
|
||||
string $tenantId,
|
||||
PreviewSource $source,
|
||||
@@ -45,6 +68,7 @@ final readonly class PreviewManager
|
||||
!$configuration->enabled()
|
||||
|| $variantConfiguration === null
|
||||
|| ($source->size !== null && $source->size > $configuration->maxSourceSize())
|
||||
|| $tenant->getConfiguration()->stores()->store($configuration->store()) === null
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
@@ -69,6 +93,9 @@ final readonly class PreviewManager
|
||||
}
|
||||
|
||||
$provider = $this->selectProvider($source->mimeType, $request);
|
||||
if ($provider === null) {
|
||||
return null;
|
||||
}
|
||||
$result = $provider->generate($source, $request);
|
||||
$blob = $this->writeCache(
|
||||
$tenantId,
|
||||
@@ -218,11 +245,11 @@ final readonly class PreviewManager
|
||||
private function selectProvider(
|
||||
string $sourceMimeType,
|
||||
PreviewRequest $request,
|
||||
): PreviewProviderInterface {
|
||||
): ?PreviewProviderInterface {
|
||||
$sourceMimeType = MimeType::normalize($sourceMimeType);
|
||||
$candidates = [];
|
||||
|
||||
foreach ($this->providers->providers(ProviderInterface::TYPE_PREVIEW) as $identifier => $provider) {
|
||||
foreach ($this->previewProviders() as $identifier => $provider) {
|
||||
if (
|
||||
!$provider instanceof PreviewProviderInterface
|
||||
|| !$provider->supports($sourceMimeType, $request)
|
||||
@@ -234,9 +261,7 @@ final readonly class PreviewManager
|
||||
}
|
||||
|
||||
if ($candidates === []) {
|
||||
throw new PreviewGenerationException(
|
||||
"No preview provider supports {$sourceMimeType} to {$request->preferredMimeType}"
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
usort($candidates, static function (array $left, array $right): int {
|
||||
@@ -246,4 +271,13 @@ final readonly class PreviewManager
|
||||
|
||||
return $candidates[0]['provider'];
|
||||
}
|
||||
|
||||
/** @return array<string, PreviewProviderInterface> */
|
||||
private function previewProviders(): array
|
||||
{
|
||||
return array_filter(
|
||||
$this->providers->providers(ProviderInterface::TYPE_PREVIEW),
|
||||
static fn(ProviderInterface $provider): bool => $provider instanceof PreviewProviderInterface,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user