@@ -80,6 +80,8 @@ class Api
80
80
*/
81
81
private $ state ;
82
82
83
+ private $ resultJson ;
84
+
83
85
/**
84
86
* Api constructor.
85
87
*
@@ -150,12 +152,14 @@ private function _getWafEndpoint()
150
152
* Purge a single URL
151
153
*
152
154
* @param $url
153
- * @return bool
155
+ * @return \Magento\Framework\Controller\Result\Json
154
156
* @throws \Zend_Uri_Exception
155
157
*/
156
158
public function cleanUrl ($ url )
157
159
{
158
- if ($ result = $ this ->_purge ($ url , 'PURGE ' )) {
160
+ $ result = $ this ->_purge ($ url , 'PURGE ' );
161
+
162
+ if ($ result ['status ' ]) {
159
163
$ this ->logger ->execute ($ url );
160
164
}
161
165
@@ -170,7 +174,7 @@ public function cleanUrl($url)
170
174
* Purge Fastly by a given surrogate key
171
175
*
172
176
* @param $keys
173
- * @return bool
177
+ * @return bool|\Magento\Framework\Controller\Result\Json
174
178
* @throws \Zend_Uri_Exception
175
179
*/
176
180
public function cleanBySurrogateKey ($ keys )
@@ -191,7 +195,8 @@ public function cleanBySurrogateKey($keys)
191
195
192
196
foreach ($ collection as $ keys ) {
193
197
$ payload = json_encode (['surrogate_keys ' => $ keys ]);
194
- if ($ result = $ this ->_purge ($ uri , null , \Zend_Http_Client::POST , $ payload )) {
198
+ $ result = $ this ->_purge ($ uri , null , \Zend_Http_Client::POST , $ payload );
199
+ if ($ result ['status ' ]) {
195
200
foreach ($ keys as $ key ) {
196
201
$ this ->logger ->execute ('surrogate key: ' . $ key );
197
202
}
@@ -201,27 +206,27 @@ public function cleanBySurrogateKey($keys)
201
206
$ canPublishPurgeChanges = $ this ->config ->canPublishPurgeChanges ();
202
207
203
208
if ($ this ->config ->areWebHooksEnabled () && ($ canPublishKeyUrlChanges || $ canPublishPurgeChanges )) {
204
- $ status = $ result ? '' : 'FAILED ' ;
209
+ $ status = $ result[ ' status ' ] ? '' : 'FAILED ' ;
205
210
$ this ->sendWebHook ($ status . '*clean by key on ' . join (" " , $ keys ) . '* ' );
206
211
207
212
$ canPublishPurgeByKeyDebugBacktrace = $ this ->config ->canPublishPurgeByKeyDebugBacktrace ();
208
213
$ canPublishPurgeDebugBacktrace = $ this ->config ->canPublishPurgeDebugBacktrace ();
209
214
210
215
if ($ canPublishPurgeByKeyDebugBacktrace == false && $ canPublishPurgeDebugBacktrace == false ) {
211
- return $ result ;
216
+ return $ result[ ' status ' ] ;
212
217
}
213
218
214
219
$ this ->stackTrace ($ type . join (" " , $ keys ));
215
220
}
216
221
}
217
222
218
- return $ result ;
223
+ return $ result[ ' status ' ] ;
219
224
}
220
225
221
226
/**
222
227
* Purge all of Fastly's CDN content. Can be called only once per request
223
228
*
224
- * @return bool
229
+ * @return bool|\Magento\Framework\Controller\Result\Json
225
230
* @throws \Zend_Uri_Exception
226
231
*/
227
232
public function cleanAll ()
@@ -234,7 +239,8 @@ public function cleanAll()
234
239
235
240
$ type = 'clean/purge all ' ;
236
241
$ uri = $ this ->_getApiServiceUri () . 'purge_all ' ;
237
- if ($ result = $ this ->_purge ($ uri , null )) {
242
+ $ result = $ this ->_purge ($ uri , null );
243
+ if ($ result ['status ' ]) {
238
244
$ this ->logger ->execute ('clean all items ' );
239
245
}
240
246
@@ -248,13 +254,13 @@ public function cleanAll()
248
254
$ canPublishPurgeDebugBacktrace = $ this ->config ->canPublishPurgeDebugBacktrace ();
249
255
250
256
if ($ canPublishPurgeAllDebugBacktrace == false && $ canPublishPurgeDebugBacktrace == false ) {
251
- return $ result ;
257
+ return $ result[ ' status ' ] ;
252
258
}
253
259
254
260
$ this ->stackTrace ($ type );
255
261
}
256
262
257
- return $ result ;
263
+ return $ result[ ' status ' ] ;
258
264
}
259
265
260
266
/**
@@ -264,7 +270,7 @@ public function cleanAll()
264
270
* @param $type
265
271
* @param string $method
266
272
* @param null $payload
267
- * @return bool
273
+ * @return \Magento\Framework\Controller\Result\Json
268
274
* @throws \Zend_Uri_Exception
269
275
*/
270
276
private function _purge ($ uri , $ type , $ method = \Zend_Http_Client::POST , $ payload = null )
@@ -296,8 +302,7 @@ private function _purge($uri, $type, $method = \Zend_Http_Client::POST, $payload
296
302
self ::FASTLY_HEADER_SOFT_PURGE . ': 1 '
297
303
);
298
304
}
299
-
300
- $ result = true ;
305
+ $ result ['status ' ] = true ;
301
306
try {
302
307
$ client = $ this ->curlFactory ->create ();
303
308
$ client ->setConfig (['timeout ' => self ::PURGE_TIMEOUT ]);
@@ -314,11 +319,12 @@ private function _purge($uri, $type, $method = \Zend_Http_Client::POST, $payload
314
319
if ($ responseCode == '429 ' ) {
315
320
throw new LocalizedException (__ ($ responseMessage ));
316
321
} elseif ($ responseCode != '200 ' ) {
317
- throw new LocalizedException (__ (' Return status ' . $ responseCode ));
322
+ throw new LocalizedException (__ ($ responseCode . ' : ' . $ responseMessage ));
318
323
}
319
324
} catch (\Exception $ e ) {
320
325
$ this ->logger ->critical ($ e ->getMessage (), $ uri );
321
- $ result = false ;
326
+ $ result ['status ' ] = false ;
327
+ $ result ['msg ' ] = $ e ->getMessage ();
322
328
}
323
329
324
330
if (empty ($ type )) {
0 commit comments