Passed
Push — ft/config-healthmonitor ( f13174 )
by Philippe
14:52 queued 03:34
created

HomepageAccessibleCheck   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
eloc 13
c 2
b 0
f 1
dl 0
loc 34
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 3 1
A get_http_response_code() 0 16 2
A message() 0 3 1
A notifiers() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Thinktomorrow\Chief\HealthMonitor\Checks;
6
7
use Thinktomorrow\Chief\HealthMonitor\Notifiers\AlertBarNotifier;
8
use Thinktomorrow\Chief\Settings\Homepage;
9
10
class HomepageAccessibleCheck implements HealthCheck
11
{
12
    public function check(): bool
13
    {
14
        return $this->get_http_response_code(Homepage::url()) == 200;
15
    }
16
17
    private function get_http_response_code(string $url)
18
    {
19
        if ($url =='') {
20
            return false;
21
        }
22
23
        // Avoid ssl errors: SSL operation failed with code 1
24
        stream_context_set_default([
25
            'ssl' => [
26
                'verify_peer' => false,
27
                'verify_peer_name' => false,
28
            ],
29
        ]);
30
31
        $headers = get_headers($url);
32
        return substr($headers[0], 9, 3);
33
    }
34
35
    public function message(): string
36
    {
37
        return 'Het lijkt erop dat de homepagina niet meer bereikbaar is. <a href="'. route('chief.back.settings.edit') .'" class="text-secondary-800 underline hover:text-white">Kies een nieuwe</a>.';
38
    }
39
40
    public function notifiers(): array
41
    {
42
        return [
43
            AlertBarNotifier::class,
44
        ];
45
    }
46
}
47