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 ( 2f10be...8a7e89 )
by Freek
04:01 queued 02:04
created

Monitor   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 5
dl 0
loc 97
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeEnabled() 0 6 1
A getUrlAttribute() 0 8 2
A boot() 0 10 2
B isHealthy() 0 12 5
A enable() 0 9 1
A disable() 0 9 1
A alreadyExists() 0 10 2
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
        SupportsCertificateCheck,
18
        MonitorPresenter;
19
20
    protected $guarded = [];
21
22
    protected $dates = [
23
        'uptime_last_check_date',
24
        'uptime_status_last_change_date',
25
        'down_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 scopeEnabled($query)
35
    {
36
        return $query
37
            ->where('uptime_check_enabled', true)
38
            ->orWhere('certificate_check_enabled', true);
39
    }
40
41
    public function getUrlAttribute()
42
    {
43
        if (! isset($this->attributes['url'])) {
44
            return;
45
        }
46
47
        return Url::fromString($this->attributes['url']);
48
    }
49
50
    public static function boot()
51
    {
52
        parent::boot();
53
54
        static::saving(function (Monitor $monitor) {
55
            if (static::alreadyExists($monitor)) {
56
                throw CannotSaveMonitor::alreadyExists($monitor);
57
            }
58
        });
59
    }
60
61
    public function isHealthy()
62
    {
63
        if ($this->uptime_check_enabled && in_array($this->uptime_status, [UptimeStatus::DOWN, UptimeStatus::NOT_YET_CHECKED])) {
64
            return false;
65
        }
66
67
        if ($this->certificate_check_enabled && $this->certificate_status === CertificateStatus::INVALID) {
68
            return false;
69
        }
70
71
        return true;
72
    }
73
74
    /**
75
     * @return $this
76
     */
77
    public function enable()
78
    {
79
        $this->uptime_check_enabled = true;
80
        $this->certificate_check_enabled = true;
81
82
        $this->save();
83
84
        return $this;
85
    }
86
87
    /**
88
     * @return $this
89
     */
90
    public function disable()
91
    {
92
        $this->uptime_check_enabled = false;
93
        $this->certificate_check_enabled = false;
94
95
        $this->save();
96
97
        return $this;
98
    }
99
100
    protected static function alreadyExists(Monitor $monitor): bool
101
    {
102
        $query = static::where('url', $monitor->url);
103
104
        if ($monitor->exists) {
105
            $query->where('id', '<>', $monitor->id);
106
        }
107
108
        return (bool) $query->first();
109
    }
110
}
111