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
Push — master ( 73f9d5...efcda2 )
by Freek
9s
created

Site::isHealthy()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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