Skip to content

Commit

Permalink
[framework] handle image resizing by image proxy (#2924)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek-rostislav authored Dec 7, 2023
2 parents 87eccdf + e06b8f7 commit 98ae1a3
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 152 deletions.
21 changes: 0 additions & 21 deletions src/Model/Resolver/Image/Exception/ImageSizeInvalidUserError.php

This file was deleted.

104 changes: 12 additions & 92 deletions src/Model/Resolver/Image/ImagesQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,17 @@
namespace Shopsys\FrontendApiBundle\Model\Resolver\Image;

use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\Image\Config\Exception\ImageSizeNotFoundException;
use Shopsys\FrameworkBundle\Component\Image\Config\Exception\ImageTypeNotFoundException;
use Shopsys\FrameworkBundle\Component\Image\Config\ImageConfig;
use Shopsys\FrameworkBundle\Component\Image\Config\ImageEntityConfig;
use Shopsys\FrameworkBundle\Component\Image\Config\ImageSizeConfig;
use Shopsys\FrameworkBundle\Component\Image\Image;
use Shopsys\FrameworkBundle\Component\Image\ImageFacade;
use Shopsys\FrameworkBundle\Model\Advert\Advert;
use Shopsys\FrameworkBundle\Model\Product\Product;
use Shopsys\FrontendApiBundle\Component\Image\ImageFacade as FrontendApiImageFacade;
use Shopsys\FrontendApiBundle\Model\Resolver\AbstractQuery;
use Shopsys\FrontendApiBundle\Model\Resolver\Image\Exception\ImageSizeInvalidUserError;
use Shopsys\FrontendApiBundle\Model\Resolver\Image\Exception\ImageTypeInvalidUserError;

class ImagesQuery extends AbstractQuery
{
protected const IMAGE_ENTITY_PRODUCT = 'product';
protected const IMAGE_ENTITY_ADVERT = 'noticer';

/**
* @param \Shopsys\FrameworkBundle\Component\Image\ImageFacade $imageFacade
Expand All @@ -41,36 +34,33 @@ public function __construct(
/**
* @param object $entity
* @param string|null $type
* @param string|null $size
* @return array
*/
public function imagesByEntityQuery(object $entity, ?string $type, ?string $size): array
public function imagesByEntityQuery(object $entity, ?string $type): array
{
$entityName = $this->imageConfig->getEntityName($entity);

return $this->resolveByEntityId($entity->getId(), $entityName, $type, $size);
return $this->resolveByEntityId($entity->getId(), $entityName, $type);
}

/**
* @param \Shopsys\FrameworkBundle\Model\Product\Product|array $data
* @param string|null $type
* @param string|null $size
* @return array
*/
public function imagesByProductQuery($data, ?string $type, ?string $size): array
public function imagesByProductQuery($data, ?string $type): array
{
$productId = $data instanceof Product ? $data->getId() : $data['id'];

return $this->resolveByEntityId($productId, static::IMAGE_ENTITY_PRODUCT, $type, $size);
return $this->resolveByEntityId($productId, static::IMAGE_ENTITY_PRODUCT, $type);
}

/**
* @param \Shopsys\FrameworkBundle\Model\Advert\Advert $advert
* @param string|null $type
* @param string|null $size
* @return array
*/
public function imagesByAdvertQuery(Advert $advert, ?string $type, ?string $size): array
public function imagesByAdvertQuery(Advert $advert, ?string $type): array
{
$entityName = $this->imageConfig->getEntityName($advert);

Expand All @@ -80,120 +70,50 @@ public function imagesByAdvertQuery(Advert $advert, ?string $type, ?string $size
$entityName,
$type,
),
$this->getSizeConfigsForAdvert($advert, $type, $size),
);
}

/**
* @param int $entityId
* @param string $entityName
* @param string|null $type
* @param string|null $size
* @return array
*/
protected function resolveByEntityId(int $entityId, string $entityName, ?string $type, ?string $size): array
protected function resolveByEntityId(int $entityId, string $entityName, ?string $type): array
{
$sizeConfigs = $this->getSizeConfigs($type, $size, $entityName);
$images = $this->frontendApiImageFacade->getImagesByEntityIdAndNameIndexedById($entityId, $entityName, $type);

return $this->getResolvedImages($images, $sizeConfigs);
}

/**
* @param string|null $type
* @param string|null $size
* @param string $entityName
* @return \Shopsys\FrameworkBundle\Component\Image\Config\ImageSizeConfig[]
*/
protected function getSizeConfigs(?string $type, ?string $size, string $entityName): array
{
$imageConfig = $this->imageConfig->getEntityConfigByEntityName($entityName);

if ($size === ImageConfig::DEFAULT_SIZE_NAME) {
$size = ImageEntityConfig::WITHOUT_NAME_KEY;
}

try {
if ($type === null) {
if ($size === null) {
$sizeConfigs = $imageConfig->getSizeConfigs();
} else {
$sizeConfigs = [$imageConfig->getSizeConfig($size)];
}
} else {
if ($size === null) {
$sizeConfigs = $imageConfig->getSizeConfigsByType($type);
} else {
$sizeConfigs = [$imageConfig->getSizeConfigByType($type, $size)];
}
}
} catch (ImageSizeNotFoundException $e) {
throw new ImageSizeInvalidUserError(sprintf('Image size %s not found for %s', $size, $entityName));
} catch (ImageTypeNotFoundException $e) {
throw new ImageTypeInvalidUserError(sprintf('Image type %s not found for %s', $type, $entityName));
}

return $sizeConfigs;
return $this->getResolvedImages($images);
}

/**
* @param \Shopsys\FrameworkBundle\Component\Image\Image[] $images
* @param \Shopsys\FrameworkBundle\Component\Image\Config\ImageSizeConfig[] $sizeConfigs
* @return array
* @return array<int, array{url: string, name: string|null}>
*/
protected function getResolvedImages(array $images, array $sizeConfigs): array
protected function getResolvedImages(array $images): array
{
$resolvedImages = [];

foreach ($images as $image) {
foreach ($sizeConfigs as $sizeConfig) {
$resolvedImages[] = $this->getResolvedImage($image, $sizeConfig);
}
$resolvedImages[] = $this->getResolvedImage($image);
}

return $resolvedImages;
}

/**
* @param \Shopsys\FrameworkBundle\Component\Image\Image $image
* @param \Shopsys\FrameworkBundle\Component\Image\Config\ImageSizeConfig $sizeConfig
* @return array
* @return array{url: string, name: string|null}
*/
protected function getResolvedImage(Image $image, ImageSizeConfig $sizeConfig): array
protected function getResolvedImage(Image $image): array
{
return [
'name' => $image->getName(),
'type' => $image->getType(),
'position' => $image->getPosition(),
'width' => $sizeConfig->getWidth(),
'height' => $sizeConfig->getHeight(),
'size' => $sizeConfig->getName() === null ? ImageConfig::DEFAULT_SIZE_NAME : $sizeConfig->getName(),
'url' => $this->imageFacade->getImageUrl(
$this->domain->getCurrentDomainConfig(),
$image,
$sizeConfig->getName(),
$image->getType(),
),
];
}

/**
* @param \Shopsys\FrameworkBundle\Model\Advert\Advert $advert
* @param string|null $type
* @param string|null $size
* @return \Shopsys\FrameworkBundle\Component\Image\Config\ImageSizeConfig[]
*/
protected function getSizeConfigsForAdvert(Advert $advert, ?string $type, ?string $size): array
{
$entityName = static::IMAGE_ENTITY_ADVERT;

if ($size === null) {
return array_merge(
$this->getSizeConfigs($type, $advert->getPositionName(), $entityName),
$this->getSizeConfigs($type, ImageConfig::ORIGINAL_SIZE_NAME, $entityName),
);
}

return $this->getSizeConfigs($type, $size, $entityName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ AdvertImageDecorator:
description: "Advert link"
images:
type: "[Image!]!"
description: "Advert image"
resolve: '@=query("imagesByAdvertQuery", value, args["type"], args["size"])'
description: "Advert images"
resolve: '@=query("imagesByAdvertQuery", value, args["type"])'
args:
type:
type: "String"
defaultValue: null
size:
type: "String"
defaultValue: null
interfaces:
- 'Advert'
4 changes: 1 addition & 3 deletions src/Resources/config/graphql-types/BrandDecorator.types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ BrandDecorator:
images:
type: "[Image!]!"
description: "Brand images"
resolve: '@=query("imagesByEntityQuery", value, args["type"], args["size"])'
resolve: '@=query("imagesByEntityQuery", value, args["type"])'
args:
type:
type: "String"
defaultValue: null
size:
type: "String"
products:
type: "ProductConnection"
description: "Paginated and ordered products of brand"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ CategoryDecorator:
images:
type: "[Image!]!"
description: "Category images"
resolve: '@=query("imagesByEntityQuery", value, args["type"], args["size"])'
resolve: '@=query("imagesByEntityQuery", value, args["type"])'
args:
type:
type: "String"
defaultValue: null
size:
type: "String"
products:
type: "ProductConnection"
description: "Paginated and ordered products of category"
Expand Down
21 changes: 3 additions & 18 deletions src/Resources/config/graphql-types/ImageDecorator.types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,7 @@ ImageDecorator:
fields:
name:
type: "String"
description: "Name of image usable as alternative text"
type:
type: "String"
description: "Image type"
position:
type: "Int"
description: "Position of image in list"
size:
type: "String"
description: "Image size defined in images.yaml"
description: "Name of the image usable as an alternative text"
url:
type: "String"
description: "URL address of image"
width:
type: "Int"
description: "Width in pixels defined in images.yaml"
height:
type: "Int"
description: "Height in pixels defined in images.yaml"
type: "String!"
description: "URL address of the image"
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ PaymentDecorator:
images:
type: "[Image!]!"
description: "Payment images"
resolve: '@=query("imagesByEntityQuery", value, args["type"], args["size"])'
resolve: '@=query("imagesByEntityQuery", value, args["type"])'
args:
type:
type: "String"
defaultValue: null
size:
type: "String"
transports:
type: "[Transport!]!"
description: "List of assigned transports"
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ ProductDecorator:
images:
type: "[Image]!"
description: "Product images"
resolve: '@=query("imagesByProductQuery", value, args["type"], args["size"])'
resolve: '@=query("imagesByProductQuery", value, args["type"])'
args:
type:
type: "String"
defaultValue: null
size:
type: "String"
brand:
type: "Brand"
description: "Brand of product"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ TransportDecorator:
description: "Transport price"
resolve: '@=query("priceByTransportQuery", value)'
images:
type: "[Image]!"
type: "[Image!]!"
description: "Transport images"
resolve: '@=query("imagesByEntityQuery", value, args["type"], args["size"])'
resolve: '@=query("imagesByEntityQuery", value, args["type"])'
args:
type:
type: "String"
defaultValue: null
size:
type: "String"
payments:
type: "[Payment!]!"
description: "List of assigned payments"

0 comments on commit 98ae1a3

Please sign in to comment.