Passed
Push — master ( ae2bb8...f2edfe )
by Maciej
02:47
created

CacheCleaner::setPurgeBaseUrls()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 6
nop 0
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
//TODO remove this entire class and change it to a service contract
24
/**
25
 * Class CacheCleaner
26
 * @package LizardMedia\VarnishWarmer\Helper
27
 */
28
class CacheCleaner
29
{
30
    /**
31
     * @var VarnishUrlRegeneratorInterface
32
     */
33
    protected $varnishUrlRegenerator;
34
35
    /**
36
     * @var VarnishUrlPurgerInterface
37
     */
38
    protected $varnishUrlPurger;
39
40
    /**
41
     * @var LockInterface
42
     */
43
    protected $lockHandler;
44
45
    /**
46
     * @var ScopeConfigInterface
47
     */
48
    protected $scopeConfig;
49
50
    /**
51
     * @var ProductUrlProviderInterface
52
     */
53
    protected $productUrlProvider;
54
55
    /**
56
     * @var CategoryUrlProviderInterface
57
     */
58
    protected $categoryUrlProvider;
59
60
    /**
61
     * @var PurgingConfigProviderInterface
62
     */
63
    protected $purgingConfigProvider;
64
65
    /**
66
     * @var array
67
     */
68
    protected $purgeBaseUrls;
69
70
    /**
71
     * @var string
72
     */
73
    protected $regenBaseUrl;
74
75
    /**
76
     * @var int
77
     */
78
    protected $storeViewId;
79
80
    /**
81
     * @var bool
82
     */
83
    public $verifyPeer = true;
84
85
    /**
86
     * CacheCleaner constructor.
87
     * @param VarnishUrlRegeneratorFactory $varnishUrlRegeneratorFactory
88
     * @param VarnishUrlPurgerFactory $varnishUrlPurgerFactory
89
     * @param LockInterface $lockHandler
90
     * @param ScopeConfigInterface $scopeConfig
91
     * @param ProductUrlProviderInterface $productUrlProvider
92
     * @param CategoryUrlProviderInterface $categoryUrlProvider
93
     */
94
    public function __construct(
95
        VarnishUrlRegeneratorFactory $varnishUrlRegeneratorFactory,
96
        VarnishUrlPurgerFactory $varnishUrlPurgerFactory,
97
        LockInterface $lockHandler,
98
        ScopeConfigInterface $scopeConfig,
99
        ProductUrlProviderInterface $productUrlProvider,
100
        CategoryUrlProviderInterface $categoryUrlProvider,
101
        PurgingConfigProviderInterface $purgingConfigProvider
102
    ) {
103
        $this->lockHandler = $lockHandler;
104
        $this->scopeConfig = $scopeConfig;
105
        $this->productUrlProvider = $productUrlProvider;
106
        $this->categoryUrlProvider = $categoryUrlProvider;
107
        $this->purgingConfigProvider = $purgingConfigProvider;
108
109
        /** @var VarnishUrlRegeneratorInterface varnishUrlRegenerator */
110
        $this->varnishUrlRegenerator = $varnishUrlRegeneratorFactory->create();
111
        /** @var VarnishUrlPurgerInterface varnishUrlPurger */
112
        $this->varnishUrlPurger = $varnishUrlPurgerFactory->create();
113
    }
114
115
    /**
116
     * @param int $storeViewId
117
     */
118
    public function setStoreViewId(int $storeViewId)
119
    {
120
        $this->storeViewId = $storeViewId;
121
    }
122
123
    /**
124
     * Purge *
125
     * Regen homepage, categories, products
126
     * @return void
127
     */
128
    public function purgeWildcard(): void
129
    {
130
        $this->lock();
131
        $this->addUrlToPurge('*');
132
        $this->addUrlToRegenerate('');
133
        $this->regenerateCategories();
134
        $this->processProductsRegenerate();
135
        $this->varnishUrlPurger->setVerifyPeer($this->verifyPeer);
136
        $this->varnishUrlPurger->runPurgeQueue();
137
        $this->varnishUrlRegenerator->runRegenerationQueue();
138
        $this->unlock();
139
    }
140
141
    /**
142
     * Purge * without any regeneration
143
     * Pass through lock
144
     * @return void
145
     */
146
    public function purgeWildcardWithoutRegen(): void
147
    {
148
        $this->addUrlToPurge('*');
149
        $this->varnishUrlPurger->setVerifyPeer($this->verifyPeer);
150
        $this->varnishUrlPurger->runPurgeQueue();
151
    }
152
153
    /**
154
     * Purge homepage, categories, products
155
     * Regen homepage, categories, products
156
     * @return void
157
     */
158
    public function purgeAll(): void
159
    {
160
        $this->lock();
161
        $this->addUrlToPurge('');
162
        $this->addUrlToRegenerate('');
163
        $this->processCategoriesPurgeAndRegenerate();
164
        $this->processProductsPurgeAndRegenerate();
165
        $this->varnishUrlPurger->setVerifyPeer($this->verifyPeer);
166
        $this->varnishUrlPurger->runPurgeQueue();
167
        $this->varnishUrlRegenerator->runRegenerationQueue();
168
        $this->unlock();
169
    }
170
171
    /**
172
     * Purge homepage, categories
173
     * Regen homepage, categories
174
     * @return void
175
     */
176
    public function purgeGeneral(): void
177
    {
178
        $this->lock();
179
        $this->addUrlToPurge('');
180
        $this->addUrlToRegenerate('');
181
        $this->processCategoriesPurgeAndRegenerate();
182
        $this->varnishUrlPurger->setVerifyPeer($this->verifyPeer);
183
        $this->varnishUrlPurger->runPurgeQueue();
184
        $this->varnishUrlRegenerator->runRegenerationQueue();
185
        $this->unlock();
186
    }
187
188
    /**
189
     * Purge homepage
190
     * Regen homepage
191
     * @return void
192
     */
193 View Code Duplication
    public function purgeHomepage(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
194
    {
195
        $this->lock();
196
        $this->addUrlToPurge('');
197
        $this->addUrlToRegenerate('');
198
        $this->varnishUrlPurger->setVerifyPeer($this->verifyPeer);
199
        $this->varnishUrlPurger->runPurgeQueue();
200
        $this->varnishUrlRegenerator->runRegenerationQueue();
201
        $this->unlock();
202
    }
203
204
    /**
205
     * @param string $url
206
     * @return void
207
     */
208
    public function purgeAndRegenerateUrl(string $url): void
209
    {
210
        $this->addUrlToPurge($url);
211
        $this->addUrlToRegenerate($url);
212
        $this->varnishUrlPurger->setVerifyPeer($this->verifyPeer);
213
        $this->varnishUrlPurger->runPurgeQueue();
214
        $this->varnishUrlRegenerator->runRegenerationQueue();
215
    }
216
217
    /**
218
     * @param $product
219
     * @return void
220
     */
221
    public function purgeProduct($product): void
222
    {
223
        $productUrls = $this->getProductUrls($product->getEntityId());
224
        foreach ($productUrls as $url) {
225
            $this->addUrlToPurge($url['request_path'], true);
226
        }
227
        $this->varnishUrlPurger->runPurgeQueue();
228
        $this->varnishUrlRegenerator->runRegenerationQueue();
229
    }
230
231
    /**
232
     * @return void
233
     */
234 View Code Duplication
    public function purgeAndRegenerateProducts(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
235
    {
236
        $this->lock();
237
        $this->processProductsPurgeAndRegenerate();
238
        $this->varnishUrlPurger->setVerifyPeer($this->verifyPeer);
239
        $this->varnishUrlPurger->runPurgeQueue();
240
        $this->varnishUrlRegenerator->runRegenerationQueue();
241
        $this->unlock();
242
    }
243
244
    /**
245
     * @return bool
246
     */
247
    public function isLocked(): bool
248
    {
249
        return $this->lockHandler->isLocked();
250
    }
251
252
    /**
253
     * @return string
254
     */
255
    public function getLockMessage(): string
256
    {
257
        return $this->lockHandler->getLockDate();
258
    }
259
260
    /**
261
     * @param $relativeUrl
262
     * @param bool $autoRegenerate
263
     * @return void
264
     */
265
    private function addUrlToPurge($relativeUrl, $autoRegenerate = false): void
266
    {
267
        foreach ($this->getPurgeBaseUrls() as $purgeBaseUrl) {
268
            $url = $purgeBaseUrl . $relativeUrl;
269
            $this->varnishUrlPurger->addUrlToPurge($url);
270
            if ($autoRegenerate) {
271
                $this->addUrlToRegenerate($relativeUrl);
272
            }
273
        }
274
    }
275
276
    /**
277
     * @param $relativeUrl
278
     * @return void
279
     */
280
    private function addUrlToRegenerate($relativeUrl): void
281
    {
282
        $url = $this->getRegenBaseUrl() . $relativeUrl;
283
        $this->varnishUrlRegenerator->addUrlToRegenerate($url);
284
    }
285
286
    /**
287
     * @return void
288
     */
289
    private function regenerateCategories(): void
290
    {
291
        $categories = $this->getCategories();
292
        foreach ($categories as $category) {
293
            $this->addUrlToRegenerate($category['request_path']);
294
        }
295
    }
296
297
    /**
298
     * @return void
299
     */
300
    private function processCategoriesPurgeAndRegenerate(): void
301
    {
302
        $categories = $this->getCategories();
303
        foreach ($categories as $category) {
304
            $this->addUrlToPurge($category['request_path'], true);
305
        }
306
    }
307
308
    /**
309
     * @return void
310
     */
311
    private function processProductsRegenerate(): void
312
    {
313
        $productUrls = $this->getAllProductsUrls();
314
        foreach ($productUrls as $key => $url) {
315
            $this->addUrlToRegenerate($url['request_path']);
316
        }
317
    }
318
319
    /**
320
     * @return void
321
     */
322
    private function processProductsPurgeAndRegenerate(): void
323
    {
324
        $productUrls = $this->getAllProductsUrls();
325
        foreach ($productUrls as $key => $url) {
326
            $this->addUrlToPurge($url['request_path'], true);
327
        }
328
    }
329
330
    /**
331
     * @return array
332
     */
333
    private function getAllProductsUrls(): array
334
    {
335
        return $this->productUrlProvider->getActiveProductsUrls();
336
    }
337
338
    /**
339
     * @param $productId
340
     * @return array
341
     */
342
    private function getProductUrls($productId): array
343
    {
344
        return $this->productUrlProvider->getProductUrls($productId);
345
    }
346
347
    /**
348
     * @return array
349
     */
350
    private function getCategories(): array
351
    {
352
        return $this->categoryUrlProvider->getActiveCategoriesUrls();
353
    }
354
355
    /**
356
     * @return void
357
     */
358
    private function lock(): void
359
    {
360
        $this->lockHandler->lock();
361
    }
362
363
    /**
364
     * @return void
365
     */
366
    private function unlock(): void
367
    {
368
        $this->lockHandler->unlock();
369
    }
370
371
    /**
372
     * @return void
373
     */
374
    private function setPurgeBaseUrls(): void
375
    {
376
        if ($this->purgingConfigProvider->isPurgeCustomHostEnabled()) {
377
            $this->purgeBaseUrls = $this->purgingConfigProvider->getCustomPurgeHosts();
378
        } else {
379
            $baseUrl = $this->scopeConfig->getValue(
380
                Store::XML_PATH_UNSECURE_BASE_URL,
381
                ScopeInterface::SCOPE_STORE,
382
                $this->storeViewId
383
            );
384
            $this->purgeBaseUrls = [$baseUrl];
385
        }
386
387
        foreach ($this->purgeBaseUrls as &$purgeBaseUrl) {
388
            if (substr($purgeBaseUrl, -1) != "/") {
389
                $purgeBaseUrl .= "/";
390
            }
391
        }
392
    }
393
394
    /**
395
     * @return void
396
     */
397
    private function setRegenBaseUrl(): void
398
    {
399
        $this->regenBaseUrl = $this->scopeConfig->getValue(
400
            Store::XML_PATH_UNSECURE_BASE_URL,
401
            ScopeInterface::SCOPE_STORE,
402
            $this->storeViewId
403
        );
404
405
        if (substr($this->regenBaseUrl, -1) != "/") {
406
            $this->regenBaseUrl .= "/";
407
        }
408
    }
409
410
    /**
411
     * @return array
412
     */
413
    private function getPurgeBaseUrls()
414
    {
415
        if (!$this->purgeBaseUrls) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->purgeBaseUrls of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
416
            $this->setPurgeBaseUrls();
417
        }
418
        return $this->purgeBaseUrls;
419
    }
420
421
    /**
422
     * @return string
423
     */
424
    private function getRegenBaseUrl()
425
    {
426
        if (!$this->regenBaseUrl) {
427
            $this->setRegenBaseUrl();
428
        }
429
        return $this->regenBaseUrl;
430
    }
431
}
432