src/Website/LinkGenerator/ProductLinkGenerator.php line 76

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Website\LinkGenerator;
  15. use App\Model\Product\AccessoryPart;
  16. use App\Model\Product\Car;
  17. use App\Website\Tool\ForceInheritance;
  18. use App\Website\Tool\Text;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\ProductInterface;
  20. use Pimcore\Model\DataObject\ClassDefinition\LinkGeneratorInterface;
  21. use Pimcore\Model\DataObject\Concrete;
  22. use Pimcore\Model\Document;
  23. class ProductLinkGenerator extends AbstractProductLinkGenerator implements LinkGeneratorInterface
  24. {
  25.     /**
  26.      * @param Concrete $object
  27.      * @param array $params
  28.      *
  29.      * @return string
  30.      */
  31.     public function generate(Concrete $object, array $params = []): string
  32.     {
  33.         if (!($object instanceof Car || $object instanceof AccessoryPart)) {
  34.             throw new \InvalidArgumentException('Given object is no Car');
  35.         }
  36.         if (isset($params['document']) && $params['document'] instanceof Document) {
  37.             $this->document $params['document'];
  38.         }
  39.         return $this->doGenerate($object$params);
  40.     }
  41.     /**
  42.      * @param ProductInterface $object
  43.      * @param array $params
  44.      *
  45.      * @return string
  46.      */
  47.     public function generateWithMockup(ProductInterface $object, array $params = []): string
  48.     {
  49.         return $this->doGenerate($object$params);
  50.     }
  51.     /**
  52.      * @param $object
  53.      * @param $params
  54.      *
  55.      * @return string
  56.      */
  57.     protected function doGenerate($object$params): string
  58.     {
  59.         return ForceInheritance::run(function () use ($object$params) {
  60.             if (!empty($object->getUrlSlug())) {
  61.                 return current($object->getUrlSlug())->getSlug();
  62.             }
  63.             $locale $params['locale'] ?? null;
  64.             return $this->pimcoreUrl->__invoke(
  65.                 [
  66.                     'productname' => Text::toUrl($object->getOSName() ?? 'product'),
  67.                     'product' => $object->getId(),
  68.                     'path' => $this->getNavigationPath($object->getMainCategory(), $params['rootCategory'] ?? null$locale),
  69.                     'page' => null,
  70.                     '_locale' => $locale,
  71.                 ],
  72.                 'shop-detail',
  73.                 true
  74.             );
  75.         });
  76.     }
  77. }