Passed
Push — master ( f0a91f...a4885e )
by Nils
02:33
created

UptimeCollector::collect()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 28
rs 9.0444
1
<?php
2
3
namespace Startwind\Inventorio\Collector\Website;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ServerException;
7
use Startwind\Inventorio\Collector\Application\WebServer\Apache\ApacheServerNameCollector;
8
use Startwind\Inventorio\Collector\BasicCollector;
9
use Startwind\Inventorio\Collector\InventoryAwareCollector;
10
11
class UptimeCollector extends BasicCollector implements InventoryAwareCollector
12
{
13
    protected string $identifier = "WordPress";
14
15
    private array $inventory;
16
17
    public function setInventory(array $inventory): void
18
    {
19
        $this->inventory = $inventory;
20
    }
21
22
    public function collect(): array
23
    {
24
        if (!array_key_exists(ApacheServerNameCollector::COLLECTION_IDENTIFIER, $this->inventory)
25
            || !is_array($this->inventory[ApacheServerNameCollector::COLLECTION_IDENTIFIER])
26
        ) return [];
27
28
        $configs = $this->inventory[ApacheServerNameCollector::COLLECTION_IDENTIFIER];
29
30
        $uptimeStatus = [];
31
32
        $client = new Client();
33
34
        foreach ($configs as $config) {
35
            $domain = $config[ApacheServerNameCollector::FIELD_SERVER_NAME];
36
            try {
37
                $result = $client->get('https://' . $domain);
38
            } catch (ServerException $e) {
39
                $uptimeStatus[$domain] = $e->getResponse()->getStatusCode();
40
                continue;
41
            } catch (\Exception) {
42
                $uptimeStatus[$domain] = 599;
43
                continue;
44
            }
45
46
            $uptimeStatus[$domain] = $result->getStatusCode();
47
        }
48
49
        return $uptimeStatus;
50
    }
51
}
52