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 ( 11f95b...c6944f )
by Freek
02:24
created

UptimeMonitor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 0
loc 74
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldRun() 0 12 3
A pingSucceeded() 0 16 2
A lookForStringPresentOnResponse() 0 8 2
A pingFailed() 0 14 1
A getCacheKey() 0 4 1
A getPingRequestMethod() 0 4 2
1
<?php
2
3
namespace Spatie\UptimeMonitor\Models;
4
5
use App\Events\SiteDown;
6
use Carbon\Carbon;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
10
use Illuminate\Database\Eloquent\Relations\HasMany;
11
use Illuminate\Http\Request;
12
use UrlSigner;
13
14
class UptimeMonitor extends Model
15
{
16
    const STATUS_ONLINE = 'online';
17
    const STATUS_OFFLINE = 'offline';
18
    const STATUS_NEVER_CHECKED = 'never checked';
19
20
    protected $guarded = [];
21
22
    protected $dates = ['last_checked_on'];
23
24
    public function shouldRun() : bool
25
    {
26
        if (is_null($this->last_checked_on)) {
27
            return true;
28
        }
29
30
        if ($this->status === self::STATUS_OFFLINE) {
31
            return true;
32
        }
33
34
        return $this->last_checked_on->diffInMinutes() >= $this->ping_every_minutes;
35
    }
36
37
    public function pingSucceeded($responseHtml)
38
    {
39
        $this->status = self::STATUS_ONLINE;
40
        $this->last_failure_reason = '';
41
42
        $wasFailing = $this->times_failed_in_a_row > 0;
43
44
        $this->times_failed_in_a_row = 0;
45
        $this->last_checked_on = Carbon::now();
46
47
        $this->save();
48
49
        $eventClass = 'App\\Events\\'.($wasFailing ? 'SiteRestored' : 'SiteUp');
50
51
        event(new $eventClass($this));
52
    }
53
54
    public function lookForStringPresentOnResponse(string $responseHtml = '') : bool
55
    {
56
        if ($this->look_for_string == '') {
57
            return true;
58
        }
59
60
        return str_contains($responseHtml, $this->look_for_string);
61
    }
62
63
    public function pingFailed(string $reason)
64
    {
65
        $this->status = self::STATUS_OFFLINE;
66
67
        $this->times_failed_in_a_row++;
68
69
        $this->last_checked_on = Carbon::now();
70
71
        $this->last_failure_reason = $reason;
72
73
        $this->save();
74
75
        event(new SiteDown($this));
76
    }
77
78
    public function getCacheKey() : string
79
    {
80
        return "{$this->getPingRequestMethod()}:{$this->url}";
81
    }
82
83
    public function getPingRequestMethod() : string
84
    {
85
        return $this->look_for_string == '' ? 'HEAD' : 'GET';
86
    }
87
}
88