14
14
use Magento \Framework \Event \ObserverInterface ;
15
15
use Magento \Framework \Exception \LocalizedException ;
16
16
use Magento \Framework \Message \ManagerInterface as MessageManagerInterface ;
17
+ use Psr \Log \LoggerInterface ;
17
18
18
19
class RecommendSettings implements ObserverInterface
19
20
{
20
21
const QUANTITY_AND_STOCK_STATUS = 'quantity_and_stock_status ' ;
21
22
const STATUS = 'status ' ;
22
23
const VISIBILITY = 'visibility ' ;
23
24
25
+ const ENFORCE_VALIDATION = 0 ;
26
+
24
27
/**
25
28
* @var string
26
29
*/
@@ -40,7 +43,8 @@ public function __construct(
40
43
protected readonly RecommendManagementInterface $ recommendManagement ,
41
44
protected readonly SearchCriteriaBuilder $ searchCriteriaBuilder ,
42
45
protected readonly State $ appState ,
43
- protected readonly MessageManagerInterface $ messageManager
46
+ protected readonly MessageManagerInterface $ messageManager ,
47
+ protected readonly LoggerInterface $ logger
44
48
){}
45
49
46
50
/**
@@ -147,35 +151,94 @@ protected function validateRecommendation(string $changedPath, string $recommend
147
151
{
148
152
try {
149
153
$ recommendations = $ this ->recommendManagement ->$ recommendationMethod ($ this ->getProductId ());
150
- if ($ this ->shouldDisplayWarning ($ recommendations )) {
151
- $ this ->messageManager ->addWarningMessage (__ (
152
- "It appears that there is no trained model available for Algolia application ID %1. "
153
- . "Please verify your configuration in the Algolia Dashboard before continuing. " ,
154
- $ this ->configHelper ->getApplicationID ()
155
- ));
156
- }
154
+
155
+ $ this ->validateRecommendApiResponse ($ recommendations , $ modelName );
157
156
} catch (\Exception $ e ) {
158
- $ this ->configWriter ->save ($ changedPath , 0 );
159
- throw new LocalizedException (__ (
160
- "Unable to save %1 Recommend configuration due to the following error: %2 " ,
157
+ $ this ->handleRecommendApiException ($ e , $ modelName , $ changedPath );
158
+ }
159
+ }
160
+
161
+ /**
162
+ * If API does not return a hits response the model may not be configured correctly.
163
+ * Do not hard fail but alert the end user.
164
+ *
165
+ * @throws LocalizedException
166
+ */
167
+ protected function validateRecommendApiResponse (array $ recommendResponse , string $ modelName ): void
168
+ {
169
+ if (!array_key_exists ('hits ' , $ recommendResponse )) {
170
+ $ msg = __ (
171
+ "It appears that there is no trained %1 model available for Algolia application ID %2. "
172
+ . "Please verify your configuration in the Algolia Dashboard before continuing. " ,
173
+ $ modelName ,
174
+ $ this ->configHelper ->getApplicationID ()
175
+ );
176
+
177
+ if ($ this ->shouldDisplayWarning ()) {
178
+ $ this ->messageManager ->addWarningMessage ($ msg );
179
+ }
180
+ else {
181
+ $ this ->logger ->warning ($ msg );
182
+ }
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Handles exceptions on the test request against the Recommend API
188
+ *
189
+ * The goal is to warn the user of potential issues so they do not enable a model on the front end that has not been
190
+ * properly configured in Algolia
191
+ *
192
+ * TODO: Implement store scoped validation
193
+ *
194
+ * @throws LocalizedException
195
+ */
196
+ protected function handleRecommendApiException (\Exception $ e , string $ modelName , string $ changedPath ): void
197
+ {
198
+ $ msg = $ this ->getUserFriendlyRecommendApiErrorMessage ($ e );
199
+
200
+ if (self ::ENFORCE_VALIDATION ) {
201
+ $ this ->rollBack ($ changedPath , __ (
202
+ "Unable to save %1 Recommend configuration due to the following error: %2 " ,
161
203
$ modelName ,
162
- $ this -> getUserFriendlyRecommendApiErrorMessage ( $ e )
204
+ $ msg
163
205
)
164
206
);
165
207
}
208
+
209
+ $ msg = __ (
210
+ "The following error was encountered while enabling %1 recommendations: %2 " ,
211
+ $ modelName ,
212
+ $ msg
213
+ );
214
+
215
+ if ($ this ->shouldDisplayWarning ()) {
216
+ $ this ->messageManager ->addWarningMessage (
217
+ $ msg
218
+ . ' Please verify your configuration in the Algolia Dashboard before continuing. ' );
219
+ }
220
+ else {
221
+ $ this ->logger ->warning ($ msg );
222
+ }
223
+ }
224
+
225
+ /*
226
+ * For hard fail only
227
+ */
228
+ protected function rollBack (string $ changedPath , \Magento \Framework \Phrase $ message ): void
229
+ {
230
+ $ this ->configWriter ->save ($ changedPath , 0 );
231
+ throw new LocalizedException ($ message );
166
232
}
167
233
168
234
/**
169
- * If API does not return a hits response the model may not be configured correctly.
170
- * Do not hard fail but alert the end user.
235
+ * Warnings should only be displayed within the admin panel
171
236
* @throws LocalizedException
172
237
*/
173
- protected function shouldDisplayWarning (array $ recommendationResponse ): bool
238
+ protected function shouldDisplayWarning (): bool
174
239
{
175
- return
176
- $ this ->appState ->getAreaCode () === \Magento \Framework \App \Area::AREA_ADMINHTML
177
- &&
178
- !array_key_exists ('hits ' , $ recommendationResponse );
240
+ return $ this ->appState ->getAreaCode () === \Magento \Framework \App \Area::AREA_ADMINHTML ;
241
+
179
242
}
180
243
181
244
/**
@@ -200,6 +263,10 @@ protected function getUserFriendlyRecommendApiErrorMessage(\Exception $e): strin
200
263
}
201
264
202
265
/**
266
+ * Retrieve a test product for requests against the Recommend API
267
+ *
268
+ * TODO: Implement store scoping and independently address 404 where objectID is not found
269
+ *
203
270
* @return string - Product ID string for use in API calls
204
271
* @throws LocalizedException
205
272
*/
0 commit comments