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 ( d3b5af...6f32cb )
by Freek
03:26
created

Site::pingSucceeded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
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 ($site->getOriginal('status') != $site->status) {
46
                $site->uptime_status_last_change_date = Carbon::now();
47
            }
48
        });
49
    }
50
51
    public function shouldCheckUptime() : bool
52
    {
53
        if (! $this->enabled) {
54
            return false;
55
        }
56
57
        if ($this->uptime_status = UptimeStatus::NOT_YET_CHECKED) {
58
            return true;
59
        }
60
61
        if ($this->uptime_status === UptimeStatus::DOWN) {
62
            return true;
63
        }
64
65
        return $this->uptime_last_check_date->diffInMinutes() >= $this->ping_every_minutes;
66
    }
67
68
    public function pingSucceeded($responseHtml)
69
    {
70
        if (! $this->lookForStringPresentOnResponse($responseHtml)) {
71
            $this->siteIsDown("String `{$this->look_for_string}` was not found on the response");
72
        }
73
74
        $this->siteIsUp();
75
    }
76
77
    public function pingFailed(string $reason)
78
    {
79
        $this->siteIsDown($reason);
80
    }
81
82
    public function siteIsUp()
83
    {
84
        $this->uptime_status = UptimeStatus::UP;
85
        $this->uptime_failure_reason = '';
86
87
        $wasFailing = $this->uptime_check_times_failed_in_a_row > 0;
88
89
        $this->uptime_check_times_failed_in_a_row = 0;
90
        $this->uptime_last_check_date = Carbon::now();
91
92
        $this->save();
93
94
        $eventClass = ($wasFailing ? SiteRestored::class : SiteUp::class);
95
96
        event(new $eventClass($this));
97
    }
98
99
    public function siteIsDown(string $reason)
100
    {
101
        $previousStatus = $this->uptime_status;
102
103
        $this->uptime_status = UptimeStatus::DOWN;
104
105
        $this->uptime_check_times_failed_in_a_row++;
106
107
        $this->uptime_last_check_date = Carbon::now();
108
109
        $this->uptime_failure_reason = $reason;
110
111
        $this->save();
112
113
        if ($this->shouldFireDownEvent($previousStatus)) {
114
            event(new SiteDown($this));
115
        }
116
    }
117
118
    public function lookForStringPresentOnResponse(string $responseHtml = '') : bool
119
    {
120
        if ($this->look_for_string == '') {
121
            return true;
122
        }
123
124
        return str_contains($responseHtml, $this->look_for_string);
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
            return false;
144
        }
145
146
        if ($this->check_ssl_certificate && $this->ssl_certificate_status === SslCertificateStatus::INVALID) {
147
            return false;
148
        }
149
150
        return true;
151
    }
152
153
    public function updateWithCertificate(SslCertificate $certificate)
154
    {
155
        $this->ssl_certificate_status = $certificate->isValid($this->url)
156
            ? SslCertificateStatus::VALID
157
            : SslCertificateStatus::INVALID;
158
159
        $this->ssl_certificate_expiration_date = $certificate->expirationDate();
160
161
        $this->ssl_certificate_issuer = $certificate->getIssuer();
162
163
        $this->save();
164
165
        $this->fireEventsForUpdatedSiteWithCertificate($this, $certificate);
166
    }
167
168
    public function updateWithCertificateException(Exception $exception)
169
    {
170
        $this->ssl_certificate_status = SslCertificateStatus::INVALID;
171
        $this->ssl_certificate_expiration_date = null;
172
        $this->ssl_certificate_issuer = '';
173
        $this->ssl_certificate_failure_reason = $exception->getMessage();
174
175
        $this->save();
176
177
        event(new InvalidSslCertificateFound($this, $exception->getMessage()));
178
    }
179
180
    protected function fireEventsForUpdatedSiteWithCertificate(Site $site, SslCertificate $certificate)
181
    {
182
        if ($this->ssl_certificate_status === SslCertificateStatus::VALID) {
183
            event(new ValidSslCertificateFound($this, $certificate));
184
185
            if ($certificate->expirationDate()->diffInDays() <= config('laravel-uptime-monitor.send_notification_when_ssl_certificate_will_expire_in_days')) {
186
                event(new SoonExpiringSslCertificateFound($site, $certificate));
187
            }
188
189
            return;
190
        }
191
192
        if ($this->ssl_certificate_status === SslCertificateStatus::INVALID) {
193
194
            $reason = 'Unknown reason';
195
196
            if ($certificate->appliesToUrl($this->url)) {
197
                $reason = "Certificate does not apply to {$this->url} but only to these domains: " . implode(',', $certificate->getAdditionalDomains());
198
            }
199
200
            if ($certificate->isExpired()) {
201
                $reason = "The certificate is expired";
202
            }
203
204
            event(new InvalidSslCertificateFound($this, $reason, $certificate));
205
        }
206
    }
207
}
208