<?php
namespace App\Sitemaps;
use App\Website\LinkGenerator\ProductLinkGenerator;
use CoreShop\Component\Core\Repository\ProductRepositoryInterface;
use Pimcore\Sitemap\Element\AbstractElementGenerator;
use Pimcore\Sitemap\Element\GeneratorContext;
use Presta\SitemapBundle\Service\UrlContainerInterface;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ProductGenerator extends AbstractElementGenerator
{
public function __construct(
array $filters = [],
array $processors = [],
private readonly ProductRepositoryInterface $productRepository,
private readonly ProductLinkGenerator $linkGenerator,
private readonly array $siteConfig = [],
)
{
parent::__construct($filters, $processors);
}
public function populate(UrlContainerInterface $urlContainer, string $section = null): void
{
if (null !== $section && $section !== 'product') {
return;
}
$section = 'product';
$list = $this->productRepository->getProductsListing();
// with the params parameter
$context = new GeneratorContext($urlContainer, $section);
foreach ($list as $product) {
if (!$this->canBeAdded($product, $context)) {
continue;
}
$link = $this->linkGenerator->generate($product, [
'document' => $product->getProperty('document'),
'referenceType' => UrlGeneratorInterface::ABSOLUTE_URL
]);
$baseUrl = $this->siteConfig['schema'] . '://' . $this->siteConfig['boutique'];
$url = $this->process(new UrlConcrete($baseUrl . $link), $product, $context);
if (null === $url) {
continue;
}
$urlContainer->addUrl($url, $section);
}
}
}