|
3 | 3 | * Copyright © Magento, Inc. All rights reserved. |
4 | 4 | * See COPYING.txt for license details. |
5 | 5 | */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
6 | 8 | namespace Magento\CatalogUrlRewrite\Model\Category\Plugin\Store; |
7 | 9 |
|
8 | 10 | use Magento\Catalog\Model\Category; |
9 | 11 | use Magento\Catalog\Model\CategoryFactory; |
| 12 | +use Magento\Catalog\Model\Product; |
10 | 13 | use Magento\Catalog\Model\ProductFactory; |
11 | 14 | use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; |
12 | 15 | use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; |
13 | 16 | use Magento\Framework\Model\AbstractModel; |
| 17 | +use Magento\Store\Model\ResourceModel\Store; |
14 | 18 | use Magento\UrlRewrite\Model\UrlPersistInterface; |
15 | 19 | use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; |
16 | 20 |
|
|
19 | 23 | * |
20 | 24 | * @see \Magento\Store\Model\ResourceModel\Store |
21 | 25 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
22 | | - * @package Magento\CatalogUrlRewrite\Model\Category\Plugin\Store |
23 | 26 | */ |
24 | 27 | class View |
25 | 28 | { |
26 | 29 | /** |
27 | | - * @var \Magento\UrlRewrite\Model\UrlPersistInterface |
| 30 | + * @var UrlPersistInterface |
28 | 31 | */ |
29 | 32 | protected $urlPersist; |
30 | 33 |
|
31 | 34 | /** |
32 | | - * @var \Magento\Catalog\Model\CategoryFactory |
| 35 | + * @var CategoryFactory |
33 | 36 | */ |
34 | 37 | protected $categoryFactory; |
35 | 38 |
|
36 | 39 | /** |
37 | | - * @var \Magento\Catalog\Model\ProductFactory |
| 40 | + * @var ProductFactory |
38 | 41 | */ |
39 | 42 | protected $productFactory; |
40 | 43 |
|
41 | 44 | /** |
42 | | - * @var \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator |
| 45 | + * @var CategoryUrlRewriteGenerator |
43 | 46 | */ |
44 | 47 | protected $categoryUrlRewriteGenerator; |
45 | 48 |
|
46 | 49 | /** |
47 | | - * @var \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator |
| 50 | + * @var ProductUrlRewriteGenerator |
48 | 51 | */ |
49 | 52 | protected $productUrlRewriteGenerator; |
50 | 53 |
|
@@ -75,114 +78,116 @@ public function __construct( |
75 | 78 | } |
76 | 79 |
|
77 | 80 | /** |
78 | | - * @param \Magento\Store\Model\ResourceModel\Store $object |
| 81 | + * Set original store before saving |
| 82 | + * |
| 83 | + * @param Store $object |
79 | 84 | * @param AbstractModel $store |
80 | 85 | * @return void |
81 | 86 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
82 | 87 | */ |
83 | 88 | public function beforeSave( |
84 | | - \Magento\Store\Model\ResourceModel\Store $object, |
| 89 | + Store $object, |
85 | 90 | AbstractModel $store |
86 | | - ) { |
| 91 | + ): void { |
87 | 92 | $this->origStore = $store; |
88 | 93 | } |
89 | 94 |
|
90 | 95 | /** |
91 | 96 | * Regenerate urls on store after save |
92 | 97 | * |
93 | | - * @param \Magento\Store\Model\ResourceModel\Store $object |
94 | | - * @param \Magento\Store\Model\ResourceModel\Store $store |
95 | | - * @return \Magento\Store\Model\ResourceModel\Store |
| 98 | + * @param Store $object |
| 99 | + * @param Store $store |
| 100 | + * @return Store |
96 | 101 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
97 | 102 | */ |
98 | 103 | public function afterSave( |
99 | | - \Magento\Store\Model\ResourceModel\Store $object, |
100 | | - \Magento\Store\Model\ResourceModel\Store $store |
101 | | - ) { |
| 104 | + Store $object, |
| 105 | + Store $store |
| 106 | + ): Store { |
102 | 107 | if ($this->origStore->isObjectNew() || $this->origStore->dataHasChangedFor('group_id')) { |
103 | 108 | if (!$this->origStore->isObjectNew()) { |
104 | | - $this->urlPersist->deleteByData([UrlRewrite::STORE_ID => $this->origStore->getId()]); |
| 109 | + $this->urlPersist->deleteByData([ |
| 110 | + UrlRewrite::STORE_ID => $this->origStore->getId(), |
| 111 | + UrlRewrite::ENTITY_TYPE => [ |
| 112 | + CategoryUrlRewriteGenerator::ENTITY_TYPE, |
| 113 | + ProductUrlRewriteGenerator::ENTITY_TYPE, |
| 114 | + ], |
| 115 | + ]); |
105 | 116 | } |
106 | 117 |
|
107 | 118 | $this->urlPersist->replace( |
108 | | - $this->generateCategoryUrls($this->origStore->getRootCategoryId(), $this->origStore->getId()) |
| 119 | + $this->generateCategoryUrls((int)$this->origStore->getRootCategoryId(), (int)$this->origStore->getId()) |
109 | 120 | ); |
110 | 121 |
|
111 | 122 | $this->urlPersist->replace( |
112 | | - $this->generateProductUrls( |
113 | | - $this->origStore->getWebsiteId(), |
114 | | - $this->origStore->getOrigData('website_id'), |
115 | | - $this->origStore->getId() |
116 | | - ) |
| 123 | + $this->generateProductUrls((int)$this->origStore->getId()) |
117 | 124 | ); |
118 | 125 | } |
| 126 | + |
119 | 127 | return $store; |
120 | 128 | } |
121 | 129 |
|
122 | 130 | /** |
123 | | - * Generate url rewrites for products assigned to website |
| 131 | + * Generate url rewrites for products assigned to store |
124 | 132 | * |
125 | | - * @param int $websiteId |
126 | | - * @param int $originWebsiteId |
127 | 133 | * @param int $storeId |
128 | 134 | * @return array |
129 | 135 | */ |
130 | | - protected function generateProductUrls($websiteId, $originWebsiteId, $storeId) |
| 136 | + protected function generateProductUrls(int $storeId): array |
131 | 137 | { |
132 | 138 | $urls = []; |
133 | | - $websiteIds = $websiteId != $originWebsiteId && $originWebsiteId !== null |
134 | | - ? [$websiteId, $originWebsiteId] |
135 | | - : [$websiteId]; |
| 139 | + $rewrites = []; |
136 | 140 | $collection = $this->productFactory->create() |
137 | 141 | ->getCollection() |
138 | 142 | ->addCategoryIds() |
139 | 143 | ->addAttributeToSelect(['name', 'url_path', 'url_key', 'visibility']) |
140 | | - ->addWebsiteFilter($websiteIds); |
| 144 | + ->addStoreFilter($storeId); |
141 | 145 | foreach ($collection as $product) { |
142 | 146 | $product->setStoreId($storeId); |
143 | | - /** @var \Magento\Catalog\Model\Product $product */ |
144 | | - $urls = array_merge( |
145 | | - $urls, |
146 | | - $this->productUrlRewriteGenerator->generate($product) |
147 | | - ); |
| 147 | + /** @var Product $product */ |
| 148 | + $rewrites[] = $this->productUrlRewriteGenerator->generate($product); |
148 | 149 | } |
| 150 | + $urls = array_merge($urls, ...$rewrites); |
| 151 | + |
149 | 152 | return $urls; |
150 | 153 | } |
151 | 154 |
|
152 | 155 | /** |
| 156 | + * Generate url rewrites for categories assigned to store |
| 157 | + * |
153 | 158 | * @param int $rootCategoryId |
154 | 159 | * @param int $storeId |
155 | 160 | * @return array |
156 | 161 | */ |
157 | | - protected function generateCategoryUrls($rootCategoryId, $storeId) |
| 162 | + protected function generateCategoryUrls(int $rootCategoryId, int $storeId): array |
158 | 163 | { |
159 | 164 | $urls = []; |
| 165 | + $rewrites = []; |
160 | 166 | $categories = $this->categoryFactory->create()->getCategories($rootCategoryId, 1, false, true); |
161 | 167 | foreach ($categories as $category) { |
162 | | - /** @var \Magento\Catalog\Model\Category $category */ |
| 168 | + /** @var Category $category */ |
163 | 169 | $category->setStoreId($storeId); |
164 | | - $urls = array_merge( |
165 | | - $urls, |
166 | | - $this->categoryUrlRewriteGenerator->generate($category) |
167 | | - ); |
| 170 | + $rewrites[] = $this->categoryUrlRewriteGenerator->generate($category); |
168 | 171 | } |
| 172 | + $urls = array_merge($urls, ...$rewrites); |
| 173 | + |
169 | 174 | return $urls; |
170 | 175 | } |
171 | 176 |
|
172 | 177 | /** |
173 | 178 | * Delete unused url rewrites |
174 | 179 | * |
175 | | - * @param \Magento\Store\Model\ResourceModel\Store $subject |
176 | | - * @param \Magento\Store\Model\ResourceModel\Store $result |
| 180 | + * @param Store $subject |
| 181 | + * @param Store $result |
177 | 182 | * @param AbstractModel $store |
178 | | - * @return \Magento\Store\Model\ResourceModel\Store |
| 183 | + * @return Store |
179 | 184 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
180 | 185 | */ |
181 | 186 | public function afterDelete( |
182 | | - \Magento\Store\Model\ResourceModel\Store $subject, |
183 | | - \Magento\Store\Model\ResourceModel\Store $result, |
| 187 | + Store $subject, |
| 188 | + Store $result, |
184 | 189 | AbstractModel $store |
185 | | - ) { |
| 190 | + ): Store { |
186 | 191 | $this->urlPersist->deleteByData([UrlRewrite::STORE_ID => $store->getId()]); |
187 | 192 |
|
188 | 193 | return $result; |
|
0 commit comments