Skip to content

Commit

Permalink
[framework] load products iteratively while generating image sitemaps (
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrejBlaho committed May 15, 2024
2 parents ae05658 + 25ec55c commit 3f80fc6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
6 changes: 6 additions & 0 deletions UPGRADE-15.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,12 @@ Follow the instructions in relevant sections, e.g. `shopsys/coding-standards` or

- see #project-base-diff to update your project

#### load products iteratively while generating image sitemaps ([#3144](https://github.com/shopsys/shopsys/pull/3144))

- `Shopsys\FrameworkBundle\Model\Product\ProductRepository` class was changed:
- `getAllOfferedProducts()` method was removed, use `getAllOfferedProductsPaginated()` instead
- see #project-base-diff to update your project

### Storefront

#### added query/mutation name to URL and headers ([#3041](https://github.com/shopsys/shopsys/pull/3041))
Expand Down
51 changes: 33 additions & 18 deletions packages/framework/src/Model/ImageSitemap/ImageSitemapFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class ImageSitemapFacade
{
protected const int PRODUCTS_BATCH_SIZE = 100;

/**
* @param string $sitemapsDir
* @param string $sitemapsUrlPrefix
Expand Down Expand Up @@ -63,25 +65,38 @@ public function getImageSitemapItemsForVisibleProducts(DomainConfig $domainConfi
$imageSitemapItems = [];
$domainId = $domainConfig->getId();
$pricingGroup = $this->pricingGroupSettingFacade->getDefaultPricingGroupByDomainId($domainId);
/** @var \Shopsys\FrameworkBundle\Model\Product\Product[] $products */
$products = $this->productRepository->getAllOfferedProducts($domainId, $pricingGroup);

foreach ($products as $product) {
try {
$imageUrl = $this->imageFacade->getImageUrl($domainConfig, $product);
$imageSitemapItem = new ImageSitemapItem();
$imageSitemapItem->loc = $this->friendlyUrlFacade->getAbsoluteUrlByRouteNameAndEntityId($domainConfig->getId(), 'front_product_detail', $product->getId());

$sitemapImage = new ImageSitemapItemImage();
$sitemapImage->loc = $imageUrl;
$sitemapImage->title = $product->getName($domainConfig->getLocale());
$imageSitemapItem->images[] = $sitemapImage;

$imageSitemapItems[] = $imageSitemapItem;
} catch (ImageNotFoundException $imageNotFoundException) {
continue;

$offset = 0;

do {
$products = $this->productRepository->getAllOfferedProductsPaginated($domainId, $pricingGroup, $offset, self::PRODUCTS_BATCH_SIZE);

foreach ($products as $product) {
$productName = $product->getName($domainConfig->getLocale());

if ($productName === null) {
continue;
}

try {
$imageUrl = $this->imageFacade->getImageUrl($domainConfig, $product);
$imageSitemapItem = new ImageSitemapItem();
$imageSitemapItem->loc = $this->friendlyUrlFacade->getAbsoluteUrlByRouteNameAndEntityId($domainConfig->getId(), 'front_product_detail', $product->getId());

$sitemapImage = new ImageSitemapItemImage();
$sitemapImage->loc = $imageUrl;
$sitemapImage->title = $productName;
$imageSitemapItem->images[] = $sitemapImage;

$imageSitemapItems[] = $imageSitemapItem;
} catch (ImageNotFoundException $imageNotFoundException) {
continue;
}
}
}
$this->entityManager->clear();
$productsCount = count($products);
$offset += $productsCount;
} while ($productsCount > 0);

return $imageSitemapItems;
}
Expand Down
18 changes: 14 additions & 4 deletions packages/framework/src/Model/Product/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,21 @@ public function getOneByUuid(string $uuid): Product
/**
* @param int $domainId
* @param \Shopsys\FrameworkBundle\Model\Pricing\Group\PricingGroup $pricingGroup
* @return array
* @param int $offset
* @param int $limit
* @return \App\Model\Product\Product[]
*/
public function getAllOfferedProducts(int $domainId, PricingGroup $pricingGroup): array
{
return $this->getAllOfferedQueryBuilder($domainId, $pricingGroup)->getQuery()->execute();
public function getAllOfferedProductsPaginated(
int $domainId,
PricingGroup $pricingGroup,
int $offset,
int $limit,
): array {
return $this->getAllOfferedQueryBuilder($domainId, $pricingGroup)
->setFirstResult($offset)
->setMaxResults($limit)
->getQuery()
->getResult();
}

/**
Expand Down

0 comments on commit 3f80fc6

Please sign in to comment.