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
Pull Request — master (#127)
by
unknown
01:27
created

setUptimeCheckAdditionalHeadersAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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