Skip to content

Commit d5aaa4c

Browse files
authored
Merge pull request #471 from raymadrona/main
Bind or forget current tenant
2 parents cab0ca6 + 292fbcc commit d5aaa4c

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

src/Actions/MakeQueueTenantAwareAction.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Queue\Events\JobProcessing;
66
use Illuminate\Queue\Events\JobRetryRequested;
77
use Illuminate\Support\Arr;
8+
use Spatie\Multitenancy\Concerns\BindAsCurrentTenant;
89
use Spatie\Multitenancy\Exceptions\CurrentTenantCouldNotBeDeterminedInTenantAwareJob;
910
use Spatie\Multitenancy\Jobs\NotTenantAware;
1011
use Spatie\Multitenancy\Jobs\TenantAware;
@@ -14,6 +15,7 @@
1415
class MakeQueueTenantAwareAction
1516
{
1617
use UsesTenantModel;
18+
use BindAsCurrentTenant;
1719

1820
public function execute(): void
1921
{
@@ -41,11 +43,7 @@ protected function listenForJobsBeingQueued(): static
4143
protected function listenForJobsBeingProcessed(): static
4244
{
4345
app('events')->listen(JobProcessing::class, function (JobProcessing $event) {
44-
$this->getTenantModel()::forgetCurrent();
45-
46-
if (array_key_exists('tenantId', $event->job->payload())) {
47-
$this->findTenant($event)->makeCurrent();
48-
}
46+
$this->bindOrForgetCurrentTenant($event);
4947
});
5048

5149
return $this;
@@ -54,11 +52,7 @@ protected function listenForJobsBeingProcessed(): static
5452
protected function listenForJobsRetryRequested(): static
5553
{
5654
app('events')->listen(JobRetryRequested::class, function (JobRetryRequested $event) {
57-
$this->getTenantModel()::forgetCurrent();
58-
59-
if (array_key_exists('tenantId', $event->payload())) {
60-
$this->findTenant($event)->makeCurrent();
61-
}
55+
$this->bindOrForgetCurrentTenant($event);
6256
});
6357

6458
return $this;
@@ -131,4 +125,15 @@ protected function getJobFromQueueable(object $queueable)
131125

132126
return $queueable->$job;
133127
}
128+
129+
protected function bindOrForgetCurrentTenant(JobProcessing|JobRetryRequested $event): void
130+
{
131+
if (array_key_exists('tenantId', $this->getEventPayload($event))) {
132+
$this->bindAsCurrentTenant($this->findTenant($event)->makeCurrent());
133+
134+
return;
135+
}
136+
137+
$this->getTenantModel()::forgetCurrent();
138+
}
134139
}

src/Actions/MakeTenantCurrentAction.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Spatie\Multitenancy\Actions;
44

5+
use Spatie\Multitenancy\Concerns\BindAsCurrentTenant;
56
use Spatie\Multitenancy\Events\MadeTenantCurrentEvent;
67
use Spatie\Multitenancy\Events\MakingTenantCurrentEvent;
78
use Spatie\Multitenancy\Models\Tenant;
@@ -10,6 +11,8 @@
1011

1112
class MakeTenantCurrentAction
1213
{
14+
use BindAsCurrentTenant;
15+
1316
public function __construct(
1417
protected TasksCollection $tasksCollection
1518
) {
@@ -34,15 +37,4 @@ protected function performTasksToMakeTenantCurrent(Tenant $tenant): self
3437

3538
return $this;
3639
}
37-
38-
protected function bindAsCurrentTenant(Tenant $tenant): self
39-
{
40-
$containerKey = config('multitenancy.current_tenant_container_key');
41-
42-
app()->forgetInstance($containerKey);
43-
44-
app()->instance($containerKey, $tenant);
45-
46-
return $this;
47-
}
4840
}

src/Concerns/BindAsCurrentTenant.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Spatie\Multitenancy\Concerns;
4+
5+
use Spatie\Multitenancy\Models\Tenant;
6+
7+
trait BindAsCurrentTenant
8+
{
9+
protected function bindAsCurrentTenant(Tenant $tenant): self
10+
{
11+
$containerKey = config('multitenancy.current_tenant_container_key');
12+
13+
app()->forgetInstance($containerKey);
14+
15+
app()->instance($containerKey, $tenant);
16+
17+
return $this;
18+
}
19+
}

0 commit comments

Comments
 (0)