Skip to content

Commit e700b4d

Browse files
committed
Whoops, don't store the model until we've successfully completed the transaction internals
1 parent 8d69a60 commit e700b4d

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

app/Models/Server.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,20 +334,18 @@ public function mounts()
334334
}
335335

336336
/**
337-
* Saves an audit entry to the database for the server.
337+
* Returns a fresh AuditLog model for the server. This model is not saved to the
338+
* database when created, so it is up to the caller to correctly store it as needed.
338339
*
339340
* @param string $action
340341
* @param array $metadata
341342
* @return \Pterodactyl\Models\AuditLog
342343
*/
343-
public function newAuditEvent(string $action, array $metadata): AuditLog
344+
public function newAuditEvent(string $action, array $metadata = []): AuditLog
344345
{
345-
$model = AuditLog::factory($action, $metadata)->fill([
346+
return AuditLog::factory($action, $metadata)->fill([
346347
'server_id' => $this->id,
347348
]);
348-
$model->save();
349-
350-
return $model;
351349
}
352350

353351
/**
@@ -366,9 +364,8 @@ public function newAuditEvent(string $action, array $metadata): AuditLog
366364
*/
367365
public function audit(string $action, Closure $callback)
368366
{
369-
$model = $this->newAuditEvent($action, []);
370-
371-
return $this->getConnection()->transaction(function () use ($callback, &$model) {
367+
return $this->getConnection()->transaction(function () use ($action, $callback) {
368+
$model = $this->newAuditEvent($action);
372369
$response = $callback($model, $this);
373370
$model->save();
374371

0 commit comments

Comments
 (0)