GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#17)
by Freek
111:15 queued 61:06
created

Site::isHealthy()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 0
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\UptimeMonitor\Models;
4
5
use Spatie\UptimeMonitor\Events\SiteDown;
6
use Carbon\Carbon;
7
use Illuminate\Database\Eloquent\Model;
8
use Spatie\UptimeMonitor\Events\SiteRestored;
9
use Spatie\UptimeMonitor\Events\SiteUp;
10
use Spatie\UptimeMonitor\Models\Enums\SslCertificateStatus;
11
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus;
12
use Spatie\Url\Url;
13
14
class Site extends Model
15
{
16
    protected $guarded = [];
17
18
    protected $dates = [
19
        'uptime_last_checked_on',
20
        'last_uptime_status_change_on',
21
        'ssl_certificate_expiration_date',
22
    ];
23
24
    public function scopeEnabled($query)
25
    {
26
        return $query->where('enabled', true);
27
    }
28
29
    public function getUrlAttribute()
30
    {
31
        return Url::fromString($this->attributes['url']);
32
    }
33
34
    public static function boot()
35
    {
36
        static::saving(function (Site $site) {
37
            if ($site->getOriginal('status') != $site->status) {
38
                $site->last_uptime_status_change_on = Carbon::now();
39
            }
40
        });
41
    }
42
43
    public function shouldCheckUptime() : bool
44
    {
45
        if (! $this->enabled) {
46
            return false;
47
        }
48
49
        if ($this->uptime_status = UptimeStatus::NOT_YET_CHECKED) {
50
            return true;
51
        }
52
53
        if ($this->uptime_status === UptimeStatus::DOWN) {
54
            return true;
55
        }
56
57
        return $this->uptime_last_checked_on->diffInMinutes() >= $this->ping_every_minutes;
58
    }
59
60
    public function pingSucceeded($responseHtml)
61
    {
62
        if (! $this->lookForStringPresentOnResponse($responseHtml)) {
63
            $this->siteIsDown("String `{$this->look_for_string}` was not found on the response");
64
        }
65
66
        $this->siteIsUp();
67
    }
68
69
    public function pingFailed(string $reason)
70
    {
71
        $this->siteIsDown($reason);
72
    }
73
74
    public function siteIsUp()
75
    {
76
        $this->uptime_status = UptimeStatus::UP;
77
        $this->last_failure_reason = '';
78
79
        $wasFailing = $this->times_failed_in_a_row > 0;
80
81
        $this->times_failed_in_a_row = 0;
82
        $this->uptime_last_checked_on = Carbon::now();
83
84
        $this->save();
85
86
        $eventClass = ($wasFailing ? SiteRestored::class : SiteUp::class);
87
88
        event(new $eventClass($this));
89
    }
90
91
    public function siteIsDown(string $reason)
92
    {
93
        $previousStatus = $this->uptime_status;
94
95
        $this->uptime_status = UptimeStatus::DOWN;
96
97
        $this->times_failed_in_a_row++;
98
99
        $this->uptime_last_checked_on = Carbon::now();
100
101
        $this->last_failure_reason = $reason;
102
103
        $this->save();
104
105
        if ($this->shouldFireDownEvent($previousStatus)) {
106
            event(new SiteDown($this));
107
        }
108
    }
109
110
    public function lookForStringPresentOnResponse(string $responseHtml = '') : bool
111
    {
112
        if ($this->look_for_string == '') {
113
            return true;
114
        }
115
116
        return str_contains($responseHtml, $this->look_for_string);
117
    }
118
119
    public function getPingRequestMethod() : string
120
    {
121
        return $this->look_for_string == '' ? 'HEAD' : 'GET';
122
    }
123
124
    protected function shouldFireDownEvent($previousStatus): bool
125
    {
126
        if ($previousStatus != UptimeStatus::DOWN) {
127
            return true;
128
        }
129
130
        if (Carbon::now()->diffInMinutes() >= config('resend_down_notification_every_minutes')) {
131
            return true;
132
        }
133
134
        return false;
135
    }
136
137
    public function isHealthy()
138
    {
139
        if (in_array($this->uptime_status, [UptimeStatus::DOWN, UptimeStatus::NOT_YET_CHECKED])) {
140
            return false;
141
        }
142
143
        if ($this->check_ssl_certificate && $this->ssl_certificate_status === SslCertificateStatus::INVALID) {
144
            return false;
145
        }
146
147
        return true;
148
    }
149
}
150