Passed
Push — master ( 0fe5f5...c76c09 )
by Nils
02:59
created

UptimeCollector::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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Startwind\Inventorio\Collector\Website;
4
5
use GuzzleHttp\Exception\ClientException;
6
use GuzzleHttp\Exception\ServerException;
7
use Startwind\Inventorio\Collector\BasicCollector;
8
use Startwind\Inventorio\Collector\ClientAwareCollector;
9
use Startwind\Inventorio\Collector\InventoryAwareCollector;
10
use Startwind\Inventorio\Util\Client;
11
use Startwind\Inventorio\Util\WebsiteUtil;
12
13
class UptimeCollector extends BasicCollector implements InventoryAwareCollector, ClientAwareCollector
14
{
15
    protected string $identifier = "WebsiteUptime";
16
17
    private array $inventory;
18
    private Client $client;
19
20
    public function setClient(Client $client): void
21
    {
22
        $this->client = $client;
23
    }
24
25
    public function setInventory(array $inventory): void
26
    {
27
        $this->inventory = $inventory;
28
    }
29
30
    public function collect(): array
31
    {
32
        $domains = WebsiteUtil::extractDomains($this->inventory);
33
34
        $uptimeStatus = [];
35
36
        foreach ($domains as $domain) {
37
            try {
38
                $result = $this->client->get('https://' . $domain);
39
            } catch (ClientException $e) {
40
                $uptimeStatus[$domain] = ['code' => $e->getResponse()->getStatusCode()];
41
                continue;
42
            } catch (ServerException $e) {
43
                $uptimeStatus[$domain] = ['code' => $e->getResponse()->getStatusCode()];
44
                continue;
45
            } catch (\Exception $e) {
46
                $uptimeStatus[$domain] = ['code' => 599, 'message' => $e->getMessage()];
47
                continue;
48
            }
49
50
            $uptimeStatus[$domain] = ['code' => $result->getStatusCode()];
51
        }
52
53
        return $uptimeStatus;
54
    }
55
}
56