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 ( 7f1d39...078202 )
by Freek
03:49 queued 02:41
created

Monitor::booted()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\UptimeMonitor\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\UptimeMonitor\Exceptions\CannotSaveMonitor;
7
use Spatie\UptimeMonitor\Models\Enums\CertificateStatus;
8
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus;
9
use Spatie\UptimeMonitor\Models\Presenters\MonitorPresenter;
10
use Spatie\UptimeMonitor\Models\Traits\SupportsCertificateCheck;
11
use Spatie\UptimeMonitor\Models\Traits\SupportsUptimeCheck;
12
use Spatie\Url\Url;
13
14
class Monitor extends Model
15
{
16
    use SupportsUptimeCheck;
17
    use SupportsCertificateCheck;
18
    use MonitorPresenter;
19
20
    protected $guarded = [];
21
22
    protected $appends = ['raw_url'];
23
24
    protected $dates = [
25
        'uptime_last_check_date',
26
        'uptime_status_last_change_date',
27
        'uptime_check_failed_event_fired_on_date',
28
        'certificate_expiration_date',
29
    ];
30
31
    protected $casts = [
32
        'uptime_check_enabled' => 'boolean',
33
        'certificate_check_enabled' => 'boolean',
34
    ];
35
36
    public function getUptimeCheckAdditionalHeadersAttribute($additionalHeaders): array
37
    {
38
        return $additionalHeaders
39
            ? json_decode($additionalHeaders, true)
40
            : [];
41
    }
42
43
    public function setUptimeCheckAdditionalHeadersAttribute(array $additionalHeaders): void
44
    {
45
        $this->attributes['uptime_check_additional_headers'] = json_encode($additionalHeaders);
46
    }
47
48
    public function scopeEnabled($query)
49
    {
50
        return $query
51
            ->where('uptime_check_enabled', true)
52
            ->orWhere('certificate_check_enabled', true);
53
    }
54
55
    public function getUrlAttribute(): ?Url
56
    {
57
        if (! isset($this->attributes['url'])) {
58
            return null;
59
        }
60
61
        return Url::fromString($this->attributes['url']);
62
    }
63
64
    public function getRawUrlAttribute(): string
65
    {
66
        return (string) $this->url;
67
    }
68
69
    public static function booted()
70
    {
71
        static::saving(function (self $monitor) {
72
            if (static::alreadyExists($monitor)) {
73
                throw CannotSaveMonitor::alreadyExists($monitor);
74
            }
75
        });
76
    }
77
78
    public function isHealthy(): bool
79
    {
80
        if ($this->uptime_check_enabled && in_array($this->uptime_status, [UptimeStatus::DOWN, UptimeStatus::NOT_YET_CHECKED])) {
81
            return false;
82
        }
83
84
        if ($this->certificate_check_enabled && $this->certificate_status === CertificateStatus::INVALID) {
85
            return false;
86
        }
87
88
        return true;
89
    }
90
91
    public function enable(): self
92
    {
93
        $this->uptime_check_enabled = true;
94
95
        if ($this->url->getScheme() === 'https') {
96
            $this->certificate_check_enabled = true;
97
        }
98
99
        $this->save();
100
101
        return $this;
102
    }
103
104
    public function disable(): self
105
    {
106
        $this->uptime_check_enabled = false;
107
        $this->certificate_check_enabled = false;
108
109
        $this->save();
110
111
        return $this;
112
    }
113
114
    protected static function alreadyExists(self $monitor): bool
115
    {
116
        $query = static::where('url', $monitor->url);
117
118
        if ($monitor->exists) {
119
            $query->where('id', '<>', $monitor->id);
120
        }
121
122
        return (bool) $query->first();
123
    }
124
}
125