Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductUrlRewriteGenerator.php
Go to the documentation of this file.
1 <?php
7 
15 
22 {
26  const ENTITY_TYPE = 'product';
27 
32  protected $storeViewService;
33 
38  protected $product;
39 
45 
51 
57 
63 
68  protected $productCategories;
69 
74  protected $storeManager;
75 
79  private $productScopeRewriteGenerator;
80 
89  public function __construct(
93  ObjectRegistryFactory $objectRegistryFactory,
96  ) {
97  $this->canonicalUrlRewriteGenerator = $canonicalUrlRewriteGenerator;
98  $this->currentUrlRewritesRegenerator = $currentUrlRewritesRegenerator;
99  $this->categoriesUrlRewriteGenerator = $categoriesUrlRewriteGenerator;
100  $this->objectRegistryFactory = $objectRegistryFactory;
101  $this->storeViewService = $storeViewService;
102  $this->storeManager = $storeManager;
103  }
104 
111  private function getProductScopeRewriteGenerator()
112  {
113  if (!$this->productScopeRewriteGenerator) {
114  $this->productScopeRewriteGenerator = ObjectManager::getInstance()
115  ->get(ProductScopeRewriteGenerator::class);
116  }
117 
118  return $this->productScopeRewriteGenerator;
119  }
120 
128  public function generate(Product $product, $rootCategoryId = null)
129  {
130  if ($product->getVisibility() == Visibility::VISIBILITY_NOT_VISIBLE) {
131  return [];
132  }
133 
134  $storeId = $product->getStoreId();
135 
136  $productCategories = $product->getCategoryCollection()
137  ->addAttributeToSelect('url_key')
138  ->addAttributeToSelect('url_path');
139 
140  $urls = $this->isGlobalScope($storeId)
143 
144  return $urls;
145  }
146 
154  protected function isGlobalScope($storeId)
155  {
156  return $this->getProductScopeRewriteGenerator()->isGlobalScope($storeId);
157  }
158 
169  {
170  return $this->getProductScopeRewriteGenerator()->generateForGlobalScope(
172  $product,
174  );
175  }
176 
187  protected function generateForSpecificStoreView(
188  $storeId,
190  $product = null,
191  $rootCategoryId = null
192  ) {
193  return $this->getProductScopeRewriteGenerator()
194  ->generateForSpecificStoreView($storeId, $productCategories, $product, $rootCategoryId);
195  }
196 
204  {
205  return $this->getProductScopeRewriteGenerator()->isCategoryProperForGenerating($category, $storeId);
206  }
207 }
generateForGlobalScope($productCategories, $product=null, $rootCategoryId=null)
generateForSpecificStoreView( $storeId, $productCategories, $product=null, $rootCategoryId=null)
__construct(CanonicalUrlRewriteGenerator $canonicalUrlRewriteGenerator, CurrentUrlRewritesRegenerator $currentUrlRewritesRegenerator, CategoriesUrlRewriteGenerator $categoriesUrlRewriteGenerator, ObjectRegistryFactory $objectRegistryFactory, StoreViewService $storeViewService, \Magento\Store\Model\StoreManagerInterface $storeManager)