|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Collector\Website; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
7
|
|
|
use GuzzleHttp\Exception\ServerException; |
|
8
|
|
|
use Startwind\Inventorio\Collector\BasicCollector; |
|
9
|
|
|
use Startwind\Inventorio\Collector\ClientAwareCollector; |
|
10
|
|
|
use Startwind\Inventorio\Collector\InventoryAwareCollector; |
|
11
|
|
|
use Startwind\Inventorio\Util\Client; |
|
12
|
|
|
use Startwind\Inventorio\Util\WebsiteUtil; |
|
13
|
|
|
|
|
14
|
|
|
class ResponseCollector extends BasicCollector implements InventoryAwareCollector, ClientAwareCollector |
|
15
|
|
|
{ |
|
16
|
|
|
protected string $identifier = "WebsiteResponse"; |
|
17
|
|
|
|
|
18
|
|
|
private array $inventory; |
|
19
|
|
|
private Client $client; |
|
20
|
|
|
|
|
21
|
|
|
public function setClient(Client $client): void |
|
22
|
|
|
{ |
|
23
|
|
|
$this->client = $client; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function setInventory(array $inventory): void |
|
27
|
|
|
{ |
|
28
|
|
|
$this->inventory = $inventory; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function collect(): array |
|
32
|
|
|
{ |
|
33
|
|
|
$domains = WebsiteUtil::extractDomains($this->inventory); |
|
34
|
|
|
|
|
35
|
|
|
$uptimeStatus = []; |
|
36
|
|
|
|
|
37
|
|
|
foreach ($domains as $domain) { |
|
38
|
|
|
try { |
|
39
|
|
|
$result = $this->client->get('https://' . $domain); |
|
40
|
|
|
} catch (ClientException $e) { |
|
41
|
|
|
$uptimeStatus[$domain] = [ |
|
42
|
|
|
'code' => $e->getResponse()->getStatusCode(), |
|
43
|
|
|
'protocol_version' => $e->getResponse()->getProtocolVersion(), |
|
44
|
|
|
]; |
|
45
|
|
|
continue; |
|
46
|
|
|
} catch (ServerException $e) { |
|
47
|
|
|
$uptimeStatus[$domain] = [ |
|
48
|
|
|
'code' => $e->getResponse()->getStatusCode(), |
|
49
|
|
|
'protocol_version' => $e->getResponse()->getProtocolVersion() |
|
50
|
|
|
]; |
|
51
|
|
|
continue; |
|
52
|
|
|
} catch (Exception $e) { |
|
53
|
|
|
$uptimeStatus[$domain] = [ |
|
54
|
|
|
'code' => 599, |
|
55
|
|
|
'message' => $e->getMessage(), |
|
56
|
|
|
'protocol_version' => false |
|
57
|
|
|
]; |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$uptimeStatus[$domain] = [ |
|
62
|
|
|
'code' => $result->getStatusCode(), |
|
63
|
|
|
'protocol_version' => $result->getProtocolVersion() |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $uptimeStatus; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|