|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeubiQA\Application\Selenium; |
|
4
|
|
|
|
|
5
|
|
|
use BeubiQA\Application\Selenium\Options\SeleniumDownloaderOptions; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
|
8
|
|
|
|
|
9
|
|
|
class SeleniumDownloader |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var Client */ |
|
12
|
|
|
protected $httpClient; |
|
13
|
|
|
/** @var ProgressBar */ |
|
14
|
|
|
protected $progressBar; |
|
15
|
|
|
/** @var SeleniumDownloaderOptions */ |
|
16
|
|
|
protected $seleniumOptions; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(SeleniumDownloaderOptions $seleniumOptions, Client $httpClient) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->seleniumOptions = $seleniumOptions; |
|
21
|
|
|
$this->httpClient = $httpClient; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function setProgressBar(ProgressBar $progressBar) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->progressBar = $progressBar; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function isJarAlreadyDownloaded() |
|
30
|
|
|
{ |
|
31
|
|
|
return is_file($this->getJarPath()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getJarPath() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->seleniumOptions->getSeleniumDestination().'/selenium-server-standalone.jar'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function download() |
|
40
|
|
|
{ |
|
41
|
|
|
$destinationPath = $this->seleniumOptions->getSeleniumDestination(); |
|
42
|
|
|
$downloadUrl = $this->seleniumOptions->getSeleniumDownloadUrl(); |
|
43
|
|
|
if (!$destinationPath || !$downloadUrl) { |
|
44
|
|
|
throw new \LogicException('Destination and Download Url are mandatory.'); |
|
45
|
|
|
} |
|
46
|
|
|
if ($this->isJarAlreadyDownloaded()) { |
|
47
|
|
|
throw new \RuntimeException('File already exists. '.$this->getJarPath()); |
|
48
|
|
|
} |
|
49
|
|
|
if (!is_dir($destinationPath)) { |
|
50
|
|
|
mkdir($destinationPath, 0777, true); |
|
51
|
|
|
} |
|
52
|
|
|
if (!is_writable($destinationPath)) { |
|
53
|
|
|
throw new \RuntimeException( |
|
54
|
|
|
'Not enough permissions in '.$destinationPath.". \nTry with sudo." |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
$outputFile = $this->getJarPath(); |
|
58
|
|
|
$this->downloadFile($downloadUrl, $outputFile); |
|
59
|
|
|
if (!file_exists($outputFile)) { |
|
60
|
|
|
throw new \RuntimeException('Something wrong happent. The selenium file does not exists. '.$outputFile); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $url |
|
66
|
|
|
* @param string $saveTo |
|
67
|
|
|
*/ |
|
68
|
|
|
private function downloadFile($url, $saveTo) |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->progressBar) { |
|
71
|
|
|
$this->setDownloadWithProgressBar(); |
|
72
|
|
|
} |
|
73
|
|
|
$this->httpClient->get($url, ['save_to' => $saveTo]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
private function setDownloadWithProgressBar() |
|
77
|
|
|
{ |
|
78
|
|
|
$emitter = $this->httpClient->getEmitter(); |
|
79
|
|
|
$emitter->on('before', function (\GuzzleHttp\Event\BeforeEvent $event) { |
|
80
|
|
|
echo $event->getRequest(); |
|
81
|
|
|
}); |
|
82
|
|
|
$emitter->once('progress', function (\GuzzleHttp\Event\ProgressEvent $event) { |
|
83
|
|
|
$this->progressBar->start($event->downloadSize); |
|
84
|
|
|
}); |
|
85
|
|
|
$emitter->on('progress', function (\GuzzleHttp\Event\ProgressEvent $event) { |
|
86
|
|
|
$this->progressBar->setProgress($event->downloaded); |
|
87
|
|
|
}); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return SeleniumDownloaderOptions |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getDownloaderOptions() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->seleniumOptions; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|