Skip to content

Commit

Permalink
refactor: Rework the context setting for the tenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Sep 27, 2024
1 parent 107d513 commit 1cebaa4
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Listeners/SetCurrentTenantContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ final class SetCurrentTenantContext
*/
public function handle(CurrentTenantChanged $event): void
{
$contextKey = 'sprout.tenants.' . $event->tenancy->getName();
$contextKey = 'sprout.tenants';
$context = [];

if ($event->current === null && Context::has($contextKey)) {
Context::forget($contextKey);
} else if ($event->current !== null && ! Context::has($contextKey)) {
Context::add($contextKey, $event->current->getTenantKey());
if (Context::has($contextKey)) {
/** @var array<string, int|string> $context */
$context = Context::get($contextKey, []);
}

if ($event->current === null) {
unset($context[$event->tenancy->getName()]);
} else {
$context[$event->tenancy->getName()] = $event->current->getTenantKey();
}

Context::add($contextKey, $context);
}
}

0 comments on commit 1cebaa4

Please sign in to comment.