|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @package Divante\VsbridgeIndexerCatalog |
| 4 | + * @author Agata Firlejczyk <[email protected]> |
| 5 | + * @copyright 2019 Divante Sp. z o.o. |
| 6 | + * @license See LICENSE_DIVANTE.txt for license details. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Divante\VsbridgeIndexerCatalog\Model; |
| 10 | + |
| 11 | +use Divante\VsbridgeIndexerCatalog\Model\ResourceModel\Product\Rewrite as RewriteResource; |
| 12 | +use Magento\Framework\App\Config\ScopeConfigInterface as ScopeConfigInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class ProductUrlPathGenerator |
| 16 | + */ |
| 17 | +class ProductUrlPathGenerator |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var RewriteResource |
| 21 | + */ |
| 22 | + private $rewriteResource; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + private $productUrlSuffix; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var ScopeConfigInterface |
| 31 | + */ |
| 32 | + private $scopeConfig; |
| 33 | + |
| 34 | + /** |
| 35 | + * ProductUrlPathGenerator constructor. |
| 36 | + * |
| 37 | + * @param RewriteResource $rewrite |
| 38 | + * @param ScopeConfigInterface $config |
| 39 | + */ |
| 40 | + public function __construct( |
| 41 | + RewriteResource $rewrite, |
| 42 | + ScopeConfigInterface $config |
| 43 | + ) { |
| 44 | + $this->scopeConfig = $config; |
| 45 | + $this->rewriteResource = $rewrite; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param array $products |
| 50 | + * @param $storeId |
| 51 | + * |
| 52 | + * @return array |
| 53 | + */ |
| 54 | + public function addUrlPath(array $products, $storeId) |
| 55 | + { |
| 56 | + $productIds = array_keys($products); |
| 57 | + $urlSuffix = $this->getProductUrlSuffix(); |
| 58 | + |
| 59 | + $rewrites = $this->rewriteResource->getRawRewritesData($productIds, $storeId); |
| 60 | + |
| 61 | + foreach ($rewrites as $productId => $rewrite) { |
| 62 | + $rewrite = mb_substr($rewrite, 0, -strlen($urlSuffix)); |
| 63 | + $products[$productId]['url_path'] = $rewrite; |
| 64 | + } |
| 65 | + |
| 66 | + return $products; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Retrieve product rewrite suffix for store |
| 71 | + * |
| 72 | + * @return string |
| 73 | + */ |
| 74 | + private function getProductUrlSuffix() |
| 75 | + { |
| 76 | + if (null === $this->productUrlSuffix) { |
| 77 | + $this->productUrlSuffix = $this->scopeConfig->getValue( |
| 78 | + \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + return $this->productUrlSuffix; |
| 83 | + } |
| 84 | +} |
0 commit comments