Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Activity/ActivityLogBatchService.php
10264 views
1
<?php
2
3
namespace Pterodactyl\Services\Activity;
4
5
use Ramsey\Uuid\Uuid;
6
7
class ActivityLogBatchService
8
{
9
protected int $transaction = 0;
10
protected ?string $uuid = null;
11
12
/**
13
* Returns the UUID of the batch, or null if there is not a batch currently
14
* being executed.
15
*/
16
public function uuid(): ?string
17
{
18
return $this->uuid;
19
}
20
21
/**
22
* Starts a new batch transaction. If there is already a transaction present
23
* this will be nested.
24
*/
25
public function start(): void
26
{
27
if ($this->transaction === 0) {
28
$this->uuid = Uuid::uuid4()->toString();
29
}
30
31
++$this->transaction;
32
}
33
34
/**
35
* Ends a batch transaction, if this is the last transaction in the stack
36
* the UUID will be cleared out.
37
*/
38
public function end(): void
39
{
40
$this->transaction = max(0, $this->transaction - 1);
41
42
if ($this->transaction === 0) {
43
$this->uuid = null;
44
}
45
}
46
47
/**
48
* Executes the logic provided within the callback in the scope of an activity
49
* log batch transaction.
50
*/
51
public function transaction(\Closure $callback): mixed
52
{
53
$this->start();
54
$result = $callback($this->uuid());
55
$this->end();
56
57
return $result;
58
}
59
}
60
61