1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File: CacheCleaner.php |
4
|
|
|
* |
5
|
|
|
* @author Maciej Sławik <[email protected]> |
6
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace LizardMedia\VarnishWarmer\Helper; |
10
|
|
|
|
11
|
|
|
use LizardMedia\VarnishWarmer\Api\Config\PurgingConfigProviderInterface; |
12
|
|
|
use LizardMedia\VarnishWarmer\Api\LockHandler\LockInterface; |
13
|
|
|
use LizardMedia\VarnishWarmer\Api\QueueHandler\VarnishUrlPurgerInterface; |
14
|
|
|
use LizardMedia\VarnishWarmer\Api\QueueHandler\VarnishUrlRegeneratorInterface; |
15
|
|
|
use LizardMedia\VarnishWarmer\Api\UrlProvider\CategoryUrlProviderInterface; |
16
|
|
|
use LizardMedia\VarnishWarmer\Api\UrlProvider\ProductUrlProviderInterface; |
17
|
|
|
use LizardMedia\VarnishWarmer\Model\QueueHandler\VarnishUrlRegeneratorFactory; |
18
|
|
|
use LizardMedia\VarnishWarmer\Model\QueueHandler\VarnishUrlPurgerFactory; |
19
|
|
|
use Magento\Framework\App\Config\ScopeConfigInterface; |
20
|
|
|
use Magento\Store\Model\ScopeInterface; |
21
|
|
|
use Magento\Store\Model\Store; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class CacheCleaner |
25
|
|
|
* @package LizardMedia\VarnishWarmer\Helper |
26
|
|
|
*/ |
27
|
|
|
class CacheCleaner |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var VarnishUrlRegeneratorInterface |
31
|
|
|
*/ |
32
|
|
|
protected $varnishUrlRegenerator; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var VarnishUrlPurgerInterface |
36
|
|
|
*/ |
37
|
|
|
protected $varnishUrlPurger; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var LockInterface |
41
|
|
|
*/ |
42
|
|
|
protected $lockHandler; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ScopeConfigInterface |
46
|
|
|
*/ |
47
|
|
|
protected $scopeConfig; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var ProductUrlProviderInterface |
51
|
|
|
*/ |
52
|
|
|
protected $productUrlProvider; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var CategoryUrlProviderInterface |
56
|
|
|
*/ |
57
|
|
|
protected $categoryUrlProvider; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var PurgingConfigProviderInterface |
61
|
|
|
*/ |
62
|
|
|
protected $purgingConfigProvider; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $purgeBaseUrl; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected $regenBaseUrl; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var int |
76
|
|
|
*/ |
77
|
|
|
protected $storeViewId; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var bool |
81
|
|
|
*/ |
82
|
|
|
public $verifyPeer = true; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* CacheCleaner constructor. |
86
|
|
|
* @param VarnishUrlRegeneratorFactory $varnishUrlRegeneratorFactory |
87
|
|
|
* @param VarnishUrlPurgerFactory $varnishUrlPurgerFactory |
88
|
|
|
* @param LockInterface $lockHandler |
89
|
|
|
* @param ScopeConfigInterface $scopeConfig |
90
|
|
|
* @param ProductUrlProviderInterface $productUrlProvider |
91
|
|
|
* @param CategoryUrlProviderInterface $categoryUrlProvider |
92
|
|
|
*/ |
93
|
|
|
public function __construct( |
94
|
|
|
VarnishUrlRegeneratorFactory $varnishUrlRegeneratorFactory, |
95
|
|
|
VarnishUrlPurgerFactory $varnishUrlPurgerFactory, |
96
|
|
|
LockInterface $lockHandler, |
97
|
|
|
ScopeConfigInterface $scopeConfig, |
98
|
|
|
ProductUrlProviderInterface $productUrlProvider, |
99
|
|
|
CategoryUrlProviderInterface $categoryUrlProvider, |
100
|
|
|
PurgingConfigProviderInterface $purgingConfigProvider |
101
|
|
|
) { |
102
|
|
|
$this->lockHandler = $lockHandler; |
103
|
|
|
$this->scopeConfig = $scopeConfig; |
104
|
|
|
$this->productUrlProvider = $productUrlProvider; |
105
|
|
|
$this->categoryUrlProvider = $categoryUrlProvider; |
106
|
|
|
$this->purgingConfigProvider = $purgingConfigProvider; |
107
|
|
|
|
108
|
|
|
/** @var VarnishUrlRegeneratorInterface varnishUrlRegenerator */ |
109
|
|
|
$this->varnishUrlRegenerator = $varnishUrlRegeneratorFactory->create(); |
110
|
|
|
/** @var VarnishUrlPurgerInterface varnishUrlPurger */ |
111
|
|
|
$this->varnishUrlPurger = $varnishUrlPurgerFactory->create(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param int $storeViewId |
116
|
|
|
*/ |
117
|
|
|
public function setStoreViewId(int $storeViewId) |
118
|
|
|
{ |
119
|
|
|
$this->storeViewId = $storeViewId; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Purge * |
124
|
|
|
* Regen homepage, categories, products |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function purgeWildcard(): void |
128
|
|
|
{ |
129
|
|
|
$this->lock(); |
130
|
|
|
$this->addUrlToPurge('*'); |
131
|
|
|
$this->addUrlToRegenerate(''); |
132
|
|
|
$this->regenerateCategories(); |
133
|
|
|
$this->processProductsRegenerate(); |
134
|
|
|
$this->varnishUrlPurger->setVerifyPeer($this->verifyPeer); |
135
|
|
|
$this->varnishUrlPurger->runPurgeQueue(); |
136
|
|
|
$this->varnishUrlRegenerator->runRegenerationQueue(); |
137
|
|
|
$this->unlock(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Purge * without any regeneration |
142
|
|
|
* Pass through lock |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
public function purgeWildcardWithoutRegen(): void |
146
|
|
|
{ |
147
|
|
|
$this->addUrlToPurge('*'); |
148
|
|
|
$this->varnishUrlPurger->setVerifyPeer($this->verifyPeer); |
149
|
|
|
$this->varnishUrlPurger->runPurgeQueue(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Purge homepage, categories, products |
154
|
|
|
* Regen homepage, categories, products |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function purgeAll(): void |
158
|
|
|
{ |
159
|
|
|
$this->lock(); |
160
|
|
|
$this->addUrlToPurge(''); |
161
|
|
|
$this->addUrlToRegenerate(''); |
162
|
|
|
$this->processCategoriesPurgeAndRegenerate(); |
163
|
|
|
$this->processProductsPurgeAndRegenerate(); |
164
|
|
|
$this->varnishUrlPurger->setVerifyPeer($this->verifyPeer); |
165
|
|
|
$this->varnishUrlPurger->runPurgeQueue(); |
166
|
|
|
$this->varnishUrlRegenerator->runRegenerationQueue(); |
167
|
|
|
$this->unlock(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Purge homepage, categories |
172
|
|
|
* Regen homepage, categories |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
public function purgeGeneral(): void |
176
|
|
|
{ |
177
|
|
|
$this->lock(); |
178
|
|
|
$this->addUrlToPurge(''); |
179
|
|
|
$this->addUrlToRegenerate(''); |
180
|
|
|
$this->processCategoriesPurgeAndRegenerate(); |
181
|
|
|
$this->varnishUrlPurger->setVerifyPeer($this->verifyPeer); |
182
|
|
|
$this->varnishUrlPurger->runPurgeQueue(); |
183
|
|
|
$this->varnishUrlRegenerator->runRegenerationQueue(); |
184
|
|
|
$this->unlock(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Purge homepage |
189
|
|
|
* Regen homepage |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function purgeHomepage(): void |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$this->lock(); |
195
|
|
|
$this->addUrlToPurge(''); |
196
|
|
|
$this->addUrlToRegenerate(''); |
197
|
|
|
$this->varnishUrlPurger->setVerifyPeer($this->verifyPeer); |
198
|
|
|
$this->varnishUrlPurger->runPurgeQueue(); |
199
|
|
|
$this->varnishUrlRegenerator->runRegenerationQueue(); |
200
|
|
|
$this->unlock(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $url |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
|
|
public function purgeAndRegenerateUrl(string $url): void |
208
|
|
|
{ |
209
|
|
|
$this->addUrlToPurge($url); |
210
|
|
|
$this->addUrlToRegenerate($url); |
211
|
|
|
$this->varnishUrlPurger->setVerifyPeer($this->verifyPeer); |
212
|
|
|
$this->varnishUrlPurger->runPurgeQueue(); |
213
|
|
|
$this->varnishUrlRegenerator->runRegenerationQueue(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param $product |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
public function purgeProduct($product): void |
221
|
|
|
{ |
222
|
|
|
$productUrls = $this->getProductUrls($product->getEntityId()); |
223
|
|
|
foreach ($productUrls as $url) { |
224
|
|
|
$this->addUrlToPurge($url['request_path'], true); |
225
|
|
|
} |
226
|
|
|
$this->varnishUrlPurger->runPurgeQueue(); |
227
|
|
|
$this->varnishUrlRegenerator->runRegenerationQueue(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return void |
232
|
|
|
*/ |
233
|
|
View Code Duplication |
public function purgeAndRegenerateProducts(): void |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
$this->lock(); |
236
|
|
|
$this->processProductsPurgeAndRegenerate(); |
237
|
|
|
$this->varnishUrlPurger->setVerifyPeer($this->verifyPeer); |
238
|
|
|
$this->varnishUrlPurger->runPurgeQueue(); |
239
|
|
|
$this->varnishUrlRegenerator->runRegenerationQueue(); |
240
|
|
|
$this->unlock(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return bool |
245
|
|
|
*/ |
246
|
|
|
public function isLocked(): bool |
247
|
|
|
{ |
248
|
|
|
return $this->lockHandler->isLocked(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return string |
253
|
|
|
*/ |
254
|
|
|
public function getLockMessage(): string |
255
|
|
|
{ |
256
|
|
|
return $this->lockHandler->getLockDate(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param $relativeUrl |
261
|
|
|
* @param bool $autoRegenerate |
262
|
|
|
* @return void |
263
|
|
|
*/ |
264
|
|
|
private function addUrlToPurge($relativeUrl, $autoRegenerate = false): void |
265
|
|
|
{ |
266
|
|
|
$url = $this->getPurgeBaseUrl() . $relativeUrl; |
267
|
|
|
$this->varnishUrlPurger->addUrlToPurge($url); |
268
|
|
|
if ($autoRegenerate) { |
269
|
|
|
$this->addUrlToRegenerate($relativeUrl); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param $relativeUrl |
275
|
|
|
* @return void |
276
|
|
|
*/ |
277
|
|
|
private function addUrlToRegenerate($relativeUrl): void |
278
|
|
|
{ |
279
|
|
|
$url = $this->getRegenBaseUrl() . $relativeUrl; |
280
|
|
|
$this->varnishUrlRegenerator->addUrlToRegenerate($url); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return void |
285
|
|
|
*/ |
286
|
|
|
private function regenerateCategories(): void |
287
|
|
|
{ |
288
|
|
|
$categories = $this->getCategories(); |
289
|
|
|
foreach ($categories as $category) { |
290
|
|
|
$this->addUrlToRegenerate($category['request_path']); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @return void |
296
|
|
|
*/ |
297
|
|
|
private function processCategoriesPurgeAndRegenerate(): void |
298
|
|
|
{ |
299
|
|
|
$categories = $this->getCategories(); |
300
|
|
|
foreach ($categories as $category) { |
301
|
|
|
$this->addUrlToPurge($category['request_path'], true); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @return void |
307
|
|
|
*/ |
308
|
|
|
private function processProductsRegenerate(): void |
309
|
|
|
{ |
310
|
|
|
$productUrls = $this->getAllProductsUrls(); |
311
|
|
|
foreach ($productUrls as $key => $url) { |
312
|
|
|
$this->addUrlToRegenerate($url['request_path']); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @return void |
318
|
|
|
*/ |
319
|
|
|
private function processProductsPurgeAndRegenerate(): void |
320
|
|
|
{ |
321
|
|
|
$productUrls = $this->getAllProductsUrls(); |
322
|
|
|
foreach ($productUrls as $key => $url) { |
323
|
|
|
$this->addUrlToPurge($url['request_path'], true); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @return array |
329
|
|
|
*/ |
330
|
|
|
private function getAllProductsUrls(): array |
331
|
|
|
{ |
332
|
|
|
return $this->productUrlProvider->getActiveProductsUrls(); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param $productId |
337
|
|
|
* @return array |
338
|
|
|
*/ |
339
|
|
|
private function getProductUrls($productId): array |
340
|
|
|
{ |
341
|
|
|
return $this->productUrlProvider->getProductUrls($productId); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @return array |
346
|
|
|
*/ |
347
|
|
|
private function getCategories(): array |
348
|
|
|
{ |
349
|
|
|
return $this->categoryUrlProvider->getActiveCategoriesUrls(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @return void |
354
|
|
|
*/ |
355
|
|
|
private function lock(): void |
356
|
|
|
{ |
357
|
|
|
$this->lockHandler->lock(); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @return void |
362
|
|
|
*/ |
363
|
|
|
private function unlock(): void |
364
|
|
|
{ |
365
|
|
|
$this->lockHandler->unlock(); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @return void |
370
|
|
|
*/ |
371
|
|
|
private function setPurgeBaseUrl(): void |
372
|
|
|
{ |
373
|
|
|
if ($this->purgingConfigProvider->isPurgeCustomHostEnabled()) { |
374
|
|
|
$this->purgeBaseUrl = $this->purgingConfigProvider->getCustomPurgeHost(); |
375
|
|
|
} else { |
376
|
|
|
$this->purgeBaseUrl = $this->scopeConfig->getValue( |
377
|
|
|
Store::XML_PATH_UNSECURE_BASE_URL, |
378
|
|
|
ScopeInterface::SCOPE_STORE, |
379
|
|
|
$this->storeViewId |
380
|
|
|
); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
if (substr($this->purgeBaseUrl, -1) != "/") { |
384
|
|
|
$this->purgeBaseUrl .= "/"; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @return void |
390
|
|
|
*/ |
391
|
|
|
private function setRegenBaseUrl(): void |
392
|
|
|
{ |
393
|
|
|
$this->regenBaseUrl = $this->scopeConfig->getValue( |
394
|
|
|
Store::XML_PATH_UNSECURE_BASE_URL, |
395
|
|
|
ScopeInterface::SCOPE_STORE, |
396
|
|
|
$this->storeViewId |
397
|
|
|
); |
398
|
|
|
|
399
|
|
|
if (substr($this->regenBaseUrl, -1) != "/") { |
400
|
|
|
$this->regenBaseUrl .= "/"; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @return string |
406
|
|
|
*/ |
407
|
|
|
private function getPurgeBaseUrl() |
408
|
|
|
{ |
409
|
|
|
if (!$this->purgeBaseUrl) { |
410
|
|
|
$this->setPurgeBaseUrl(); |
411
|
|
|
} |
412
|
|
|
return $this->purgeBaseUrl; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @return string |
417
|
|
|
*/ |
418
|
|
|
private function getRegenBaseUrl() |
419
|
|
|
{ |
420
|
|
|
if (!$this->regenBaseUrl) { |
421
|
|
|
$this->setRegenBaseUrl(); |
422
|
|
|
} |
423
|
|
|
return $this->regenBaseUrl; |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.