src/Website/LinkGenerator/ProductLinkGenerator.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Website\LinkGenerator;
  3. use Pimcore\Model\DataObject\ClassDefinition\LinkGeneratorInterface;
  4. use Pimcore\Model\DataObject\Product;
  5. use Pimcore\Tool;
  6. use Pimcore\Twig\Extension\Templating\PimcoreUrl;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\String\Slugger\SluggerInterface;
  9. class ProductLinkGenerator implements LinkGeneratorInterface {
  10.     public function __construct(
  11.         private SluggerInterface $slugger,
  12.         private PimcoreUrl $pimcoreUrl,
  13.         private ContainerInterface $container){
  14.     }
  15.     public function generate($object$params = []): string{
  16.         if(!$object instanceof Product){
  17.             throw new \InvalidArgumentException('Object must be an instance of Product');
  18.         }
  19.         $slug $this->slugger->slug($object->getName())->lower()->toString();
  20.         $link $this->pimcoreUrl->__invoke([
  21.             'slug' => $slug,
  22.             'productId' => $object->getId(),
  23.         ], 'product_show'true);
  24.         if(!str_contains($link'https://') || !str_contains($link'http://')){
  25.             $protocol $this->container->getParameter('site_protocol');
  26.             $link Tool::getHostUrl($protocol) . $link;
  27.         }
  28.         return $link;
  29.     }
  30. }