Skip to content

Commit 3bfcfc3

Browse files
committed
MAGE-1104 Replace deprecated registry with observer based category retrieval
1 parent dc34b82 commit 3bfcfc3

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

Observer/AddAlgoliaAssetsObserver.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Algolia\AlgoliaSearch\Observer;
44

5+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
56
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
7+
use Algolia\AlgoliaSearch\Registry\CurrentCategory;
68
use Algolia\AlgoliaSearch\Service\AlgoliaCredentialsManager;
79
use Magento\Catalog\Model\Category;
810
use Magento\Framework\App\Request\Http;
11+
use Magento\Framework\Event\Observer;
912
use Magento\Framework\Event\ObserverInterface;
1013
use Magento\Framework\Exception\NoSuchEntityException;
11-
use Magento\Framework\Registry;
1214
use Magento\Framework\View\Layout;
1315
use Magento\Framework\View\Page\Config as PageConfig;
1416
use Magento\Store\Model\StoreManagerInterface;
@@ -20,7 +22,7 @@ class AddAlgoliaAssetsObserver implements ObserverInterface
2022
{
2123
public function __construct(
2224
protected ConfigHelper $config,
23-
protected Registry $registry,
25+
protected CurrentCategory $category,
2426
protected StoreManagerInterface $storeManager,
2527
protected PageConfig $pageConfig,
2628
protected Http $request,
@@ -29,13 +31,13 @@ public function __construct(
2931
{}
3032

3133
/**
32-
* @throws NoSuchEntityException
34+
* @throws NoSuchEntityException|AlgoliaException
3335
*/
34-
public function execute(\Magento\Framework\Event\Observer $observer)
36+
public function execute(Observer $observer): void
3537
{
3638
$actionName = $this->request->getFullActionName();
3739
if ($actionName === 'swagger_index_index') {
38-
return $this;
40+
return;
3941
}
4042
$storeId = $this->storeManager->getStore()->getId();
4143
if ($this->config->isEnabledFrontEnd($storeId)) {
@@ -51,15 +53,33 @@ public function execute(\Magento\Framework\Event\Observer $observer)
5153
}
5254
}
5355

54-
private function loadPreventBackendRenderingHandle(Layout $layout, int $storeId): void
56+
/**
57+
* Repository does not guarantee this underlying model but only the interface
58+
* but getDisplayMode is only available on model
59+
*
60+
* @throws AlgoliaException
61+
*/
62+
public function getCategory(): Category
5563
{
56-
if ($this->config->preventBackendRendering($storeId) === false) {
57-
return;
64+
$category = $this->category->get();
65+
if (!$category instanceof Category) {
66+
throw new AlgoliaException("Unexpected category object encountered.");
5867
}
68+
return $category;
69+
}
70+
71+
/**
72+
* @throws AlgoliaException
73+
*/
74+
private function loadPreventBackendRenderingHandle(Layout $layout, int $storeId): void
75+
{
76+
if (!$this->config->preventBackendRendering($storeId)) {
77+
return;
78+
}
79+
80+
$category = $this->getCategory();
5981

60-
/** @var Category $category */
61-
$category = $this->registry->registry('current_category');
62-
if (!$category) {
82+
if (!$category->getId()) {
6383
return;
6484
}
6585

Observer/RegisterCurrentCategoryObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(CurrentCategory $currentCategory) {
2121
*/
2222
public function execute(Observer $observer)
2323
{
24-
/** @var CategoryInterface */
24+
/** @var $category CategoryInterface */
2525
$category = $observer->getEvent()->getData('category');
2626
$this->currentCategory->set($category);
2727
}

0 commit comments

Comments
 (0)