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 ( 2d48be...f6c8f6 )
by Freek
05:26
created

SupportsUptimeCheck::shouldFireDownEvent()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 5
nop 0
1
<?php
2
3
namespace Spatie\UptimeMonitor\Models\Traits;
4
5
use Spatie\UptimeMonitor\Events\SiteDown;
6
use Carbon\Carbon;
7
use Spatie\UptimeMonitor\Events\SiteRestored;
8
use Spatie\UptimeMonitor\Events\SiteUp;
9
use Spatie\UptimeMonitor\Models\Enums\UptimeStatus;
10
11
trait SupportsUptimeCheck
12
{
13
    public static function bootSupportsUptimeCheck()
14
    {
15
        dd('omg it booted');
16
        static::saving(function (Site $site) {
17
            if (is_null($site->uptime_status_last_change_date)) {
18
                $site->uptime_status_last_change_date = Carbon::now();
19
20
                return;
21
            }
22
23
            if ($site->getOriginal('uptime_status') != $site->uptime_status) {
24
                $site->uptime_status_last_change_date = Carbon::now();
25
            }
26
        });
27
    }
28
29
30
    public function shouldCheckUptime() : bool
31
    {
32
        if (!$this->enabled) {
0 ignored issues
show
Bug introduced by
The property enabled does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
            return false;
34
        }
35
36
        if ($this->uptime_status == UptimeStatus::NOT_YET_CHECKED) {
0 ignored issues
show
Bug introduced by
The property uptime_status does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
            return true;
38
        }
39
40
        if ($this->uptime_status == UptimeStatus::DOWN) {
41
            return true;
42
        }
43
44
        if (is_null($this->uptime_last_check_date)) {
0 ignored issues
show
Bug introduced by
The property uptime_last_check_date does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
            return true;
46
        }
47
48
        return $this->uptime_last_check_date->diffInMinutes() >= $this->uptime_check_interval_in_minutes;
0 ignored issues
show
Bug introduced by
The property uptime_check_interval_in_minutes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
    }
50
51
    public function couldReachSite($responseHtml)
52
    {
53
        if (!str_contains($responseHtml, $this->look_for_string)) {
54
            $this->siteIsDown("String `{$this->look_for_string}` was not found on the response.");
0 ignored issues
show
Bug introduced by
The property look_for_string does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
        }
56
57
        $this->siteIsUp();
58
    }
59
60
    public function couldNotReachSite(string $reason)
61
    {
62
        $this->siteIsDown($reason);
63
    }
64
65
    public function siteIsUp()
66
    {
67
        $this->uptime_status = UptimeStatus::UP;
68
        $this->uptime_failure_reason = '';
0 ignored issues
show
Bug introduced by
The property uptime_failure_reason does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
70
        $wasFailing = !is_null($this->down_event_fired_on_date);
0 ignored issues
show
Bug introduced by
The property down_event_fired_on_date does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71
72
        $this->uptime_check_times_failed_in_a_row = 0;
0 ignored issues
show
Bug introduced by
The property uptime_check_times_failed_in_a_row does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
73
        $this->uptime_last_check_date = Carbon::now();
74
        $this->down_event_fired_on_date = null;
75
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
76
77
        $eventClass = ($wasFailing ? SiteRestored::class : SiteUp::class);
78
79
        event(new $eventClass($this));
80
    }
81
82
    public function siteIsDown(string $reason)
83
    {
84
        $this->uptime_status = UptimeStatus::DOWN;
85
        $this->uptime_check_times_failed_in_a_row++;
86
        $this->uptime_last_check_date = Carbon::now();
87
        $this->uptime_failure_reason = $reason;
88
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
89
90
        if ($this->shouldFireDownEvent()) {
91
92
            $this->down_event_fired_on_date = Carbon::now();
93
            $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
94
95
            event(new SiteDown($this));
96
        }
97
    }
98
99
    protected function shouldFireDownEvent(): bool
100
    {
101
        if ($this->uptime_check_times_failed_in_a_row === config('laravel-uptime-monitor.uptime_check.fire_down_event_after_consecutive_failures')) {
102
            return true;
103
        }
104
105
        if (is_null($this->down_event_fired_on_date)) {
106
            return false;
107
        }
108
109
        if (config('laravel-uptime-monitor.notifications.resend_down_notification_every_minutes') === 0) {
110
            return false;
111
        }
112
113
        if ($this->down_event_fired_on_date->diffInMinutes() >= config('laravel-uptime-monitor.notifications.resend_down_notification_every_minutes')) {
114
            return true;
115
        }
116
117
        return false;
118
    }
119
}