Skip to content

Commit 2d9550f

Browse files
authored
Merge pull request #502 from jacobmllr95/feat-tenant-not-found-for-request-event
Add new `TenantNotFoundForRequestEvent`
2 parents 5253dce + 3c17101 commit 2d9550f

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

docs/advanced-usage/listening-for-events.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,26 @@ The package fires events where you can listen for to perform some extra logic.
99

1010
This event will fire when a tenant is being made the current one. At this point none of [the tasks](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/overview/) have been executed.
1111

12-
It has one public property `$tenant`, that contains an instance of `Spatie\Multitenancy\Models\Tenant`
12+
It has one public property `$tenant`, that contains an instance of `Spatie\Multitenancy\Models\Tenant`.
1313

1414
## `\Spatie\Multitenancy\Events\MadeTenantCurrentEvent`
1515

1616
This event will fire when a tenant has been made the current one. At this point the `makeCurrent` method of all of [the tasks](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/overview/) have been executed. The current tenant also have been bound as `currentTenant` in the container.
1717

18-
It has one public property `$tenant`, that contains an instance of `Spatie\Multitenancy\Models\Tenant`
18+
It has one public property `$tenant`, that contains an instance of `Spatie\Multitenancy\Models\Tenant`.
1919

2020
## `\Spatie\Multitenancy\Events\ForgettingCurrentTenantEvent`
2121

2222
This event will fire when a tenant is being forgotten. At this point none of [the tasks](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/overview/) have been executed.
2323

24-
It has one public property `$tenant`, that contains an instance of `Spatie\Multitenancy\Models\Tenant`
24+
It has one public property `$tenant`, that contains an instance of `Spatie\Multitenancy\Models\Tenant`.
2525

2626
## `\Spatie\Multitenancy\Events\ForgotCurrentTenantEvent`
2727

2828
This event will fire when a tenant has been forgotten. At this point the `forgotCurrent` method of all of [the tasks](/docs/laravel-multitenancy/v3/using-tasks-to-prepare-the-environment/overview/) have been executed. `currentTenant` in the container has been emptied.
29+
30+
## `\Spatie\Multitenancy\Events\TenantNotFoundForRequestEvent`
31+
32+
This event will fire when no tenant was found by the `findForRequest()` method of the `TenantFinder` for the given request.
33+
34+
It has one public property `$request`, that contains an instance of `Illuminate\Http\Request`.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Spatie\Multitenancy\Events;
4+
5+
use Illuminate\Http\Request;
6+
7+
class TenantNotFoundForRequestEvent
8+
{
9+
public function __construct(
10+
public Request $request
11+
) {
12+
}
13+
}

src/Multitenancy.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Contracts\Foundation\Application;
66
use Spatie\Multitenancy\Actions\MakeQueueTenantAwareAction;
77
use Spatie\Multitenancy\Concerns\UsesMultitenancyConfig;
8+
use Spatie\Multitenancy\Events\TenantNotFoundForRequestEvent;
89
use Spatie\Multitenancy\Models\Concerns\UsesTenantModel;
910
use Spatie\Multitenancy\Models\Tenant;
1011
use Spatie\Multitenancy\Tasks\TasksCollection;
@@ -44,7 +45,11 @@ protected function determineCurrentTenant(): void
4445

4546
$tenant = $tenantFinder->findForRequest($this->app['request']);
4647

47-
$tenant?->makeCurrent();
48+
if ($tenant instanceof Tenant) {
49+
$tenant->makeCurrent();
50+
} else {
51+
event(new TenantNotFoundForRequestEvent($this->app['request']));
52+
}
4853
}
4954

5055
protected function registerTasksCollection(): self

0 commit comments

Comments
 (0)