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 ( 1347f4...2e6529 )
by Freek
01:54
created

Site::getPingRequestMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
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\Models\Enums\SslCertificateStatus;
14
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus;
15
use Spatie\UptimeMonitor\Models\Presenters\SitePresenter;
16
use Spatie\Url\Url;
17
18
class Site extends Model
19
{
20
    use SitePresenter;
21
22
    protected $guarded = [];
23
24
    protected $dates = [
25
        'uptime_last_check_date',
26
        'uptime_status_last_change_date',
27
        'ssl_certificate_expiration_date',
28
    ];
29
30
    public function scopeEnabled($query)
31
    {
32
        return $query->where('enabled', true);
33
    }
34
35
    public function getUrlAttribute()
36
    {
37
        return Url::fromString($this->attributes['url']);
38
    }
39
40
    public static function boot()
41
    {
42
        static::saving(function (Site $site) {
43
            if ($site->getOriginal('status') != $site->status) {
44
                $site->uptime_status_last_change_date = Carbon::now();
45
            }
46
        });
47
    }
48
49
    public function shouldCheckUptime() : bool
50
    {
51
        if (! $this->enabled) {
52
            return false;
53
        }
54
55
        if ($this->uptime_status = UptimeStatus::NOT_YET_CHECKED) {
56
            return true;
57
        }
58
59
        if ($this->uptime_status === UptimeStatus::DOWN) {
60
            return true;
61
        }
62
63
        return $this->uptime_last_check_date->diffInMinutes() >= $this->ping_every_minutes;
64
    }
65
66
    public function pingSucceeded($responseHtml)
67
    {
68
        if (! $this->lookForStringPresentOnResponse($responseHtml)) {
69
            $this->siteIsDown("String `{$this->look_for_string}` was not found on the response");
70
        }
71
72
        $this->siteIsUp();
73
    }
74
75
    public function pingFailed(string $reason)
76
    {
77
        $this->siteIsDown($reason);
78
    }
79
80
    public function siteIsUp()
81
    {
82
        $this->uptime_status = UptimeStatus::UP;
83
        $this->uptime_failure_reason = '';
84
85
        $wasFailing = $this->uptime_check_times_failed_in_a_row > 0;
86
87
        $this->uptime_check_times_failed_in_a_row = 0;
88
        $this->uptime_last_check_date = Carbon::now();
89
90
        $this->save();
91
92
        $eventClass = ($wasFailing ? SiteRestored::class : SiteUp::class);
93
94
        event(new $eventClass($this));
95
    }
96
97
    public function siteIsDown(string $reason)
98
    {
99
        $previousStatus = $this->uptime_status;
100
101
        $this->uptime_status = UptimeStatus::DOWN;
102
103
        $this->uptime_check_times_failed_in_a_row++;
104
105
        $this->uptime_last_check_date = Carbon::now();
106
107
        $this->uptime_failure_reason = $reason;
108
109
        $this->save();
110
111
        if ($this->shouldFireDownEvent($previousStatus)) {
112
            event(new SiteDown($this));
113
        }
114
    }
115
116
    public function lookForStringPresentOnResponse(string $responseHtml = '') : bool
117
    {
118
        if ($this->look_for_string == '') {
119
            return true;
120
        }
121
122
        return str_contains($responseHtml, $this->look_for_string);
123
    }
124
125
    protected function shouldFireDownEvent($previousStatus): bool
126
    {
127
        if ($previousStatus != UptimeStatus::DOWN) {
128
            return true;
129
        }
130
131
        if (Carbon::now()->diffInMinutes() >= config('resend_down_notification_every_minutes')) {
132
            return true;
133
        }
134
135
        return false;
136
    }
137
138
    public function isHealthy()
139
    {
140
        if (in_array($this->uptime_status, [UptimeStatus::DOWN, UptimeStatus::NOT_YET_CHECKED])) {
141
            return false;
142
        }
143
144
        if ($this->check_ssl_certificate && $this->ssl_certificate_status === SslCertificateStatus::INVALID) {
145
            return false;
146
        }
147
148
        return true;
149
    }
150
151
    public function updateWithCertificate(SslCertificate $certificate)
152
    {
153
        $this->ssl_certificate_status = $certificate->isValid()
154
            ? SslCertificateStatus::VALID
155
            : SslCertificateStatus::INVALID;
156
157
        $this->ssl_certificate_expiration_date = $certificate->expirationDate();
158
159
        $this->ssl_certificate_issuer = $certificate->getIssuer();
160
161
        $this->save();
162
163
        event(new ValidSslCertificateFound($this));
164
    }
165
166
    public function updateWithCertificateException(Exception $exception)
167
    {
168
        $this->ssl_certificate_status = SslCertificateStatus::INVALID;
169
        $this->ssl_certificate_expiration_date = null;
170
        $this->ssl_certificate_issuer = '';
171
        $this->ssl_certificate_failure_reason = $exception->getMessage();
172
173
        $this->save();
174
175
        event(new InvalidSslCertificateFound($this));
176
    }
177
}
178