Skip to content

Commit 98ae1a3

Browse files
[framework] handle image resizing by image proxy (#2924)
2 parents 87eccdf + e06b8f7 commit 98ae1a3

9 files changed

+23
-152
lines changed

src/Model/Resolver/Image/Exception/ImageSizeInvalidUserError.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Model/Resolver/Image/ImagesQuery.php

Lines changed: 12 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,17 @@
55
namespace Shopsys\FrontendApiBundle\Model\Resolver\Image;
66

77
use Shopsys\FrameworkBundle\Component\Domain\Domain;
8-
use Shopsys\FrameworkBundle\Component\Image\Config\Exception\ImageSizeNotFoundException;
9-
use Shopsys\FrameworkBundle\Component\Image\Config\Exception\ImageTypeNotFoundException;
108
use Shopsys\FrameworkBundle\Component\Image\Config\ImageConfig;
11-
use Shopsys\FrameworkBundle\Component\Image\Config\ImageEntityConfig;
12-
use Shopsys\FrameworkBundle\Component\Image\Config\ImageSizeConfig;
139
use Shopsys\FrameworkBundle\Component\Image\Image;
1410
use Shopsys\FrameworkBundle\Component\Image\ImageFacade;
1511
use Shopsys\FrameworkBundle\Model\Advert\Advert;
1612
use Shopsys\FrameworkBundle\Model\Product\Product;
1713
use Shopsys\FrontendApiBundle\Component\Image\ImageFacade as FrontendApiImageFacade;
1814
use Shopsys\FrontendApiBundle\Model\Resolver\AbstractQuery;
19-
use Shopsys\FrontendApiBundle\Model\Resolver\Image\Exception\ImageSizeInvalidUserError;
20-
use Shopsys\FrontendApiBundle\Model\Resolver\Image\Exception\ImageTypeInvalidUserError;
2115

2216
class ImagesQuery extends AbstractQuery
2317
{
2418
protected const IMAGE_ENTITY_PRODUCT = 'product';
25-
protected const IMAGE_ENTITY_ADVERT = 'noticer';
2619

2720
/**
2821
* @param \Shopsys\FrameworkBundle\Component\Image\ImageFacade $imageFacade
@@ -41,36 +34,33 @@ public function __construct(
4134
/**
4235
* @param object $entity
4336
* @param string|null $type
44-
* @param string|null $size
4537
* @return array
4638
*/
47-
public function imagesByEntityQuery(object $entity, ?string $type, ?string $size): array
39+
public function imagesByEntityQuery(object $entity, ?string $type): array
4840
{
4941
$entityName = $this->imageConfig->getEntityName($entity);
5042

51-
return $this->resolveByEntityId($entity->getId(), $entityName, $type, $size);
43+
return $this->resolveByEntityId($entity->getId(), $entityName, $type);
5244
}
5345

5446
/**
5547
* @param \Shopsys\FrameworkBundle\Model\Product\Product|array $data
5648
* @param string|null $type
57-
* @param string|null $size
5849
* @return array
5950
*/
60-
public function imagesByProductQuery($data, ?string $type, ?string $size): array
51+
public function imagesByProductQuery($data, ?string $type): array
6152
{
6253
$productId = $data instanceof Product ? $data->getId() : $data['id'];
6354

64-
return $this->resolveByEntityId($productId, static::IMAGE_ENTITY_PRODUCT, $type, $size);
55+
return $this->resolveByEntityId($productId, static::IMAGE_ENTITY_PRODUCT, $type);
6556
}
6657

6758
/**
6859
* @param \Shopsys\FrameworkBundle\Model\Advert\Advert $advert
6960
* @param string|null $type
70-
* @param string|null $size
7161
* @return array
7262
*/
73-
public function imagesByAdvertQuery(Advert $advert, ?string $type, ?string $size): array
63+
public function imagesByAdvertQuery(Advert $advert, ?string $type): array
7464
{
7565
$entityName = $this->imageConfig->getEntityName($advert);
7666

@@ -80,120 +70,50 @@ public function imagesByAdvertQuery(Advert $advert, ?string $type, ?string $size
8070
$entityName,
8171
$type,
8272
),
83-
$this->getSizeConfigsForAdvert($advert, $type, $size),
8473
);
8574
}
8675

8776
/**
8877
* @param int $entityId
8978
* @param string $entityName
9079
* @param string|null $type
91-
* @param string|null $size
9280
* @return array
9381
*/
94-
protected function resolveByEntityId(int $entityId, string $entityName, ?string $type, ?string $size): array
82+
protected function resolveByEntityId(int $entityId, string $entityName, ?string $type): array
9583
{
96-
$sizeConfigs = $this->getSizeConfigs($type, $size, $entityName);
9784
$images = $this->frontendApiImageFacade->getImagesByEntityIdAndNameIndexedById($entityId, $entityName, $type);
9885

99-
return $this->getResolvedImages($images, $sizeConfigs);
100-
}
101-
102-
/**
103-
* @param string|null $type
104-
* @param string|null $size
105-
* @param string $entityName
106-
* @return \Shopsys\FrameworkBundle\Component\Image\Config\ImageSizeConfig[]
107-
*/
108-
protected function getSizeConfigs(?string $type, ?string $size, string $entityName): array
109-
{
110-
$imageConfig = $this->imageConfig->getEntityConfigByEntityName($entityName);
111-
112-
if ($size === ImageConfig::DEFAULT_SIZE_NAME) {
113-
$size = ImageEntityConfig::WITHOUT_NAME_KEY;
114-
}
115-
116-
try {
117-
if ($type === null) {
118-
if ($size === null) {
119-
$sizeConfigs = $imageConfig->getSizeConfigs();
120-
} else {
121-
$sizeConfigs = [$imageConfig->getSizeConfig($size)];
122-
}
123-
} else {
124-
if ($size === null) {
125-
$sizeConfigs = $imageConfig->getSizeConfigsByType($type);
126-
} else {
127-
$sizeConfigs = [$imageConfig->getSizeConfigByType($type, $size)];
128-
}
129-
}
130-
} catch (ImageSizeNotFoundException $e) {
131-
throw new ImageSizeInvalidUserError(sprintf('Image size %s not found for %s', $size, $entityName));
132-
} catch (ImageTypeNotFoundException $e) {
133-
throw new ImageTypeInvalidUserError(sprintf('Image type %s not found for %s', $type, $entityName));
134-
}
135-
136-
return $sizeConfigs;
86+
return $this->getResolvedImages($images);
13787
}
13888

13989
/**
14090
* @param \Shopsys\FrameworkBundle\Component\Image\Image[] $images
141-
* @param \Shopsys\FrameworkBundle\Component\Image\Config\ImageSizeConfig[] $sizeConfigs
142-
* @return array
91+
* @return array<int, array{url: string, name: string|null}>
14392
*/
144-
protected function getResolvedImages(array $images, array $sizeConfigs): array
93+
protected function getResolvedImages(array $images): array
14594
{
14695
$resolvedImages = [];
14796

14897
foreach ($images as $image) {
149-
foreach ($sizeConfigs as $sizeConfig) {
150-
$resolvedImages[] = $this->getResolvedImage($image, $sizeConfig);
151-
}
98+
$resolvedImages[] = $this->getResolvedImage($image);
15299
}
153100

154101
return $resolvedImages;
155102
}
156103

157104
/**
158105
* @param \Shopsys\FrameworkBundle\Component\Image\Image $image
159-
* @param \Shopsys\FrameworkBundle\Component\Image\Config\ImageSizeConfig $sizeConfig
160-
* @return array
106+
* @return array{url: string, name: string|null}
161107
*/
162-
protected function getResolvedImage(Image $image, ImageSizeConfig $sizeConfig): array
108+
protected function getResolvedImage(Image $image): array
163109
{
164110
return [
165111
'name' => $image->getName(),
166-
'type' => $image->getType(),
167-
'position' => $image->getPosition(),
168-
'width' => $sizeConfig->getWidth(),
169-
'height' => $sizeConfig->getHeight(),
170-
'size' => $sizeConfig->getName() === null ? ImageConfig::DEFAULT_SIZE_NAME : $sizeConfig->getName(),
171112
'url' => $this->imageFacade->getImageUrl(
172113
$this->domain->getCurrentDomainConfig(),
173114
$image,
174-
$sizeConfig->getName(),
175115
$image->getType(),
176116
),
177117
];
178118
}
179-
180-
/**
181-
* @param \Shopsys\FrameworkBundle\Model\Advert\Advert $advert
182-
* @param string|null $type
183-
* @param string|null $size
184-
* @return \Shopsys\FrameworkBundle\Component\Image\Config\ImageSizeConfig[]
185-
*/
186-
protected function getSizeConfigsForAdvert(Advert $advert, ?string $type, ?string $size): array
187-
{
188-
$entityName = static::IMAGE_ENTITY_ADVERT;
189-
190-
if ($size === null) {
191-
return array_merge(
192-
$this->getSizeConfigs($type, $advert->getPositionName(), $entityName),
193-
$this->getSizeConfigs($type, ImageConfig::ORIGINAL_SIZE_NAME, $entityName),
194-
);
195-
}
196-
197-
return $this->getSizeConfigs($type, $size, $entityName);
198-
}
199119
}

src/Resources/config/graphql-types/AdvertImageDecorator.types.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ AdvertImageDecorator:
1010
description: "Advert link"
1111
images:
1212
type: "[Image!]!"
13-
description: "Advert image"
14-
resolve: '@=query("imagesByAdvertQuery", value, args["type"], args["size"])'
13+
description: "Advert images"
14+
resolve: '@=query("imagesByAdvertQuery", value, args["type"])'
1515
args:
1616
type:
1717
type: "String"
1818
defaultValue: null
19-
size:
20-
type: "String"
21-
defaultValue: null
2219
interfaces:
2320
- 'Advert'

src/Resources/config/graphql-types/BrandDecorator.types.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ BrandDecorator:
2828
images:
2929
type: "[Image!]!"
3030
description: "Brand images"
31-
resolve: '@=query("imagesByEntityQuery", value, args["type"], args["size"])'
31+
resolve: '@=query("imagesByEntityQuery", value, args["type"])'
3232
args:
3333
type:
3434
type: "String"
3535
defaultValue: null
36-
size:
37-
type: "String"
3836
products:
3937
type: "ProductConnection"
4038
description: "Paginated and ordered products of brand"

src/Resources/config/graphql-types/CategoryDecorator.types.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ CategoryDecorator:
1919
images:
2020
type: "[Image!]!"
2121
description: "Category images"
22-
resolve: '@=query("imagesByEntityQuery", value, args["type"], args["size"])'
22+
resolve: '@=query("imagesByEntityQuery", value, args["type"])'
2323
args:
2424
type:
2525
type: "String"
2626
defaultValue: null
27-
size:
28-
type: "String"
2927
products:
3028
type: "ProductConnection"
3129
description: "Paginated and ordered products of category"

src/Resources/config/graphql-types/ImageDecorator.types.yaml

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,7 @@ ImageDecorator:
66
fields:
77
name:
88
type: "String"
9-
description: "Name of image usable as alternative text"
10-
type:
11-
type: "String"
12-
description: "Image type"
13-
position:
14-
type: "Int"
15-
description: "Position of image in list"
16-
size:
17-
type: "String"
18-
description: "Image size defined in images.yaml"
9+
description: "Name of the image usable as an alternative text"
1910
url:
20-
type: "String"
21-
description: "URL address of image"
22-
width:
23-
type: "Int"
24-
description: "Width in pixels defined in images.yaml"
25-
height:
26-
type: "Int"
27-
description: "Height in pixels defined in images.yaml"
11+
type: "String!"
12+
description: "URL address of the image"

src/Resources/config/graphql-types/PaymentDecorator.types.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ PaymentDecorator:
2626
images:
2727
type: "[Image!]!"
2828
description: "Payment images"
29-
resolve: '@=query("imagesByEntityQuery", value, args["type"], args["size"])'
29+
resolve: '@=query("imagesByEntityQuery", value, args["type"])'
3030
args:
3131
type:
3232
type: "String"
3333
defaultValue: null
34-
size:
35-
type: "String"
3634
transports:
3735
type: "[Transport!]!"
3836
description: "List of assigned transports"

src/Resources/config/graphql-types/ProductDecorator.types.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ ProductDecorator:
3838
images:
3939
type: "[Image]!"
4040
description: "Product images"
41-
resolve: '@=query("imagesByProductQuery", value, args["type"], args["size"])'
41+
resolve: '@=query("imagesByProductQuery", value, args["type"])'
4242
args:
4343
type:
4444
type: "String"
4545
defaultValue: null
46-
size:
47-
type: "String"
4846
brand:
4947
type: "Brand"
5048
description: "Brand of product"

src/Resources/config/graphql-types/TransportDecorator.types.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ TransportDecorator:
2424
description: "Transport price"
2525
resolve: '@=query("priceByTransportQuery", value)'
2626
images:
27-
type: "[Image]!"
27+
type: "[Image!]!"
2828
description: "Transport images"
29-
resolve: '@=query("imagesByEntityQuery", value, args["type"], args["size"])'
29+
resolve: '@=query("imagesByEntityQuery", value, args["type"])'
3030
args:
3131
type:
3232
type: "String"
3333
defaultValue: null
34-
size:
35-
type: "String"
3634
payments:
3735
type: "[Payment!]!"
3836
description: "List of assigned payments"

0 commit comments

Comments
 (0)