@@ -98,18 +98,7 @@ public function execute(Observer $observer)
98
98
*/
99
99
protected function validateFrequentlyBroughtTogether (string $ changedPath ): void
100
100
{
101
- try {
102
- $ recommendations = $ this ->recommendManagement ->getBoughtTogetherRecommendation ($ this ->getProductId ());
103
- if (empty ($ recommendations ['renderingContent ' ])) {
104
- throw new LocalizedException (__ (
105
- "It appears that there is no trained model available for Algolia application ID %1. " ,
106
- $ this ->configHelper ->getApplicationID ()
107
- ));
108
- }
109
- } catch (\Exception $ e ) {
110
- $ this ->configWriter ->save ($ changedPath , 0 );
111
- throw new LocalizedException (__ ("Unable to save FBT Recommend configuration due to the following error: " . $ e ->getMessage ()));
112
- }
101
+ $ this ->validateRecommendation ($ changedPath , 'getBoughtTogetherRecommendation ' , 'Frequently Bought Together ' );
113
102
}
114
103
115
104
/**
@@ -119,18 +108,7 @@ protected function validateFrequentlyBroughtTogether(string $changedPath): void
119
108
*/
120
109
protected function validateRelatedProducts (string $ changedPath ): void
121
110
{
122
- try {
123
- $ recommendations = $ this ->recommendManagement ->getRelatedProductsRecommendation ($ this ->getProductId ());
124
- if (empty ($ recommendations ['renderingContent ' ])) {
125
- throw new LocalizedException (__ (
126
- "It appears that there is no trained model available for Algolia application ID: %1. " ,
127
- $ this ->configHelper ->getApplicationID ()
128
- ));
129
- }
130
- } catch (\Exception $ e ) {
131
- $ this ->configWriter ->save ($ changedPath , 0 );
132
- throw new LocalizedException (__ ("Unable to save Related Products Recommend configuration due to the following error: " . $ e ->getMessage ()));
133
- }
111
+ $ this ->validateRecommendation ($ changedPath , 'getRelatedProductsRecommendation ' , 'Related Products ' );
134
112
}
135
113
136
114
/**
@@ -140,19 +118,7 @@ protected function validateRelatedProducts(string $changedPath): void
140
118
*/
141
119
protected function validateTrendingItems (string $ changedPath ): void
142
120
{
143
- try {
144
- $ recommendations = $ this ->recommendManagement ->getTrendingItemsRecommendation ();
145
- // When no recommendations suggested, most likely trained model is missing
146
- if (empty ($ recommendations ['renderingContent ' ])) {
147
- throw new LocalizedException (__ (
148
- "It appears that there is no trained model available for Algolia application ID: %1. " ,
149
- $ this ->configHelper ->getApplicationID ()
150
- ));
151
- }
152
- } catch (\Exception $ e ) {
153
- $ this ->configWriter ->save ($ changedPath , 0 );
154
- throw new LocalizedException (__ ("Unable to save Trending Items Recommend configuration due to the following error: " . $ e ->getMessage ()));
155
- }
121
+ $ this ->validateRecommendation ($ changedPath , 'getTrendingItemsRecommendation ' , 'Trending Items ' );
156
122
}
157
123
158
124
/**
@@ -161,18 +127,35 @@ protected function validateTrendingItems(string $changedPath): void
161
127
* @throws LocalizedException
162
128
*/
163
129
protected function validateLookingSimilar (string $ changedPath ): void
130
+ {
131
+ $ this ->validateRecommendation ($ changedPath , 'getLookingSimilarRecommendation ' , 'Looking Similar ' );
132
+ }
133
+
134
+ /**
135
+ * @param string $changedPath - config path to be reverted if validation failed
136
+ * @param string $recommendationMethod - name of method to call to retrieve method from RecommendManagementInterface
137
+ * @param string $modelName - user friendly name to refer to model in error messaging
138
+ * @return void
139
+ * @throws LocalizedException
140
+ */
141
+ protected function validateRecommendation (string $ changedPath , string $ recommendationMethod , string $ modelName ): void
164
142
{
165
143
try {
166
- $ recommendations = $ this ->recommendManagement ->getLookingSimilarRecommendation ($ this ->getProductId ());
144
+ $ recommendations = $ this ->recommendManagement ->$ recommendationMethod ($ this ->getProductId ());
167
145
if (empty ($ recommendations ['renderingContent ' ])) {
168
146
throw new LocalizedException (__ (
169
- "It appears that there is no trained model available for Algolia application ID: %1. " ,
147
+ "It appears that there is no trained model available for Algolia application ID %1. " ,
170
148
$ this ->configHelper ->getApplicationID ()
171
149
));
172
150
}
173
151
} catch (\Exception $ e ) {
174
152
$ this ->configWriter ->save ($ changedPath , 0 );
175
- throw new LocalizedException (__ ("Unable to save Looking Similar Recommend configuration due to the following error: " . $ e ->getMessage ()));
153
+ throw new LocalizedException (__ (
154
+ "Unable to save %1 Recommend configuration due to the following error: %2 " ,
155
+ $ modelName ,
156
+ $ e ->getMessage ()
157
+ )
158
+ );
176
159
}
177
160
}
178
161
0 commit comments