2
2
3
3
namespace Algolia \AlgoliaSearch \Observer ;
4
4
5
+ use Algolia \AlgoliaSearch \Exceptions \AlgoliaException ;
5
6
use Algolia \AlgoliaSearch \Helper \ConfigHelper ;
7
+ use Algolia \AlgoliaSearch \Registry \CurrentCategory ;
6
8
use Algolia \AlgoliaSearch \Service \AlgoliaCredentialsManager ;
7
9
use Magento \Catalog \Model \Category ;
8
10
use Magento \Framework \App \Request \Http ;
11
+ use Magento \Framework \Event \Observer ;
9
12
use Magento \Framework \Event \ObserverInterface ;
10
13
use Magento \Framework \Exception \NoSuchEntityException ;
11
- use Magento \Framework \Registry ;
12
14
use Magento \Framework \View \Layout ;
13
15
use Magento \Framework \View \Page \Config as PageConfig ;
14
16
use Magento \Store \Model \StoreManagerInterface ;
@@ -20,7 +22,7 @@ class AddAlgoliaAssetsObserver implements ObserverInterface
20
22
{
21
23
public function __construct (
22
24
protected ConfigHelper $ config ,
23
- protected Registry $ registry ,
25
+ protected CurrentCategory $ category ,
24
26
protected StoreManagerInterface $ storeManager ,
25
27
protected PageConfig $ pageConfig ,
26
28
protected Http $ request ,
@@ -29,13 +31,13 @@ public function __construct(
29
31
{}
30
32
31
33
/**
32
- * @throws NoSuchEntityException
34
+ * @throws NoSuchEntityException|AlgoliaException
33
35
*/
34
- public function execute (\ Magento \ Framework \ Event \ Observer $ observer )
36
+ public function execute (Observer $ observer ): void
35
37
{
36
38
$ actionName = $ this ->request ->getFullActionName ();
37
39
if ($ actionName === 'swagger_index_index ' ) {
38
- return $ this ;
40
+ return ;
39
41
}
40
42
$ storeId = $ this ->storeManager ->getStore ()->getId ();
41
43
if ($ this ->config ->isEnabledFrontEnd ($ storeId )) {
@@ -51,15 +53,33 @@ public function execute(\Magento\Framework\Event\Observer $observer)
51
53
}
52
54
}
53
55
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
55
63
{
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. " );
58
67
}
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 ();
59
81
60
- /** @var Category $category */
61
- $ category = $ this ->registry ->registry ('current_category ' );
62
- if (!$ category ) {
82
+ if (!$ category ->getId ()) {
63
83
return ;
64
84
}
65
85
0 commit comments