Skip to content

Commit

Permalink
as a callable within a transaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Jan 25, 2025
1 parent 9d66a20 commit 6220a05
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions app/Actions/Sharing/Propagate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;

class Propagate
final class Propagate
{
/**
* Update all descendants with the current album access permissions.
* This is run in a DB transaction for safety.
*
* @param Album $album
*
Expand All @@ -28,9 +29,24 @@ public function update(Album $album): void
{
if (!App::runningUnitTests()) {
// @codeCoverageIgnoreStart
DB::beginTransaction();
DB::transaction(fn () => $this->applyUpdate($album));

return;
// @codeCoverageIgnoreEnd
}

$this->applyUpdate($album);
}

/**
* Apply the current album access permissions to all descendants.
*
* @param Album $album
*
* @return void
*/
private function applyUpdate(Album $album): void
{
// for each descendant, create a new permission if it does not exist.
// or update the existing permission.
/** @var Collection<int,string> $descendants */
Expand All @@ -55,12 +71,6 @@ public function update(Album $album): void
$perm->save();
});
});

if (!App::runningUnitTests()) {
// @codeCoverageIgnoreStart
DB::commit();
// @codeCoverageIgnoreEnd
}
}

/**
Expand All @@ -74,10 +84,24 @@ public function overwrite(Album $album): void
{
if (!App::runningUnitTests()) {
// @codeCoverageIgnoreStart
DB::beginTransaction();
DB::transaction(fn () => $this->applyOverwrite($album));

return;
// @codeCoverageIgnoreEnd
}

$this->applyOverwrite($album);
}

/**
* Apply the overwrite of all descendants with the current album access permissions.
*
* @param Album $album
*
* @return void
*/
private function applyOverwrite(Album $album): void
{
// override permission for all descendants albums.
// Faster done by:
// 1. clearing all the permissions.
Expand Down Expand Up @@ -120,11 +144,5 @@ public function overwrite(Album $album): void
);

DB::table(APC::ACCESS_PERMISSIONS)->insert($new_perm);

if (!App::runningUnitTests()) {
// @codeCoverageIgnoreStart
DB::commit();
// @codeCoverageIgnoreEnd
}
}
}

0 comments on commit 6220a05

Please sign in to comment.