Passed
Push — master ( 82058b...990cd6 )
by Nils
02:38
created

ResponseCollector::setClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
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\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