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 ( 9e170a...fa9406 )
by Freek
04:03
created

Site::boot()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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