Skip to content

Commit 4c9b252

Browse files
committed
Fix some errors on feed generation
1 parent e57cd10 commit 4c9b252

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

src/DataSyncInfrastructure/Generator/ResourceFeedGenerator.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,19 @@ public function generate(
3939

4040
$payload = [];
4141
foreach ($resources as $resource) {
42-
$payload[] = $this->serializer->normalize(
43-
$resource,
44-
'array',
45-
[
46-
'type' => 'webgriffe_sylius_clerk_plugin',
47-
'channel' => $channel,
48-
'localeCode' => $localeCode,
49-
],
50-
);
42+
try {
43+
$payload[] = $this->serializer->normalize(
44+
$resource,
45+
'array',
46+
[
47+
'type' => 'webgriffe_sylius_clerk_plugin',
48+
'channel' => $channel,
49+
'localeCode' => $localeCode,
50+
],
51+
);
52+
} catch (\Throwable $e) {
53+
continue;
54+
}
5155
}
5256

5357
return new Feed(

src/DataSyncInfrastructure/Normalizer/Event/ProductNormalizerEvent.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ final class ProductNormalizerEvent
1313
* @param array{
1414
* id: string|int,
1515
* name: string,
16-
* description: string,
16+
* description?: string,
1717
* price: float,
1818
* list_price: float,
19-
* image: string,
19+
* image?: string,
2020
* url: string,
2121
* categories: array<int|string>,
2222
* created_at: string,
@@ -40,10 +40,10 @@ public function __construct(
4040
* @return array{
4141
* id: string|int,
4242
* name: string,
43-
* description: string,
43+
* description?: string,
4444
* price: float,
4545
* list_price: float,
46-
* image: string,
46+
* image?: string,
4747
* url: string,
4848
* categories: array<int|string>,
4949
* created_at: string,
@@ -63,10 +63,10 @@ public function getProductData(): array
6363
* @param array{
6464
* id: string|int,
6565
* name: string,
66-
* description: string,
66+
* description?: string,
6767
* price: float,
6868
* list_price: float,
69-
* image: string,
69+
* image?: string,
7070
* url: string,
7171
* categories: array<int|string>,
7272
* created_at: string,

src/DataSyncInfrastructure/Normalizer/ProductNormalizer.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public function __construct(
3737
* @return array{
3838
* id: string|int,
3939
* name: string,
40-
* description: string,
40+
* description?: string,
4141
* price: float,
4242
* list_price: float,
43-
* image: string,
43+
* image?: string,
4444
* url: string,
4545
* categories: array<int|string>,
4646
* created_at: string,
@@ -72,9 +72,6 @@ public function normalize(mixed $object, ?string $format = null, array $context
7272
throw new \InvalidArgumentException('Product name must not be null for the product "' . $productId . '".');
7373
}
7474
$productDescription = $productTranslation->getDescription();
75-
if ($productDescription === null) {
76-
throw new \InvalidArgumentException('Product description must not be null for the product "' . $productId . '".');
77-
}
7875
$productVariant = $this->productVariantResolver->getVariant($product);
7976
if (!$productVariant instanceof ProductVariantInterface) {
8077
throw new \InvalidArgumentException('At least one product variant should exists for the product "' . $productId . '".');
@@ -83,7 +80,10 @@ public function normalize(mixed $object, ?string $format = null, array $context
8380
$originalPrice = $this->productVariantPricesCalculator->calculateOriginal($productVariant, ['channel' => $channel]);
8481

8582
$productMainImage = $this->getMainImage($product);
86-
$productMainImageUrl = $this->getUrlOfImage($productMainImage, $channel);
83+
$productMainImageUrl = null;
84+
if ($productMainImage !== null) {
85+
$productMainImageUrl = $this->getUrlOfImage($productMainImage, $channel);
86+
}
8787
$productUrl = $this->getUrlOfProduct($productTranslation, $channel);
8888
$createdAt = $product->getCreatedAt();
8989
if ($createdAt === null) {
@@ -93,14 +93,18 @@ public function normalize(mixed $object, ?string $format = null, array $context
9393
$productData = [
9494
'id' => $productId,
9595
'name' => $productName,
96-
'description' => $productDescription,
9796
'price' => $price / 100,
9897
'list_price' => $originalPrice / 100,
99-
'image' => $productMainImageUrl,
10098
'url' => $productUrl,
10199
'categories' => $this->getCategoryIds($product),
102100
'created_at' => $createdAt->format('c'),
103101
];
102+
if ($productDescription !== null) {
103+
$productData['description'] = $productDescription;
104+
}
105+
if ($productMainImageUrl !== null) {
106+
$productData['image'] = $productMainImageUrl;
107+
}
104108

105109
$productNormalizerEvent = new ProductNormalizerEvent(
106110
$productData,
@@ -123,19 +127,18 @@ public function supportsNormalization(mixed $data, ?string $format = null, array
123127
;
124128
}
125129

126-
public function getMainImage(ProductInterface $product): ProductImageInterface
130+
public function getMainImage(ProductInterface $product): ?ProductImageInterface
127131
{
128132
$imageByType = $product->getImagesByType($this->imageType)->first();
129133
if ($imageByType instanceof ProductImageInterface) {
130134
return $imageByType;
131135
}
132-
133136
$image = $product->getImages()->first();
134137
if ($image instanceof ProductImageInterface) {
135138
return $image;
136139
}
137140

138-
throw new \InvalidArgumentException('Product "' . (string) $product->getId() . '" has no images.');
141+
return null;
139142
}
140143

141144
public function getUrlOfImage(ProductImageInterface $productMainImage, ChannelInterface $channel): string
@@ -146,8 +149,11 @@ public function getUrlOfImage(ProductImageInterface $productMainImage, ChannelIn
146149
Assert::stringNotEmpty($channelHost);
147150
$channelRequestContext->setHost($channelHost);
148151

152+
$imagePath = $productMainImage->getPath();
153+
Assert::stringNotEmpty($imagePath, 'Product image path must not be empty.');
154+
149155
$imageUrl = $this->cacheManager->getBrowserPath(
150-
(string) $productMainImage->getPath(),
156+
$imagePath,
151157
$this->imageFilterToApply,
152158
);
153159

0 commit comments

Comments
 (0)