1 | <?php |
||
18 | class Site extends Model |
||
19 | { |
||
20 | use SitePresenter; |
||
21 | |||
22 | protected $guarded = []; |
||
23 | |||
24 | protected $dates = [ |
||
25 | 'uptime_last_check_date', |
||
26 | 'uptime_status_last_change_date', |
||
27 | 'ssl_certificate_expiration_date', |
||
28 | ]; |
||
29 | |||
30 | public function scopeEnabled($query) |
||
31 | { |
||
32 | return $query->where('enabled', true); |
||
33 | } |
||
34 | |||
35 | public function getUrlAttribute() |
||
36 | { |
||
37 | return Url::fromString($this->attributes['url']); |
||
38 | } |
||
39 | |||
40 | public static function boot() |
||
41 | { |
||
42 | static::saving(function (Site $site) { |
||
43 | if ($site->getOriginal('status') != $site->status) { |
||
44 | $site->uptime_status_last_change_date = Carbon::now(); |
||
45 | } |
||
46 | }); |
||
47 | } |
||
48 | |||
49 | public function shouldCheckUptime() : bool |
||
50 | { |
||
51 | if (! $this->enabled) { |
||
52 | return false; |
||
53 | } |
||
54 | |||
55 | if ($this->uptime_status = UptimeStatus::NOT_YET_CHECKED) { |
||
56 | return true; |
||
57 | } |
||
58 | |||
59 | if ($this->uptime_status === UptimeStatus::DOWN) { |
||
60 | return true; |
||
61 | } |
||
62 | |||
63 | return $this->uptime_last_check_date->diffInMinutes() >= $this->ping_every_minutes; |
||
64 | } |
||
65 | |||
66 | public function pingSucceeded($responseHtml) |
||
67 | { |
||
68 | if (! $this->lookForStringPresentOnResponse($responseHtml)) { |
||
69 | $this->siteIsDown("String `{$this->look_for_string}` was not found on the response"); |
||
70 | } |
||
71 | |||
72 | $this->siteIsUp(); |
||
73 | } |
||
74 | |||
75 | public function pingFailed(string $reason) |
||
76 | { |
||
77 | $this->siteIsDown($reason); |
||
78 | } |
||
79 | |||
80 | public function siteIsUp() |
||
81 | { |
||
82 | $this->uptime_status = UptimeStatus::UP; |
||
83 | $this->uptime_failure_reason = ''; |
||
84 | |||
85 | $wasFailing = $this->uptime_check_times_failed_in_a_row > 0; |
||
86 | |||
87 | $this->uptime_check_times_failed_in_a_row = 0; |
||
88 | $this->uptime_last_check_date = Carbon::now(); |
||
89 | |||
90 | $this->save(); |
||
91 | |||
92 | $eventClass = ($wasFailing ? SiteRestored::class : SiteUp::class); |
||
93 | |||
94 | event(new $eventClass($this)); |
||
95 | } |
||
96 | |||
97 | public function siteIsDown(string $reason) |
||
98 | { |
||
99 | $previousStatus = $this->uptime_status; |
||
100 | |||
101 | $this->uptime_status = UptimeStatus::DOWN; |
||
102 | |||
103 | $this->uptime_check_times_failed_in_a_row++; |
||
104 | |||
105 | $this->uptime_last_check_date = Carbon::now(); |
||
106 | |||
107 | $this->uptime_failure_reason = $reason; |
||
108 | |||
109 | $this->save(); |
||
110 | |||
111 | if ($this->shouldFireDownEvent($previousStatus)) { |
||
112 | event(new SiteDown($this)); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | public function lookForStringPresentOnResponse(string $responseHtml = '') : bool |
||
117 | { |
||
118 | if ($this->look_for_string == '') { |
||
119 | return true; |
||
120 | } |
||
121 | |||
122 | return str_contains($responseHtml, $this->look_for_string); |
||
123 | } |
||
124 | |||
125 | protected function shouldFireDownEvent($previousStatus): bool |
||
137 | |||
138 | public function isHealthy() |
||
150 | |||
151 | public function updateWithCertificate(SslCertificate $certificate) |
||
152 | { |
||
153 | $this->ssl_certificate_status = $certificate->isValid() |
||
154 | ? SslCertificateStatus::VALID |
||
155 | : SslCertificateStatus::INVALID; |
||
156 | |||
157 | $this->ssl_certificate_expiration_date = $certificate->expirationDate(); |
||
158 | |||
159 | $this->ssl_certificate_issuer = $certificate->getIssuer(); |
||
165 | |||
166 | public function updateWithCertificateException(Exception $exception) |
||
177 | } |
||
178 |