Skip to content

Commit

Permalink
Merge pull request #471 from raymadrona/main
Browse files Browse the repository at this point in the history
Bind or forget current tenant
  • Loading branch information
masterix21 authored Jun 30, 2023
2 parents cab0ca6 + 292fbcc commit d5aaa4c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
25 changes: 15 additions & 10 deletions src/Actions/MakeQueueTenantAwareAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\JobRetryRequested;
use Illuminate\Support\Arr;
use Spatie\Multitenancy\Concerns\BindAsCurrentTenant;
use Spatie\Multitenancy\Exceptions\CurrentTenantCouldNotBeDeterminedInTenantAwareJob;
use Spatie\Multitenancy\Jobs\NotTenantAware;
use Spatie\Multitenancy\Jobs\TenantAware;
Expand All @@ -14,6 +15,7 @@
class MakeQueueTenantAwareAction
{
use UsesTenantModel;
use BindAsCurrentTenant;

public function execute(): void
{
Expand Down Expand Up @@ -41,11 +43,7 @@ protected function listenForJobsBeingQueued(): static
protected function listenForJobsBeingProcessed(): static
{
app('events')->listen(JobProcessing::class, function (JobProcessing $event) {
$this->getTenantModel()::forgetCurrent();

if (array_key_exists('tenantId', $event->job->payload())) {
$this->findTenant($event)->makeCurrent();
}
$this->bindOrForgetCurrentTenant($event);
});

return $this;
Expand All @@ -54,11 +52,7 @@ protected function listenForJobsBeingProcessed(): static
protected function listenForJobsRetryRequested(): static
{
app('events')->listen(JobRetryRequested::class, function (JobRetryRequested $event) {
$this->getTenantModel()::forgetCurrent();

if (array_key_exists('tenantId', $event->payload())) {
$this->findTenant($event)->makeCurrent();
}
$this->bindOrForgetCurrentTenant($event);
});

return $this;
Expand Down Expand Up @@ -131,4 +125,15 @@ protected function getJobFromQueueable(object $queueable)

return $queueable->$job;
}

protected function bindOrForgetCurrentTenant(JobProcessing|JobRetryRequested $event): void
{
if (array_key_exists('tenantId', $this->getEventPayload($event))) {
$this->bindAsCurrentTenant($this->findTenant($event)->makeCurrent());

return;
}

$this->getTenantModel()::forgetCurrent();
}
}
14 changes: 3 additions & 11 deletions src/Actions/MakeTenantCurrentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Multitenancy\Actions;

use Spatie\Multitenancy\Concerns\BindAsCurrentTenant;
use Spatie\Multitenancy\Events\MadeTenantCurrentEvent;
use Spatie\Multitenancy\Events\MakingTenantCurrentEvent;
use Spatie\Multitenancy\Models\Tenant;
Expand All @@ -10,6 +11,8 @@

class MakeTenantCurrentAction
{
use BindAsCurrentTenant;

public function __construct(
protected TasksCollection $tasksCollection
) {
Expand All @@ -34,15 +37,4 @@ protected function performTasksToMakeTenantCurrent(Tenant $tenant): self

return $this;
}

protected function bindAsCurrentTenant(Tenant $tenant): self
{
$containerKey = config('multitenancy.current_tenant_container_key');

app()->forgetInstance($containerKey);

app()->instance($containerKey, $tenant);

return $this;
}
}
19 changes: 19 additions & 0 deletions src/Concerns/BindAsCurrentTenant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spatie\Multitenancy\Concerns;

use Spatie\Multitenancy\Models\Tenant;

trait BindAsCurrentTenant
{
protected function bindAsCurrentTenant(Tenant $tenant): self
{
$containerKey = config('multitenancy.current_tenant_container_key');

app()->forgetInstance($containerKey);

app()->instance($containerKey, $tenant);

return $this;
}
}

0 comments on commit d5aaa4c

Please sign in to comment.