Skip to content

Commit 9b4bf8e

Browse files
committed
MAGE-1326: refactor assets observer and introduce RenderingManager class
1 parent 4fe2979 commit 9b4bf8e

File tree

2 files changed

+97
-40
lines changed

2 files changed

+97
-40
lines changed

Observer/AddAlgoliaAssetsObserver.php

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
66
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
7-
use Algolia\AlgoliaSearch\Registry\CurrentCategory;
87
use Algolia\AlgoliaSearch\Service\AlgoliaCredentialsManager;
9-
use Magento\Catalog\Model\Category;
8+
use Algolia\AlgoliaSearch\Service\RenderingManager;
109
use Magento\Framework\App\Request\Http;
1110
use Magento\Framework\Event\Observer;
1211
use Magento\Framework\Event\ObserverInterface;
1312
use Magento\Framework\Exception\NoSuchEntityException;
1413
use Magento\Framework\View\Layout;
15-
use Magento\Framework\View\Page\Config as PageConfig;
1614
use Magento\Store\Model\StoreManagerInterface;
1715

1816
/**
@@ -22,13 +20,11 @@ class AddAlgoliaAssetsObserver implements ObserverInterface
2220
{
2321
public function __construct(
2422
protected ConfigHelper $config,
25-
protected CurrentCategory $category,
23+
protected RenderingManager $renderingManager,
2624
protected StoreManagerInterface $storeManager,
27-
protected PageConfig $pageConfig,
2825
protected Http $request,
2926
protected AlgoliaCredentialsManager $algoliaCredentialsManager
30-
)
31-
{}
27+
) {}
3228

3329
/**
3430
* @throws NoSuchEntityException|AlgoliaException
@@ -39,42 +35,16 @@ public function execute(Observer $observer): void
3935
if ($actionName === 'swagger_index_index') {
4036
return;
4137
}
42-
$storeId = $this->storeManager->getStore()->getId();
43-
if ($this->config->isEnabledFrontEnd($storeId)) {
44-
if ($this->algoliaCredentialsManager->checkCredentials($storeId)) {
45-
if ($this->config->isAutoCompleteEnabled($storeId) || $this->config->isInstantEnabled($storeId)) {
46-
/** @var Layout $layout */
47-
$layout = $observer->getData('layout');
48-
$layout->getUpdate()->addHandle('algolia_search_handle');
49-
50-
$this->loadPreventBackendRenderingHandle($layout, $storeId);
51-
}
52-
}
53-
}
54-
}
55-
56-
private function loadPreventBackendRenderingHandle(Layout $layout, int $storeId): void
57-
{
58-
if (!$this->config->preventBackendRendering($storeId)) {
59-
return;
60-
}
61-
62-
$category = $this->category->get();
6338

64-
if (!$category->getId()) {
65-
return;
66-
}
39+
$storeId = $this->storeManager->getStore()->getId();
6740

68-
if (!$this->config->replaceCategories($storeId)) {
69-
return;
70-
}
41+
if ($this->config->isEnabledFrontEnd($storeId) && $this->algoliaCredentialsManager->checkCredentials($storeId)) {
42+
/** @var Layout $layout */
43+
$layout = $observer->getData('layout');
7144

72-
$displayMode = $this->config->getBackendRenderingDisplayMode($storeId);
73-
if ($displayMode === 'only_products'
74-
&& $category->getData('display_mode') === \Magento\Catalog\Model\Category::DM_PAGE) {
75-
return;
45+
$this->renderingManager->handleFrontendAssets($layout, $storeId);
46+
$this->renderingManager->handleBackendRendering($layout, $actionName, $storeId);
7647
}
77-
78-
$layout->getUpdate()->addHandle('algolia_search_handle_prevent_backend_rendering');
7948
}
49+
8050
}

Service/RenderingManager.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Service;
4+
5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Algolia\AlgoliaSearch\Helper\Configuration\AutocompleteHelper;
7+
use Algolia\AlgoliaSearch\Helper\Configuration\InstantSearchHelper;
8+
use Algolia\AlgoliaSearch\Registry\CurrentCategory;
9+
use Magento\Catalog\Model\Category;
10+
use Magento\Framework\View\Layout;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
13+
class RenderingManager
14+
{
15+
public function __construct(
16+
protected ConfigHelper $config,
17+
protected AutocompleteHelper $autocompleteConfigHelper,
18+
protected InstantSearchHelper $instantSearchConfigHelper,
19+
protected CurrentCategory $category,
20+
protected StoreManagerInterface $storeManager
21+
) {}
22+
23+
/**
24+
* @param Layout $layout
25+
* @param int $storeId
26+
* @return void
27+
*/
28+
public function handleFrontendAssets(Layout $layout, int $storeId): void
29+
{
30+
// If an Algolia frontend feature is enabled, add the frontend assets
31+
if (!$this->hasAlgoliaFrontend($storeId)) {
32+
return;
33+
}
34+
35+
$layout->getUpdate()->addHandle('algolia_search_handle');
36+
}
37+
38+
/**
39+
* @param Layout $layout
40+
* @param string $actionName
41+
* @param int $storeId
42+
* @return void
43+
*/
44+
public function handleBackendRendering(Layout $layout, string $actionName, int $storeId): void
45+
{
46+
// If the page is not a category or the catalogsearch page or if no Algolia frontend feature is enabled, no need to go further
47+
if (!$this->isSearchPage($actionName) || !$this->hasAlgoliaFrontend($storeId)) {
48+
return;
49+
}
50+
51+
// @todo replace this check with the new backend rendering feature (MAGE-1325)
52+
if (!$this->config->preventBackendRendering($storeId)) {
53+
return;
54+
}
55+
56+
$category = $this->category->get();
57+
// Legacy check regarding category display mode (we don't want to hide the static blocks if there's not product list
58+
if ($category->getId() && $this->instantSearchConfigHelper->shouldReplaceCategories($storeId)) {
59+
$displayMode = $this->config->getBackendRenderingDisplayMode($storeId);
60+
61+
if ($displayMode === 'only_products' && $category->getData('display_mode') === Category::DM_PAGE) {
62+
return;
63+
}
64+
}
65+
66+
$layout->getUpdate()->addHandle('algolia_search_handle_prevent_backend_rendering');
67+
}
68+
69+
/**
70+
* @param int $storeId
71+
* @return bool
72+
*/
73+
protected function hasAlgoliaFrontend(int $storeId): bool
74+
{
75+
return $this->autocompleteConfigHelper->isEnabled($storeId) ||
76+
$this->instantSearchConfigHelper->isEnabled($storeId);
77+
}
78+
79+
/**
80+
* @param string $actionName
81+
* @return bool
82+
*/
83+
protected function isSearchPage(string $actionName): bool
84+
{
85+
return $actionName === 'catalog_category_view' || $actionName === 'catalogsearch_result_index';
86+
}
87+
}

0 commit comments

Comments
 (0)