Skip to content

Commit

Permalink
Merge pull request #502 from jacobmllr95/feat-tenant-not-found-for-re…
Browse files Browse the repository at this point in the history
…quest-event

Add new `TenantNotFoundForRequestEvent`
  • Loading branch information
masterix21 committed Jan 9, 2024
2 parents 5253dce + 3c17101 commit 2d9550f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
12 changes: 9 additions & 3 deletions docs/advanced-usage/listening-for-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@ The package fires events where you can listen for to perform some extra logic.

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.

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

## `\Spatie\Multitenancy\Events\MadeTenantCurrentEvent`

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.

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

## `\Spatie\Multitenancy\Events\ForgettingCurrentTenantEvent`

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.

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

## `\Spatie\Multitenancy\Events\ForgotCurrentTenantEvent`

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.

## `\Spatie\Multitenancy\Events\TenantNotFoundForRequestEvent`

This event will fire when no tenant was found by the `findForRequest()` method of the `TenantFinder` for the given request.

It has one public property `$request`, that contains an instance of `Illuminate\Http\Request`.
13 changes: 13 additions & 0 deletions src/Events/TenantNotFoundForRequestEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\Multitenancy\Events;

use Illuminate\Http\Request;

class TenantNotFoundForRequestEvent
{
public function __construct(
public Request $request
) {
}
}
7 changes: 6 additions & 1 deletion src/Multitenancy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Foundation\Application;
use Spatie\Multitenancy\Actions\MakeQueueTenantAwareAction;
use Spatie\Multitenancy\Concerns\UsesMultitenancyConfig;
use Spatie\Multitenancy\Events\TenantNotFoundForRequestEvent;
use Spatie\Multitenancy\Models\Concerns\UsesTenantModel;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\Tasks\TasksCollection;
Expand Down Expand Up @@ -44,7 +45,11 @@ protected function determineCurrentTenant(): void

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

$tenant?->makeCurrent();
if ($tenant instanceof Tenant) {
$tenant->makeCurrent();
} else {
event(new TenantNotFoundForRequestEvent($this->app['request']));
}
}

protected function registerTasksCollection(): self
Expand Down

0 comments on commit 2d9550f

Please sign in to comment.