Files

69 lines
1.4 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Services\Activity;
2026-10-05 18:10:18 -04:00
use Closure;
use Ramsey\Uuid\Uuid;
2022-10-14 10:59:20 -06:00
class ActivityLogBatchService
{
protected int $transaction = 0;
2026-10-05 18:10:18 -04:00
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();
}
2026-10-05 18:10:18 -04:00
$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.
2026-10-05 18:10:18 -04:00
*
2026-10-11 19:54:55 -04:00
* @template TReturn
2026-10-05 18:10:18 -04:00
*
* @param Closure(?string): TReturn $callback
* @return TReturn
*/
2026-10-05 18:10:18 -04:00
public function transaction(Closure $callback): mixed
{
$this->start();
$result = $callback($this->uuid());
$this->end();
return $result;
}
}