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 ( 4c8ea6...9fb14b )
by Freek
07:41
created

Site::updateWithCertifcateException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
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 Spatie\SslCertificate\SslCertificate;
6
use Spatie\UptimeMonitor\Events\SiteDown;
7
use Carbon\Carbon;
8
use Illuminate\Database\Eloquent\Model;
9
use Spatie\UptimeMonitor\Events\SiteRestored;
10
use Spatie\UptimeMonitor\Events\SiteUp;
11
use Spatie\UptimeMonitor\Models\Enums\SslCertificateStatus;
12
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus;
13
use Spatie\Url\Url;
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
            event(new SiteDown($this));
108
        }
109
    }
110
111
    public function lookForStringPresentOnResponse(string $responseHtml = '') : bool
112
    {
113
        if ($this->look_for_string == '') {
114
            return true;
115
        }
116
117
        return str_contains($responseHtml, $this->look_for_string);
118
    }
119
120
    public function getPingRequestMethod() : string
121
    {
122
        return $this->look_for_string == '' ? 'HEAD' : 'GET';
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
164
    public function updateWithCertifcateException($exception)
165
    {
166
        $this->ssl_certificate_status = SslCertificateStatus::INVALID;
167
        $this->ssl_certificate_expiration_date = null;
168
        $this->ssl_certificate_issuer = '';
169
170
        $this->save();
171
    }
172
}
173