1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JeroenG\ActivityLogger; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This class is the core of the package. Everything is handles through here, |
7
|
|
|
* although you might always use the Facade 'Activity'. |
8
|
|
|
* |
9
|
|
|
* @author JeroenG |
10
|
|
|
**/ |
11
|
|
|
class ActivityLogger |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Log an activity. |
15
|
|
|
* |
16
|
|
|
* @param string $message A basic message |
17
|
|
|
* @param array $context Any additional data you might want to save. |
18
|
|
|
* @param timestamp $date Timestamp of when it's happened. If none is passed the current timestamp will be used. |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function log($message, $context = [], $date = null) |
22
|
|
|
{ |
23
|
|
|
if (is_null($date)) { |
24
|
|
|
$date = new \DateTime; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return Activity::create([ |
28
|
|
|
'message' => $message, |
29
|
|
|
'context' => $context, |
30
|
|
|
'created_at' => $date, |
31
|
|
|
]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get all data from one log entry. |
36
|
|
|
* |
37
|
|
|
* @param int $id The id of the log. |
38
|
|
|
* @return object |
39
|
|
|
*/ |
40
|
|
|
public function getLog($id) |
41
|
|
|
{ |
42
|
|
|
return Activity::find($id); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get all data from all logs. |
47
|
|
|
* |
48
|
|
|
* @return object |
49
|
|
|
*/ |
50
|
|
|
public function getAllLogs() |
51
|
|
|
{ |
52
|
|
|
return Activity::orderBy('created_at', 'desc')->get(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get all logs created within a given timespan. |
57
|
|
|
* |
58
|
|
|
* @param timestamp $start Every log created after this date. |
59
|
|
|
* @param timestamp $end Every log created before this date. |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function getLogsBetween($start, $end) |
63
|
|
|
{ |
64
|
|
|
return Activity::whereBetween('created_at', [$start, $end])->orderBy('created_at', 'desc')->get(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the most recent logs. |
69
|
|
|
* |
70
|
|
|
* @param int $number (optional) The number of logs to retrieve, default is 5. |
71
|
|
|
* @return object |
72
|
|
|
*/ |
73
|
|
|
public function getRecentLogs($number = 5) |
74
|
|
|
{ |
75
|
|
|
return Activity::take($number)->orderBy('created_at', 'desc')->get(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Delete all logs (not way back!). |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function deleteAll() |
83
|
|
|
{ |
84
|
|
|
return Activity::deleteAll(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|