|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeubiQA\Application\Lib; |
|
4
|
|
|
|
|
5
|
|
|
use BeubiQA\Application\Exception\Timeout; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use GuzzleHttp\Exception\ConnectException; |
|
8
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
|
9
|
|
|
|
|
10
|
|
|
class ResponseWaitter |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var Client */ |
|
13
|
|
|
private $httpClient; |
|
14
|
|
|
/** @var ProgressBar */ |
|
15
|
|
|
private $progressBar; |
|
16
|
|
|
private $timeout = 30000000; |
|
17
|
|
|
private $waitInterval = 25000; // 0.025 seconds |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Client $httpClient, $timeoutSeconds = 30000000, $waitInterval = 25000) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->setTimeout($timeoutSeconds); |
|
22
|
|
|
$this->waitInterval = $waitInterval; |
|
23
|
|
|
$this->httpClient = $httpClient; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getTimeout() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->timeout; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function setTimeout($timeoutSeconds) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->timeout = (int) $timeoutSeconds * 1000000; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function setProgressBar(ProgressBar $progressBar) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->progressBar = $progressBar; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function progressBarStart() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->progressBar ? $this->progressBar->start($this->timeout) : ''; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function progressBarSetProgress() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->progressBar ? $this->progressBar->setProgress($this->timeout - $this->timeLeft) : ''; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function progressBarFinish() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->progressBar ? $this->progressBar->finish() : ''; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function updateTimeleft() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->timeLeft -= $this->waitInterval; |
|
59
|
|
|
if ($this->timeLeft < 0) { |
|
60
|
|
|
throw new Timeout('Timeout of '.var_export($this->timeout, true).' seconds.'); |
|
61
|
|
|
} |
|
62
|
|
|
usleep($this->waitInterval); |
|
63
|
|
|
$this->progressBarSetProgress(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function isAvailable($url, $requestOptions) |
|
67
|
|
|
{ |
|
68
|
|
|
try { |
|
69
|
|
|
$this->httpClient->get($url, $requestOptions); |
|
70
|
|
|
} catch (ConnectException $e) { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
public function waitUntilAvailable($url, $requestOptions) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->progressBarStart(); |
|
80
|
|
|
$this->timeLeft = $this->timeout; |
|
81
|
|
|
while (true) { |
|
82
|
|
|
$this->updateTimeleft(); |
|
83
|
|
|
if ($this->isAvailable($url, $requestOptions)) { |
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
$this->progressBarFinish(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
View Code Duplication |
public function waitUntilNotAvailable($url, $requestOptions) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->progressBarStart(); |
|
93
|
|
|
$this->timeLeft = $this->timeout; |
|
94
|
|
|
while (true) { |
|
95
|
|
|
$this->updateTimeleft(); |
|
96
|
|
|
if (!$this->isAvailable($url, $requestOptions)) { |
|
97
|
|
|
break; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
$this->progressBarFinish(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|