forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivityLogService.php
More file actions
177 lines (146 loc) · 4.35 KB
/
Copy pathActivityLogService.php
File metadata and controls
177 lines (146 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace Pterodactyl\Services\Activity;
use Illuminate\Support\Collection;
use Pterodactyl\Models\ActivityLog;
use Illuminate\Contracts\Auth\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\ConnectionInterface;
class ActivityLogService
{
protected ?ActivityLog $activity = null;
protected Factory $manager;
protected ConnectionInterface $connection;
protected AcitvityLogBatchService $batch;
protected ActivityLogTargetableService $targetable;
public function __construct(
Factory $manager,
AcitvityLogBatchService $batch,
ActivityLogTargetableService $targetable,
ConnectionInterface $connection
) {
$this->manager = $manager;
$this->batch = $batch;
$this->targetable = $targetable;
$this->connection = $connection;
}
/**
* Sets the activity logger as having been caused by an anonymous
* user type.
*/
public function anonymous(): self
{
$this->getActivity()->actor_id = null;
$this->getActivity()->actor_type = null;
$this->getActivity()->setRelation('actor', null);
return $this;
}
/**
* Sets the action for this activity log.
*/
public function event(string $action): self
{
$this->getActivity()->event = $action;
return $this;
}
/**
* Set the description for this activity.
*/
public function withDescription(?string $description): self
{
$this->getActivity()->description = $description;
return $this;
}
/**
* Sets the subject model instance.
*/
public function withSubject(Model $subject): self
{
$this->getActivity()->subject()->associate($subject);
return $this;
}
/**
* Sets the actor model instance.
*/
public function withActor(Model $actor): self
{
$this->getActivity()->actor()->associate($actor);
return $this;
}
/**
* Sets the custom properties for the activity log instance.
*
* @param \Illuminate\Support\Collection|array $properties
*/
public function withProperties($properties): self
{
$this->getActivity()->properties = Collection::make($properties);
return $this;
}
/**
* Sets a custom property on the activty log instance.
*
* @param mixed $value
*/
public function withProperty(string $key, $value): self
{
$this->getActivity()->properties = $this->getActivity()->properties->put($key, $value);
return $this;
}
/**
* Logs an activity log entry with the set values and then returns the
* model instance to the caller.
*/
public function log(string $description): ActivityLog
{
$this->withDescription($description);
$activity = $this->activity;
$activity->save();
$this->activity = null;
return $activity;
}
/**
* Executes the provided callback within the scope of a database transaction
* and will only save the activity log entry if everything else succesfully
* settles.
*
* @return mixed
*
* @throws \Throwable
*/
public function transaction(\Closure $callback, string $description = null)
{
if (!is_null($description)) {
$this->withDescription($description);
}
return $this->connection->transaction(function () use ($callback) {
$response = $callback($activity = $this->getActivity());
$activity->save();
$this->activity = null;
return $response;
});
}
/**
* Returns the current activity log instance.
*/
protected function getActivity(): ActivityLog
{
if ($this->activity) {
return $this->activity;
}
$this->activity = new ActivityLog([
'batch_uuid' => $this->batch->uuid(),
'properties' => Collection::make([]),
]);
if ($subject = $this->targetable->subject()) {
$this->withSubject($subject);
}
if ($actor = $this->targetable->actor()) {
$this->withActor($actor);
} elseif ($user = $this->manager->guard()->user()) {
if ($user instanceof Model) {
$this->withActor($user);
}
}
return $this->activity;
}
}