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 ( 614aca...a6c60d )
by Freek
02:09
created

Site::shouldCheckUptime()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 0
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\UptimeMonitor\Models;
4
5
use Exception;
6
use Spatie\SslCertificate\SslCertificate;
7
use Spatie\UptimeMonitor\Events\InvalidSslCertificateFound;
8
use Spatie\UptimeMonitor\Events\SiteDown;
9
use Carbon\Carbon;
10
use Illuminate\Database\Eloquent\Model;
11
use Spatie\UptimeMonitor\Events\SiteRestored;
12
use Spatie\UptimeMonitor\Events\SiteUp;
13
use Spatie\UptimeMonitor\Events\SoonExpiringSslCertificateFound;
14
use Spatie\UptimeMonitor\Events\ValidSslCertificateFound;
15
use Spatie\UptimeMonitor\Models\Enums\SslCertificateStatus;
16
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus;
17
use Spatie\UptimeMonitor\Models\Presenters\SitePresenter;
18
use Spatie\Url\Url;
19
20
class Site extends Model
21
{
22
    use SitePresenter;
23
24
    protected $guarded = [];
25
26
    protected $dates = [
27
        'uptime_last_check_date',
28
        'uptime_status_last_change_date',
29
        'ssl_certificate_expiration_date',
30
    ];
31
32
    public function scopeEnabled($query)
33
    {
34
        return $query->where('enabled', true);
35
    }
36
37
    public function getUrlAttribute()
38
    {
39
        return Url::fromString($this->attributes['url']);
40
    }
41
42
    public static function boot()
43
    {
44
        static::saving(function (Site $site) {
45
            if (is_null($site->uptime_status_last_change_date)) {
46
                $site->uptime_status_last_change_date = Carbon::now();
47
48
                return;
49
            }
50
51
            if ($site->getOriginal('uptime_status') != $site->uptime_status) {
52
                $site->uptime_status_last_change_date = Carbon::now();
53
            }
54
        });
55
    }
56
57
    public function shouldCheckUptime() : bool
58
    {
59
        if (! $this->enabled) {
60
            return false;
61
        }
62
63
        if ($this->uptime_status = UptimeStatus::NOT_YET_CHECKED) {
64
            return true;
65
        }
66
67
        if ($this->uptime_status === UptimeStatus::DOWN) {
68
            return true;
69
        }
70
71
        return $this->uptime_last_check_date->diffInMinutes() >= $this->ping_every_minutes;
72
    }
73
74
    public function pingSucceeded($responseHtml)
75
    {
76
        if (! $this->lookForStringPresentOnResponse($responseHtml)) {
77
            $this->siteIsDown("String `{$this->look_for_string}` was not found on the response");
78
        }
79
80
        $this->siteIsUp();
81
    }
82
83
    public function pingFailed(string $reason)
84
    {
85
        $this->siteIsDown($reason);
86
    }
87
88
    public function siteIsUp()
89
    {
90
        $this->uptime_status = UptimeStatus::UP;
91
        $this->uptime_failure_reason = '';
92
93
        $wasFailing = ! is_null($this->down_event_fired_on_date);
94
95
        $this->uptime_check_times_failed_in_a_row = 0;
96
        $this->uptime_last_check_date = Carbon::now();
97
        $this->down_event_fired_on_date = null;
98
99
        $this->save();
100
101
        $eventClass = ($wasFailing ? SiteRestored::class : SiteUp::class);
102
103
        event(new $eventClass($this));
104
    }
105
106
    public function siteIsDown(string $reason)
107
    {
108
        $this->uptime_status = UptimeStatus::DOWN;
109
110
        $this->uptime_check_times_failed_in_a_row++;
111
112
        $this->uptime_last_check_date = Carbon::now();
113
114
        $this->uptime_failure_reason = $reason;
115
116
        $this->save();
117
118
        if ($this->shouldFireDownEvent()) {
119
            $this->down_event_fired_on_date = Carbon::now();
120
121
            event(new SiteDown($this));
122
        }
123
    }
124
125
    public function lookForStringPresentOnResponse(string $responseHtml = '') : bool
126
    {
127
        if ($this->look_for_string == '') {
128
            return true;
129
        }
130
131
        return str_contains($responseHtml, $this->look_for_string);
132
    }
133
134
    protected function shouldFireDownEvent(): bool
135
    {
136
        if ($this->uptime_check_times_failed_in_a_row === config('laravel-uptime-monitor.fire_down_event_after_consecutive_failed_checks')) {
137
            return true;
138
        }
139
140
        if (is_null($this->down_event_fired_on_date)) {
141
            return false;
142
        }
143
144
        if (config('resend_down_notification_every_minutes') == 0) {
145
            return false;
146
        }
147
148
        if ($this->down_event_fired_on_date()->diffInMinutes() >= config('laravel-uptime-monitor.resend_down_notification_every_minutes')) {
149
            return true;
150
        }
151
152
        return false;
153
    }
154
155
    public function isHealthy()
156
    {
157
        if (in_array($this->uptime_status, [UptimeStatus::DOWN, UptimeStatus::NOT_YET_CHECKED])) {
158
            return false;
159
        }
160
161
        if ($this->check_ssl_certificate && $this->ssl_certificate_status === SslCertificateStatus::INVALID) {
162
            return false;
163
        }
164
165
        return true;
166
    }
167
168
    public function updateWithCertificate(SslCertificate $certificate)
169
    {
170
        $this->ssl_certificate_status = $certificate->isValid($this->url)
171
            ? SslCertificateStatus::VALID
172
            : SslCertificateStatus::INVALID;
173
174
        $this->ssl_certificate_expiration_date = $certificate->expirationDate();
175
176
        $this->ssl_certificate_issuer = $certificate->getIssuer();
177
178
        $this->save();
179
180
        $this->fireEventsForUpdatedSiteWithCertificate($this, $certificate);
181
    }
182
183
    public function updateWithCertificateException(Exception $exception)
184
    {
185
        $this->ssl_certificate_status = SslCertificateStatus::INVALID;
186
        $this->ssl_certificate_expiration_date = null;
187
        $this->ssl_certificate_issuer = '';
188
        $this->ssl_certificate_failure_reason = $exception->getMessage();
189
190
        $this->save();
191
192
        event(new InvalidSslCertificateFound($this, $exception->getMessage()));
193
    }
194
195
    protected function fireEventsForUpdatedSiteWithCertificate(Site $site, SslCertificate $certificate)
196
    {
197
        if ($this->ssl_certificate_status === SslCertificateStatus::VALID) {
198
            event(new ValidSslCertificateFound($this, $certificate));
199
200
            if ($certificate->expirationDate()->diffInDays() <= config('laravel-uptime-monitor.notifications.send_notification_when_ssl_certificate_will_expire_in_days')) {
201
                event(new SoonExpiringSslCertificateFound($site, $certificate));
202
            }
203
204
            return;
205
        }
206
207
        if ($this->ssl_certificate_status === SslCertificateStatus::INVALID) {
208
209
            $reason = 'Unknown reason';
210
211
            if ($certificate->appliesToUrl($this->url)) {
212
                $reason = "Certificate does not apply to {$this->url} but only to these domains: " . implode(',', $certificate->getAdditionalDomains());
213
            }
214
215
            if ($certificate->isExpired()) {
216
                $reason = "The certificate is expired";
217
            }
218
219
            event(new InvalidSslCertificateFound($this, $reason, $certificate));
220
        }
221
    }
222
}
223