1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File: VarnishUrlPurger.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\Model\QueueHandler; |
10
|
|
|
|
11
|
|
|
use LizardMedia\VarnishWarmer\Api\Config\GeneralConfigProviderInterface; |
12
|
|
|
use LizardMedia\VarnishWarmer\Api\Config\PurgingConfigProviderInterface; |
13
|
|
|
use LizardMedia\VarnishWarmer\Api\ProgressHandler\QueueProgressLoggerInterface; |
14
|
|
|
use LizardMedia\VarnishWarmer\Api\QueueHandler\VarnishUrlPurgerInterface; |
15
|
|
|
use cURL\Event; |
16
|
|
|
use cURL\Request; |
17
|
|
|
use cURL\RequestsQueue; |
18
|
|
|
use Magento\Framework\App\Filesystem\DirectoryList; |
19
|
|
|
use Psr\Log\LoggerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class VarnishUrlPurger |
23
|
|
|
* @package LizardMedia\VarnishWarmer\Model\QueueHandler |
24
|
|
|
*/ |
25
|
|
|
class VarnishUrlPurger extends AbstractQueueHandler implements VarnishUrlPurgerInterface |
26
|
|
|
{ |
27
|
|
|
const CURL_CUSTOMREQUEST = 'PURGE'; |
28
|
|
|
const PROCESS_TYPE = 'PURGE'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $verifyPeer = true; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var PurgingConfigProviderInterface |
37
|
|
|
*/ |
38
|
|
|
private $purgingConfigProvider; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* VarnishUrlPurger constructor. |
42
|
|
|
* @param GeneralConfigProviderInterface $configProvider |
43
|
|
|
* @param LoggerInterface $logger |
44
|
|
|
* @param QueueProgressLoggerInterface $queueProgressLogger |
45
|
|
|
* @param PurgingConfigProviderInterface $purgingConfigProvider |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
GeneralConfigProviderInterface $configProvider, |
49
|
|
|
LoggerInterface $logger, |
50
|
|
|
QueueProgressLoggerInterface $queueProgressLogger, |
51
|
|
|
PurgingConfigProviderInterface $purgingConfigProvider |
52
|
|
|
) { |
53
|
|
|
parent::__construct($configProvider, $logger, $queueProgressLogger); |
54
|
|
|
$this->purgingConfigProvider = $purgingConfigProvider; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function isVerifyPeer(): bool |
61
|
|
|
{ |
62
|
|
|
return $this->verifyPeer; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param bool $verifyPeer |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function setVerifyPeer(bool $verifyPeer): void |
70
|
|
|
{ |
71
|
|
|
$this->verifyPeer = $verifyPeer; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $url |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function addUrlToPurge(string $url): void |
79
|
|
|
{ |
80
|
|
|
$this->requests[] = new Request($url); |
81
|
|
|
$this->total++; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function runPurgeQueue(): void |
88
|
|
|
{ |
89
|
|
|
$this->buildQueue(); |
90
|
|
|
$this->runQueueProcesses(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
protected function buildQueue(): void |
97
|
|
|
{ |
98
|
|
|
$this->queue |
99
|
|
|
->getDefaultOptions() |
100
|
|
|
->set(CURLOPT_TIMEOUT, self::CURL_TIMEOUT) |
101
|
|
|
->set(CURLOPT_CUSTOMREQUEST, self::CURL_CUSTOMREQUEST) |
102
|
|
|
->set(CURLOPT_VERBOSE, false) |
103
|
|
|
->set(CURLOPT_SSL_VERIFYPEER, $this->isVerifyPeer()) |
104
|
|
|
->set(CURLOPT_HTTPHEADER, $this->buildHeaders()) |
105
|
|
|
->set(CURLOPT_RETURNTRANSFER, true); |
106
|
|
|
|
107
|
|
|
$requests = &$this->requests; |
108
|
|
View Code Duplication |
$this->queue->addListener('complete', function (Event $event) use (&$requests) { |
|
|
|
|
109
|
|
|
$this->counter++; |
110
|
|
|
$url = curl_getinfo($event->request->getHandle(), CURLINFO_EFFECTIVE_URL); |
111
|
|
|
$this->log($url); |
112
|
|
|
$this->logProgress(); |
113
|
|
|
if ($next = array_pop($requests)) { |
114
|
|
|
$event->queue->attach($next); |
115
|
|
|
} |
116
|
|
|
}); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
protected function runQueueProcesses(): void |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$numberOfProcesses = $this->getNumberOfParallelProcesses(); |
125
|
|
|
for ($i = 0; $i < $numberOfProcesses; $i++) { |
126
|
|
|
$this->queue->attach(array_pop($this->requests)); |
127
|
|
|
} |
128
|
|
|
if ($this->queue->count() > 0) { |
129
|
|
|
$this->queue->send(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
|
|
protected function getMaxNumberOfProcesses(): int |
137
|
|
|
{ |
138
|
|
|
return $this->configProvider->getMaxConcurrentPurgeProcesses(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
protected function getQueueProcessType(): string |
145
|
|
|
{ |
146
|
|
|
return self::PROCESS_TYPE; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
private function buildHeaders(): array |
153
|
|
|
{ |
154
|
|
|
$headers = []; |
155
|
|
|
$headers[] = 'X-Magento-Tags-Pattern: .*'; |
156
|
|
|
if ($this->purgingConfigProvider->isPurgeCustomHostEnabled() |
157
|
|
|
&& $this->purgingConfigProvider->getAdditionalHostForHeader()) { |
158
|
|
|
$headers[] = "Host: {$this->purgingConfigProvider->getAdditionalHostForHeader()}"; |
159
|
|
|
} |
160
|
|
|
return $headers; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
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.