@@ -467,15 +467,8 @@ private function canonicalUrlRewriteGenerate(array $products)
467467 $ targetPath = $ this ->productUrlPathGenerator ->getCanonicalUrlPath ($ product );
468468 if ((int ) $ storeId !== (int ) $ product ->getStoreId ()
469469 && $ this ->isGlobalScope ($ product ->getStoreId ())) {
470- if ($ this ->cachedValues === null ) {
471- $ this ->cachedValues = $ this ->getScopeBasedUrlKeyValues ($ products );
472- }
473- if (!empty ($ this ->cachedValues ) && isset ($ this ->cachedValues [$ productId ][$ storeId ])) {
474- $ storeProduct = clone $ product ;
475- $ storeProduct ->setStoreId ($ storeId );
476- $ storeProduct ->setUrlKey ($ this ->cachedValues [$ productId ][$ storeId ]);
477- $ reqPath = $ this ->productUrlPathGenerator ->getUrlPathWithSuffix ($ storeProduct , $ storeId );
478- }
470+ $ this ->initializeCacheForProducts ($ products );
471+ $ reqPath = $ this ->getReqPath ((int )$ productId , (int )$ storeId , $ product );
479472 }
480473 $ urls [] = $ this ->urlRewriteFactory ->create ()
481474 ->setEntityType (ProductUrlRewriteGenerator::ENTITY_TYPE )
@@ -489,6 +482,40 @@ private function canonicalUrlRewriteGenerate(array $products)
489482 return $ urls ;
490483 }
491484
485+ /**
486+ * Initialization for cache with scop based values
487+ *
488+ * @param array $products
489+ * @return void
490+ */
491+ private function initializeCacheForProducts (array $ products ) : void
492+ {
493+ if ($ this ->cachedValues === null ) {
494+ $ this ->cachedValues = $ this ->getScopeBasedUrlKeyValues ($ products );
495+ }
496+ }
497+
498+ /**
499+ * Get request path for the selected scope
500+ *
501+ * @param int $productId
502+ * @param int $storeId
503+ * @param Product $product
504+ * @pram Category|null $category
505+ * @return string
506+ */
507+ private function getReqPath (int $ productId , int $ storeId , Product $ product , ?Category $ category = null ) : string
508+ {
509+ $ reqPath = $ this ->productUrlPathGenerator ->getUrlPathWithSuffix ($ product , $ storeId , $ category );
510+ if (!empty ($ this ->cachedValues ) && isset ($ this ->cachedValues [$ productId ][$ storeId ])) {
511+ $ storeProduct = clone $ product ;
512+ $ storeProduct ->setStoreId ($ storeId );
513+ $ storeProduct ->setUrlKey ($ this ->cachedValues [$ productId ][$ storeId ]);
514+ $ reqPath = $ this ->productUrlPathGenerator ->getUrlPathWithSuffix ($ storeProduct , $ storeId , $ category );
515+ }
516+ return $ reqPath ;
517+ }
518+
492519 /**
493520 * Get url key attribute values for the specified scope
494521 *
@@ -541,19 +568,8 @@ private function categoriesUrlRewriteGenerate(array $products): array
541568 $ targetPath = $ this ->productUrlPathGenerator ->getCanonicalUrlPath ($ product , $ category );
542569 if ((int ) $ storeId !== (int ) $ product ->getStoreId ()
543570 && $ this ->isGlobalScope ($ product ->getStoreId ())) {
544- if ($ this ->cachedValues === null ) {
545- $ this ->cachedValues = $ this ->getScopeBasedUrlKeyValues ($ products );
546- }
547- if (!empty ($ this ->cachedValues ) && isset ($ this ->cachedValues [$ productId ][$ storeId ])) {
548- $ storeProduct = clone $ product ;
549- $ storeProduct ->setStoreId ($ storeId );
550- $ storeProduct ->setUrlKey ($ this ->cachedValues [$ productId ][$ storeId ]);
551- $ requestPath = $ this ->productUrlPathGenerator ->getUrlPathWithSuffix (
552- $ storeProduct ,
553- $ storeId ,
554- $ category
555- );
556- }
571+ $ this ->initializeCacheForProducts ($ products );
572+ $ requestPath = $ this ->getReqPath ($ productId , $ storeId , $ product , $ category );
557573 }
558574 $ urls [] = [
559575 $ this ->urlRewriteFactory ->create ()
@@ -662,9 +678,7 @@ private function generateForCustom(UrlRewrite $url, ?Category $category, array $
662678 : $ url ->getTargetPath ();
663679 if ((int ) $ storeId !== (int ) $ product ->getStoreId ()
664680 && $ this ->isGlobalScope ($ product ->getStoreId ())) {
665- if ($ this ->cachedValues === null ) {
666- $ this ->cachedValues = $ this ->getScopeBasedUrlKeyValues ($ products );
667- }
681+ $ this ->initializeCacheForProducts ($ products );
668682 if (!empty ($ this ->cachedValues ) && isset ($ this ->cachedValues [$ productId ][$ storeId ])) {
669683 $ storeProduct = clone $ product ;
670684 $ storeProduct ->setStoreId ($ storeId );
0 commit comments