-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIdentificationMiddleware.php
57 lines (45 loc) · 1.58 KB
/
IdentificationMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace App\Containers\Larabeans\Tenanter\Middleware;
use App\Containers\Larabeans\Tenanter\Contracts\HostCouldNotBeIdentifiedException;
use App\Containers\Larabeans\Tenanter\Contracts\TenantCouldNotBeIdentifiedException;
use App\Containers\Larabeans\Tenanter\Contracts\TenantResolver;
use App\Containers\Larabeans\Tenanter\Tenancy;
abstract class IdentificationMiddleware
{
/** @var callable */
public static $onFail;
/** @var Tenancy */
protected $tenancy;
/** @var TenantResolver|HostResolver */
protected $resolver;
public function initializeTenant($request, $next, ...$resolverArguments)
{
if(! $this->tenancy->host) {
try {
$this->tenancy->initializeTenant(
$this->resolver->resolve(...$resolverArguments)
);
} catch (TenantCouldNotBeIdentifiedException $e) {
$onFail = static::$onFail ?? function ($e) {
throw $e;
};
return $onFail($e, $request, $next);
}
}
return $next($request);
}
public function initializeHost($request, $next, ...$resolverArguments)
{
try {
$this->tenancy->initializeHost(
$this->resolver->resolve(...$resolverArguments)
);
} catch (HostCouldNotBeIdentifiedException $e) {
$onFail = static::$onFail ?? function ($e) {
throw $e;
};
return $onFail($e, $request, $next);
}
return $next($request);
}
}