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