|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use App\Events\PingFailure; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @property int id |
|
12
|
|
|
* @property array tags |
|
13
|
|
|
* @property string name |
|
14
|
|
|
* @property bool overdue |
|
15
|
|
|
* @property bool error |
|
16
|
|
|
* @property Carbon last_ping |
|
17
|
|
|
* @property int frequency_value |
|
18
|
|
|
* @property string frequency |
|
19
|
|
|
* @property Carbon overdueDate |
|
20
|
|
|
*/ |
|
21
|
|
|
class Ping extends Model |
|
22
|
|
|
{ |
|
23
|
|
|
use SoftDeletes; |
|
24
|
|
|
|
|
25
|
|
|
protected $dates = ['last_ping', 'deleted_at']; |
|
26
|
|
|
|
|
27
|
|
|
protected $appends = ['ping_url']; |
|
28
|
|
|
|
|
29
|
|
|
protected $casts = [ |
|
30
|
|
|
'active' => 'boolean', |
|
31
|
|
|
'error' => 'boolean', |
|
32
|
|
|
'frequency_value' => 'integer', |
|
33
|
|
|
'created_by' => 'integer', |
|
34
|
|
|
'updated_by' => 'integer', |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Attributes |
|
40
|
|
|
*/ |
|
41
|
|
|
|
|
42
|
|
View Code Duplication |
public function getTagsAttribute() |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
if (empty($this->attributes['tags'])) { |
|
45
|
|
|
return []; |
|
46
|
|
|
} |
|
47
|
|
|
$tagArray = explode(',', $this->attributes['tags']); |
|
48
|
|
|
|
|
49
|
|
|
return $this->trimArrayItems($tagArray); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
View Code Duplication |
public function setTagsAttribute($tags) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
if (is_array($tags)) { |
|
56
|
|
|
$this->attributes['tags'] = implode(',', $this->trimArrayItems($tags)); |
|
57
|
|
|
} else { |
|
58
|
|
|
$this->attributes['tags'] = trim($tags); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getLastPingAttribute() |
|
63
|
|
|
{ |
|
64
|
|
|
if ($this->attributes['last_ping'] == '0000-00-00') { |
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->attributes['last_ping']; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getPingUrlAttribute() |
|
72
|
|
|
{ |
|
73
|
|
|
return route('ping_url', $this->attributes['name']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* The date when the check becaomes overdue |
|
79
|
|
|
* |
|
80
|
|
|
* @return Carbon |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getOverdueDateAttribute() |
|
83
|
|
|
{ |
|
84
|
|
|
if (is_null($this->last_ping)) { |
|
85
|
|
|
return new Carbon(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
switch ($this->frequency) { |
|
89
|
|
|
case 'minute': |
|
90
|
|
|
return (new Carbon($this->last_ping))->addMinutes($this->frequency_value); |
|
91
|
|
|
case 'hour': |
|
92
|
|
|
return (new Carbon($this->last_ping))->addHours($this->frequency_value); |
|
93
|
|
|
case 'day': |
|
94
|
|
|
return (new Carbon($this->last_ping))->addDays($this->frequency_value); |
|
95
|
|
|
case 'week': |
|
96
|
|
|
return (new Carbon($this->last_ping))->addWeeks($this->frequency_value); |
|
97
|
|
|
case 'month': |
|
98
|
|
|
return (new Carbon($this->last_ping))->addMonths($this->frequency_value); |
|
99
|
|
|
case 'year': |
|
100
|
|
|
return (new Carbon($this->last_ping))->addYear($this->frequency_value); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* This this ping overdue? |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getOverdueAttribute() |
|
111
|
|
|
{ |
|
112
|
|
|
return ($this->overdue_date->lt(new Carbon())); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Scopes |
|
119
|
|
|
*/ |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Scope a query to only include active pings. |
|
123
|
|
|
* |
|
124
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
125
|
|
|
*/ |
|
126
|
|
|
public function scopeActive($query) |
|
127
|
|
|
{ |
|
128
|
|
|
return $query->where('active', 1); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Methods |
|
134
|
|
|
*/ |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Locate an existing ping or create a new one |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $name |
|
140
|
|
|
* |
|
141
|
|
|
* @return Ping |
|
142
|
|
|
*/ |
|
143
|
|
|
public static function findOrNewFromName($name) |
|
144
|
|
|
{ |
|
145
|
|
|
$ping = self::where('name', $name)->first(); |
|
146
|
|
|
|
|
147
|
|
|
if (!$ping) { |
|
148
|
|
|
$ping = self::createDefaultPing($name, true); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $ping; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Create a new ping object with the default starting params |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $name |
|
158
|
|
|
* @param bool $active |
|
159
|
|
|
* @param null $createdBy |
|
160
|
|
|
* |
|
161
|
|
|
* @return Ping |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function createDefaultPing($name, $active, $createdBy = null) |
|
164
|
|
|
{ |
|
165
|
|
|
$ping = new Ping; |
|
166
|
|
|
$ping->name = $name; |
|
167
|
|
|
if ($createdBy) { |
|
168
|
|
|
$ping->created_by = $createdBy; |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
$ping->active = $active; |
|
|
|
|
|
|
171
|
|
|
$ping->description = ''; |
|
|
|
|
|
|
172
|
|
|
$ping->tags = []; |
|
173
|
|
|
$ping->error = false; |
|
174
|
|
|
$ping->frequency = 'day'; |
|
175
|
|
|
$ping->frequency_value = '1'; |
|
176
|
|
|
$ping->save(); |
|
177
|
|
|
|
|
178
|
|
|
return $ping; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Set the error state for the ping |
|
183
|
|
|
*/ |
|
184
|
|
|
public function setError() |
|
185
|
|
|
{ |
|
186
|
|
|
$broadcastEvent = false; |
|
187
|
|
|
if ($this->error == false) { |
|
|
|
|
|
|
188
|
|
|
$broadcastEvent = true; |
|
189
|
|
|
} |
|
190
|
|
|
$this->error = true; |
|
191
|
|
|
$this->save(); |
|
192
|
|
|
|
|
193
|
|
|
if ($broadcastEvent) { |
|
194
|
|
|
event(new PingFailure($this)); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Clear the error state for the ping |
|
200
|
|
|
*/ |
|
201
|
|
|
public function clearError() |
|
202
|
|
|
{ |
|
203
|
|
|
$this->error = false; |
|
204
|
|
|
$this->save(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function recordPingUpdate() |
|
208
|
|
|
{ |
|
209
|
|
|
$this->last_ping = new Carbon(); |
|
210
|
|
|
$this->error = false; |
|
211
|
|
|
$this->save(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Static |
|
217
|
|
|
*/ |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* The base url needed to make a ping url |
|
222
|
|
|
* |
|
223
|
|
|
* @return string |
|
224
|
|
|
*/ |
|
225
|
|
|
public static function baseUrl() |
|
226
|
|
|
{ |
|
227
|
|
|
return route('ping_url', ''); |
|
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Trim each item in the array |
|
232
|
|
|
* |
|
233
|
|
|
* @param array $tags |
|
234
|
|
|
* |
|
235
|
|
|
* @return array |
|
236
|
|
|
*/ |
|
237
|
|
|
private function trimArrayItems(array $tags) |
|
238
|
|
|
{ |
|
239
|
|
|
return array_map(function($tag) { |
|
240
|
|
|
return trim($tag); |
|
241
|
|
|
}, $tags); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.