7
7
8
8
namespace Magento \UrlRewrite \Model ;
9
9
10
- use Magento \Catalog \Api \CategoryRepositoryInterface ;
11
- use Magento \Catalog \Api \ProductRepositoryInterface ;
12
10
use Magento \Catalog \Model \Category ;
13
11
use Magento \Catalog \Model \Product ;
14
- use Magento \Cms \Api \PageRepositoryInterface ;
15
12
use Magento \Cms \Model \Page ;
16
13
use Magento \Framework \App \ObjectManager ;
14
+ use Magento \Framework \Data \Collection \AbstractDb ;
17
15
use Magento \Framework \EntityManager \EventManager ;
18
16
use Magento \Framework \Indexer \CacheContext ;
17
+ use Magento \Framework \Model \AbstractModel ;
18
+ use Magento \Framework \Model \Context ;
19
+ use Magento \Framework \Model \ResourceModel \AbstractResource ;
20
+ use Magento \Framework \Registry ;
19
21
use Magento \Framework \Serialize \Serializer \Json ;
20
22
use Magento \UrlRewrite \Controller \Adminhtml \Url \Rewrite ;
23
+ use Magento \UrlRewrite \Model \ResourceModel \UrlRewriteCollection ;
21
24
22
25
/**
23
26
* UrlRewrite model class
36
39
* @method UrlRewrite setStoreId($value)
37
40
* @method UrlRewrite setDescription($value)
38
41
*/
39
- class UrlRewrite extends \ Magento \ Framework \ Model \ AbstractModel
42
+ class UrlRewrite extends AbstractModel
40
43
{
41
44
/**
42
45
* @var Json
43
46
*/
44
47
private $ serializer ;
45
48
49
+ /**
50
+ * @var CacheContext|mixed|null
51
+ */
52
+ private $ cacheContext ;
53
+
54
+ /**
55
+ * @var EventManager|mixed|null
56
+ */
57
+ private $ eventManager ;
58
+
46
59
/**
47
60
* UrlRewrite constructor.
48
61
*
49
- * @param \Magento\Framework\Model\ Context $context
50
- * @param \Magento\Framework\ Registry $registry
51
- * @param \Magento\Framework\Model\ResourceModel\ AbstractResource|null $resource
52
- * @param \Magento\Framework\Data\Collection\ AbstractDb|null $resourceCollection
62
+ * @param Context $context
63
+ * @param Registry $registry
64
+ * @param AbstractResource|null $resource
65
+ * @param AbstractDb|null $resourceCollection
53
66
* @param array $data
54
- * @param Json $serializer
67
+ * @param Json|null $serializer
68
+ * @param CacheContext|null $cacheContext
69
+ * @param EventManager|null $eventManager
55
70
*/
56
71
public function __construct (
57
- \ Magento \ Framework \ Model \ Context $ context ,
58
- \ Magento \ Framework \ Registry $ registry ,
59
- \ Magento \ Framework \ Model \ ResourceModel \ AbstractResource $ resource = null ,
60
- \ Magento \ Framework \ Data \ Collection \ AbstractDb $ resourceCollection = null ,
72
+ Context $ context ,
73
+ Registry $ registry ,
74
+ AbstractResource $ resource = null ,
75
+ AbstractDb $ resourceCollection = null ,
61
76
array $ data = [],
62
- Json $ serializer = null
63
- ) {
77
+ Json $ serializer = null ,
78
+ CacheContext $ cacheContext = null ,
79
+ EventManager $ eventManager = null
80
+ )
81
+ {
64
82
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
83
+ $ this ->cacheContext = $ cacheContext ?: ObjectManager::getInstance ()->get (CacheContext::class);
84
+ $ this ->eventManager = $ eventManager ?: ObjectManager::getInstance ()->get (EventManager::class);
65
85
parent ::__construct ($ context , $ registry , $ resource , $ resourceCollection , $ data );
66
86
}
67
87
@@ -72,8 +92,8 @@ public function __construct(
72
92
*/
73
93
protected function _construct ()
74
94
{
75
- $ this ->_init (\ Magento \ UrlRewrite \ Model \ ResourceModel \UrlRewrite::class);
76
- $ this ->_collectionName = \ Magento \ UrlRewrite \ Model \ ResourceModel \ UrlRewriteCollection::class;
95
+ $ this ->_init (ResourceModel \UrlRewrite::class);
96
+ $ this ->_collectionName = UrlRewriteCollection::class;
77
97
}
78
98
79
99
/**
@@ -102,57 +122,37 @@ public function setMetadata($metadata)
102
122
return $ this ->setData (\Magento \UrlRewrite \Service \V1 \Data \UrlRewrite::METADATA , $ metadata );
103
123
}
104
124
105
- private function opt1 () {
106
- $ map = [
107
- Rewrite::ENTITY_TYPE_PRODUCT => Product::CACHE_TAG ,
108
- Rewrite::ENTITY_TYPE_CATEGORY => Category::CACHE_TAG ,
109
- Rewrite::ENTITY_TYPE_CMS_PAGE => Page::CACHE_TAG
110
- ];
111
-
112
- if ($ this ->getEntityType () !== Rewrite::ENTITY_TYPE_CUSTOM ) {
113
- $ cacheKey = $ map [$ this ->getEntityType ()];
125
+ /**
126
+ * Clean cache for the entity which was affected by updating UrlRewrite
127
+ *
128
+ * @param $entityType
129
+ * @param $entityId
130
+ */
131
+ private function cleanCacheForEntity ($ entityType , $ entityId )
132
+ {
133
+ if ($ entityType !== Rewrite::ENTITY_TYPE_CUSTOM ) {
134
+ $ map = [
135
+ Rewrite::ENTITY_TYPE_PRODUCT => Product::CACHE_TAG ,
136
+ Rewrite::ENTITY_TYPE_CATEGORY => Category::CACHE_TAG ,
137
+ Rewrite::ENTITY_TYPE_CMS_PAGE => Page::CACHE_TAG
138
+ ];
114
139
115
- $ cacheContext = ObjectManager::getInstance ()->get (CacheContext::class);
116
- $ eventManager = ObjectManager::getInstance ()->get (EventManager::class);
140
+ $ cacheKey = $ map [$ entityType ];
117
141
118
- $ cacheContext ->registerEntities ($ cacheKey , [$ this -> getEntityId () ]);
119
- $ eventManager ->dispatch ('clean_cache_by_tags ' , ['object ' => $ cacheContext ]);
142
+ $ this -> cacheContext ->registerEntities ($ cacheKey , [$ entityId ]);
143
+ $ this -> eventManager ->dispatch ('clean_cache_by_tags ' , ['object ' => $ this -> cacheContext ]);
120
144
}
121
145
}
122
146
123
- private function opt2 () {
124
- $ map = [
125
- Rewrite::ENTITY_TYPE_PRODUCT => function ($ prodId ) {
126
- /** @var ProductRepositoryInterface $productRepository */
127
- $ productRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
128
- return $ productRepository ->getById ($ prodId );
129
- },
130
- Rewrite::ENTITY_TYPE_CATEGORY => function ($ catId ) {
131
- /** @var CategoryRepositoryInterface $productRepository */
132
- $ categoryRepository = ObjectManager::getInstance ()->get (CategoryRepositoryInterface::class);
133
- return $ categoryRepository ->get ($ catId );
134
- },
135
- Rewrite::ENTITY_TYPE_CMS_PAGE => function ($ cmsId ) {
136
- /** @var PageRepositoryInterface $productRepository */
137
- $ pageRepository = ObjectManager::getInstance ()->get (PageRepositoryInterface::class);
138
- return $ pageRepository ->getById ($ cmsId );
139
- },
140
- Rewrite::ENTITY_TYPE_CUSTOM => false
141
- ];
142
-
143
- $ getter = $ map [$ this ->getEntityType ()];
144
-
145
- if ($ getter ) {
146
- $ entity = $ getter ($ this ->getEntityId ());
147
-
148
- $ entityManager = ObjectManager::getInstance ()->get (EventManager::class);
149
- $ entityManager ->dispatch ('clean_cache_by_tags ' , ['object ' => $ entity ]);
150
- }
147
+ public function afterDelete ()
148
+ {
149
+ $ this ->cleanCacheForEntity ($ this ->getEntityType (), $ this ->getEntityId ());
150
+ return parent ::afterDelete (); // TODO: Change the autogenerated stub
151
151
}
152
152
153
153
public function afterSave ()
154
154
{
155
- $ this ->opt1 ( );
156
- return parent ::afterSave (); // TODO: Change the autogenerated stub
155
+ $ this ->cleanCacheForEntity ( $ this -> getEntityType (), $ this -> getEntityId () );
156
+ return parent ::afterSave ();
157
157
}
158
158
}
0 commit comments