src/Sitemaps/ProductGenerator.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Sitemaps;
  3. use App\Website\LinkGenerator\ProductLinkGenerator;
  4. use CoreShop\Component\Core\Repository\ProductRepositoryInterface;
  5. use Pimcore\Sitemap\Element\AbstractElementGenerator;
  6. use Pimcore\Sitemap\Element\GeneratorContext;
  7. use Presta\SitemapBundle\Service\UrlContainerInterface;
  8. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. class ProductGenerator extends AbstractElementGenerator
  11. {
  12.     public function __construct(
  13.         array                                       $filters = [],
  14.         array                                       $processors = [],
  15.         private readonly ProductRepositoryInterface $productRepository,
  16.         private readonly ProductLinkGenerator       $linkGenerator,
  17.         private readonly array                      $siteConfig = [],
  18.     )
  19.     {
  20.         parent::__construct($filters$processors);
  21.     }
  22.     public function populate(UrlContainerInterface $urlContainerstring $section null): void
  23.     {
  24.         if (null !== $section && $section !== 'product') {
  25.             return;
  26.         }
  27.         $section 'product';
  28.         $list $this->productRepository->getProductsListing();
  29.         // with the params parameter
  30.         $context = new GeneratorContext($urlContainer$section);
  31.         foreach ($list as $product) {
  32.             if (!$this->canBeAdded($product$context)) {
  33.                 continue;
  34.             }
  35.             $link $this->linkGenerator->generate($product, [
  36.                 'document' => $product->getProperty('document'),
  37.                 'referenceType' => UrlGeneratorInterface::ABSOLUTE_URL
  38.             ]);
  39.             $baseUrl $this->siteConfig['schema'] . '://' $this->siteConfig['boutique'];
  40.             $url $this->process(new UrlConcrete($baseUrl $link), $product$context);
  41.             if (null === $url) {
  42.                 continue;
  43.             }
  44.             $urlContainer->addUrl($url$section);
  45.         }
  46.     }
  47. }