src/Website/LinkGenerator/AbstractProductLinkGenerator.php line 82

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\Category;
  16. use App\Website\Tool\Text;
  17. use Pimcore\Http\Request\Resolver\DocumentResolver;
  18. use Pimcore\Model\DataObject\ClassDefinition\LinkGeneratorInterface;
  19. use Pimcore\Model\Document;
  20. use Pimcore\Twig\Extension\Templating\PimcoreUrl;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. abstract class AbstractProductLinkGenerator implements LinkGeneratorInterface
  23. {
  24.     const ROOT_CATEGORY_PROPERTY_NAME 'root_category';
  25.     /**
  26.      * @var DocumentResolver
  27.      */
  28.     protected $documentResolver;
  29.     /**
  30.      * @var RequestStack
  31.      */
  32.     protected $requestStack;
  33.     /**
  34.      * @var PimcoreUrl
  35.      */
  36.     protected $pimcoreUrl;
  37.     /**
  38.      * @var Document|null
  39.      */
  40.     protected $document;
  41.     public function __construct(DocumentResolver $documentResolverRequestStack $requestStackPimcoreUrl $pimcoreUrl)
  42.     {
  43.         $this->documentResolver $documentResolver;
  44.         $this->requestStack $requestStack;
  45.         $this->pimcoreUrl $pimcoreUrl;
  46.     }
  47.     /**
  48.      * @param Category|null $category
  49.      * @param Category|null $rootCategory
  50.      * @return string
  51.      */
  52.     public function getNavigationPath(?Category $category, ?Category $rootCategory null$locale null)
  53.     {
  54.         if (empty($rootCategory)) {
  55.             if (!$this->document) {
  56.                 try {
  57.                     $this->document $this->documentResolver->getDocument($this->requestStack->getCurrentRequest());
  58.                 } catch (\Exception $e) {
  59.                     // nothing to do
  60.                 }
  61.             }
  62.             if ($this->document) {
  63.                 $rootCategory $this->document->getProperty(self::ROOT_CATEGORY_PROPERTY_NAME);
  64.             }
  65.         }
  66.         $categories = [];
  67.         $path '';
  68.         if ($category) {
  69.             $categories $category->getParentCategoryList($rootCategory);
  70.         }
  71.         foreach ($categories as $categoryInPath) {
  72.             $path .= Text::toUrl($categoryInPath->getName($locale)).'/';
  73.         }
  74.         return $path;
  75.     }
  76. }