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
23
27
class View
24
28
{
25
29
/**
26
- * @var \Magento\UrlRewrite\Model\ UrlPersistInterface
30
+ * @var UrlPersistInterface
27
31
*/
28
32
protected $ urlPersist ;
29
33
30
34
/**
31
- * @var \Magento\Catalog\Model\ CategoryFactory
35
+ * @var CategoryFactory
32
36
*/
33
37
protected $ categoryFactory ;
34
38
35
39
/**
36
- * @var \Magento\Catalog\Model\ ProductFactory
40
+ * @var ProductFactory
37
41
*/
38
42
protected $ productFactory ;
39
43
@@ -43,7 +47,7 @@ class View
43
47
protected $ categoryUrlRewriteGenerator ;
44
48
45
49
/**
46
- * @var \Magento\CatalogUrlRewrite\Model\ ProductUrlRewriteGenerator
50
+ * @var ProductUrlRewriteGenerator
47
51
*/
48
52
protected $ productUrlRewriteGenerator ;
49
53
@@ -76,70 +80,62 @@ public function __construct(
76
80
/**
77
81
* Setter for Orig Store data
78
82
*
79
- * @param \Magento\Store\Model\ResourceModel\ Store $object
83
+ * @param Store $object
80
84
* @param AbstractModel $store
81
85
* @return void
82
86
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
83
87
*/
84
88
public function beforeSave (
85
- \ Magento \ Store \ Model \ ResourceModel \ Store $ object ,
89
+ Store $ object ,
86
90
AbstractModel $ store
87
- ) {
91
+ ): void {
88
92
$ this ->origStore = $ store ;
89
93
}
90
94
91
95
/**
92
96
* Regenerate urls on store after save
93
97
*
94
- * @param \Magento\Store\Model\ResourceModel\ Store $object
95
- * @param \Magento\Store\Model\ResourceModel\ Store $store
96
- * @return \Magento\Store\Model\ResourceModel\ Store
98
+ * @param Store $object
99
+ * @param Store $store
100
+ * @return Store
97
101
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
98
102
*/
99
103
public function afterSave (
100
- \ Magento \ Store \ Model \ ResourceModel \ Store $ object ,
101
- \ Magento \ Store \ Model \ ResourceModel \ Store $ store
102
- ) {
104
+ Store $ object ,
105
+ Store $ store
106
+ ): Store {
103
107
if ($ this ->origStore ->isObjectNew () || $ this ->origStore ->dataHasChangedFor ('group_id ' )) {
104
108
$ categoryRewriteUrls = $ this ->generateCategoryUrls (
105
- $ this ->origStore ->getRootCategoryId (),
106
- $ this ->origStore ->getId ()
109
+ ( int ) $ this ->origStore ->getRootCategoryId (),
110
+ ( int ) $ this ->origStore ->getId ()
107
111
);
108
112
109
113
$ this ->urlPersist ->replace ($ categoryRewriteUrls );
110
114
111
115
$ this ->urlPersist ->replace (
112
- $ this ->generateProductUrls (
113
- $ this ->origStore ->getWebsiteId (),
114
- $ this ->origStore ->getOrigData ('website_id ' ),
115
- $ this ->origStore ->getId ()
116
- )
116
+ $ this ->generateProductUrls ((int )$ this ->origStore ->getId ())
117
117
);
118
118
}
119
+
119
120
return $ store ;
120
121
}
121
122
122
123
/**
123
- * Generate url rewrites for products assigned to website
124
+ * Generate url rewrites for products assigned to store
124
125
*
125
- * @param int $websiteId
126
- * @param int $originWebsiteId
127
126
* @param int $storeId
128
127
* @return array
129
128
*/
130
- protected function generateProductUrls ($ websiteId , $ originWebsiteId , $ storeId )
129
+ protected function generateProductUrls (int $ storeId ): array
131
130
{
132
131
$ urls = [];
133
- $ websiteIds = $ websiteId != $ originWebsiteId && $ originWebsiteId !== null
134
- ? [$ websiteId , $ originWebsiteId ]
135
- : [$ websiteId ];
136
132
$ collection = $ this ->productFactory ->create ()
137
133
->getCollection ()
138
134
->addCategoryIds ()
139
135
->addAttributeToSelect (['name ' , 'url_path ' , 'url_key ' , 'visibility ' ])
140
- ->addWebsiteFilter ( $ websiteIds );
136
+ ->addStoreFilter ( $ storeId );
141
137
foreach ($ collection as $ product ) {
142
- /** @var \Magento\Catalog\Model\ Product $product */
138
+ /** @var Product $product */
143
139
$ product ->setStoreId ($ storeId );
144
140
$ urls [] = $ this ->productUrlRewriteGenerator ->generate ($ product );
145
141
}
@@ -149,13 +145,13 @@ protected function generateProductUrls($websiteId, $originWebsiteId, $storeId)
149
145
}
150
146
151
147
/**
152
- * Generate url rewrites for categories
148
+ * Generate url rewrites for categories assigned to store
153
149
*
154
150
* @param int $rootCategoryId
155
151
* @param int $storeId
156
152
* @return array
157
153
*/
158
- protected function generateCategoryUrls ($ rootCategoryId , $ storeId )
154
+ protected function generateCategoryUrls (int $ rootCategoryId , int $ storeId ): array
159
155
{
160
156
$ urls = [];
161
157
$ categories = $ this ->categoryFactory ->create ()->getCategories ($ rootCategoryId , 1 , false , true , false );
@@ -173,17 +169,17 @@ protected function generateCategoryUrls($rootCategoryId, $storeId)
173
169
/**
174
170
* Delete unused url rewrites
175
171
*
176
- * @param \Magento\Store\Model\ResourceModel\ Store $subject
177
- * @param \Magento\Store\Model\ResourceModel\ Store $result
172
+ * @param Store $subject
173
+ * @param Store $result
178
174
* @param AbstractModel $store
179
- * @return \Magento\Store\Model\ResourceModel\ Store
175
+ * @return Store
180
176
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
181
177
*/
182
178
public function afterDelete (
183
- \ Magento \ Store \ Model \ ResourceModel \ Store $ subject ,
184
- \ Magento \ Store \ Model \ ResourceModel \ Store $ result ,
179
+ Store $ subject ,
180
+ Store $ result ,
185
181
AbstractModel $ store
186
- ) {
182
+ ): Store {
187
183
$ this ->urlPersist ->deleteByData ([UrlRewrite::STORE_ID => $ store ->getId ()]);
188
184
189
185
return $ result ;
0 commit comments