HeaderCollector::setClient()   A
last analyzed

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 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 HeaderCollector extends BasicCollector implements InventoryAwareCollector, ClientAwareCollector
14
{
15
    protected string $identifier = "WebsiteHeader";
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
        $headers = [];
35
36
        foreach ($domains as $domain) {
37
            try {
38
                $url = $domain;
39
                if (!str_starts_with($url, 'http')) $url = 'https://' . $url;
40
                $response = $this->client->get($url);
41
            } catch (ClientException $e) {
42
                $response = $e->getResponse();
43
            } catch (ServerException $e) {
44
                $response = $e->getResponse();
45
            } catch (\Exception) {
46
                continue;
47
            }
48
49
            $headers[$domain] = $response->getHeaders();
50
        }
51
52
        return $headers;
53
    }
54
}
55