forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAcitvityLogBatchService.php
More file actions
62 lines (52 loc) · 1.34 KB
/
AcitvityLogBatchService.php
File metadata and controls
62 lines (52 loc) · 1.34 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
<?php
namespace Pterodactyl\Services\Activity;
use Ramsey\Uuid\Uuid;
class AcitvityLogBatchService
{
protected int $transaction = 0;
protected ?string $uuid = null;
/**
* Returns the UUID of the batch, or null if there is not a batch currently
* being executed.
*/
public function uuid(): ?string
{
return $this->uuid;
}
/**
* Starts a new batch transaction. If there is already a transaction present
* this will be nested.
*/
public function start(): void
{
if ($this->transaction === 0) {
$this->uuid = Uuid::uuid4()->toString();
}
++$this->transaction;
}
/**
* Ends a batch transaction, if this is the last transaction in the stack
* the UUID will be cleared out.
*/
public function end(): void
{
$this->transaction = max(0, $this->transaction - 1);
if ($this->transaction === 0) {
$this->uuid = null;
}
}
/**
* Executes the logic provided within the callback in the scope of an activity
* log batch transaction.
*
* @param \Closure $callback
* @return mixed
*/
public function transaction(\Closure $callback)
{
$this->start();
$result = $callback($this->uuid());
$this->end();
return $result;
}
}