Skip to content

Commit 9c79754

Browse files
committed
MC-39463: GraphQL caches urlResolver response and can return the old value after the url rewrite update
1 parent 9b349c5 commit 9c79754

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

app/code/Magento/UrlRewrite/Model/UrlRewrite.php

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@
77

88
namespace Magento\UrlRewrite\Model;
99

10-
use Magento\Catalog\Api\CategoryRepositoryInterface;
11-
use Magento\Catalog\Api\ProductRepositoryInterface;
1210
use Magento\Catalog\Model\Category;
1311
use Magento\Catalog\Model\Product;
14-
use Magento\Cms\Api\PageRepositoryInterface;
1512
use Magento\Cms\Model\Page;
1613
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\Data\Collection\AbstractDb;
1715
use Magento\Framework\EntityManager\EventManager;
1816
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;
1921
use Magento\Framework\Serialize\Serializer\Json;
2022
use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
23+
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection;
2124

2225
/**
2326
* UrlRewrite model class
@@ -36,32 +39,49 @@
3639
* @method UrlRewrite setStoreId($value)
3740
* @method UrlRewrite setDescription($value)
3841
*/
39-
class UrlRewrite extends \Magento\Framework\Model\AbstractModel
42+
class UrlRewrite extends AbstractModel
4043
{
4144
/**
4245
* @var Json
4346
*/
4447
private $serializer;
4548

49+
/**
50+
* @var CacheContext|mixed|null
51+
*/
52+
private $cacheContext;
53+
54+
/**
55+
* @var EventManager|mixed|null
56+
*/
57+
private $eventManager;
58+
4659
/**
4760
* UrlRewrite constructor.
4861
*
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
5366
* @param array $data
54-
* @param Json $serializer
67+
* @param Json|null $serializer
68+
* @param CacheContext|null $cacheContext
69+
* @param EventManager|null $eventManager
5570
*/
5671
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,
6176
array $data = [],
62-
Json $serializer = null
63-
) {
77+
Json $serializer = null,
78+
CacheContext $cacheContext = null,
79+
EventManager $eventManager = null
80+
)
81+
{
6482
$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);
6585
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
6686
}
6787

@@ -72,8 +92,8 @@ public function __construct(
7292
*/
7393
protected function _construct()
7494
{
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;
7797
}
7898

7999
/**
@@ -102,57 +122,37 @@ public function setMetadata($metadata)
102122
return $this->setData(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::METADATA, $metadata);
103123
}
104124

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+
];
114139

115-
$cacheContext = ObjectManager::getInstance()->get(CacheContext::class);
116-
$eventManager = ObjectManager::getInstance()->get(EventManager::class);
140+
$cacheKey = $map[$entityType];
117141

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]);
120144
}
121145
}
122146

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
151151
}
152152

153153
public function afterSave()
154154
{
155-
$this->opt1();
156-
return parent::afterSave(); // TODO: Change the autogenerated stub
155+
$this->cleanCacheForEntity($this->getEntityType(), $this->getEntityId());
156+
return parent::afterSave();
157157
}
158158
}

0 commit comments

Comments
 (0)