Passed
Push — master ( 203b49...a3b100 )
by Nils
02:52 queued 12s
created

ResponseCollector::collect()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 27
nc 5
nop 0
dl 0
loc 39
rs 9.1768
c 1
b 0
f 0
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\Exec\Runner;
12
use Startwind\Inventorio\Util\Client;
13
use Startwind\Inventorio\Util\WebsiteUtil;
14
15
class ResponseCollector extends BasicCollector implements InventoryAwareCollector, ClientAwareCollector
16
{
17
    protected string $identifier = "WebsiteResponse";
18
19
    private array $inventory;
20
    private Client $client;
21
22
    public function setClient(Client $client): void
23
    {
24
        $this->client = $client;
25
    }
26
27
    public function setInventory(array $inventory): void
28
    {
29
        $this->inventory = $inventory;
30
    }
31
32
    public function collect(): array
33
    {
34
        $domains = WebsiteUtil::extractDomains($this->inventory);
35
36
        $uptimeStatus = [];
37
38
        foreach ($domains as $domain) {
39
            try {
40
                $url = 'https://' . $domain;
41
                $result = $this->client->get($url);
42
            } catch (ClientException $e) {
43
                $uptimeStatus[$domain] = [
44
                    'code' => $e->getResponse()->getStatusCode(),
45
                    'h2' => $this->supportsHttp2($domain)
46
                ];
47
                continue;
48
            } catch (ServerException $e) {
49
                $uptimeStatus[$domain] = [
50
                    'code' => $e->getResponse()->getStatusCode(),
51
                    'h2' => $this->supportsHttp2($domain)
52
                ];
53
                continue;
54
            } catch (Exception $e) {
55
                $uptimeStatus[$domain] = [
56
                    'code' => 599,
57
                    'message' => $e->getMessage(),
58
                    'h2' => false
59
                ];
60
                continue;
61
            }
62
63
            $uptimeStatus[$domain] = [
64
                'code' => $result->getStatusCode(),
65
                'protocol_version' => $result->getProtocolVersion(),
66
                'h2' => $this->supportsHttp2($domain)
67
            ];
68
        }
69
70
        return $uptimeStatus;
71
    }
72
73
    private function supportsHttp2(string $host): bool
74
    {
75
        $output = Runner::getInstance()->run("echo | openssl s_client -alpn h2 -connect {$host}:443 2>/dev/null")->getOutput();
76
        return str_contains($output, 'ALPN protocol: h2');
77
    }
78
}
79