|
6 | 6 |
|
7 | 7 | use Carbon\Carbon;
|
8 | 8 | use Illuminate\Database\Eloquent\Model;
|
| 9 | +use Illuminate\Support\Facades\DB; |
9 | 10 | use Stancl\Tenancy\Contracts\Tenant;
|
10 | 11 | use Stancl\Tenancy\Events\CreatingPendingTenant;
|
11 | 12 | use Stancl\Tenancy\Events\PendingTenantCreated;
|
12 | 13 | use Stancl\Tenancy\Events\PendingTenantPulled;
|
13 | 14 | use Stancl\Tenancy\Events\PullingPendingTenant;
|
14 | 15 |
|
15 |
| -// todo consider adding a method that sets pending_since to null — to flag tenants as not-pending |
16 |
| - |
17 | 16 | /**
|
18 | 17 | * @property ?Carbon $pending_since
|
19 | 18 | *
|
@@ -50,46 +49,62 @@ public function pending(): bool
|
50 | 49 | */
|
51 | 50 | public static function createPending(array $attributes = []): Model&Tenant
|
52 | 51 | {
|
53 |
| - $tenant = static::create($attributes); |
54 |
| - |
55 |
| - event(new CreatingPendingTenant($tenant)); |
56 |
| - |
57 |
| - // Update the pending_since value only after the tenant is created so it's |
58 |
| - // Not marked as pending until finishing running the migrations, seeders, etc. |
59 |
| - $tenant->update([ |
60 |
| - 'pending_since' => now()->timestamp, |
61 |
| - ]); |
| 52 | + try { |
| 53 | + $tenant = static::create($attributes); |
| 54 | + event(new CreatingPendingTenant($tenant)); |
| 55 | + } finally { |
| 56 | + // Update the pending_since value only after the tenant is created so it's |
| 57 | + // not marked as pending until after migrations, seeders, etc are run. |
| 58 | + $tenant->update([ |
| 59 | + 'pending_since' => now()->timestamp, |
| 60 | + ]); |
| 61 | + } |
62 | 62 |
|
63 | 63 | event(new PendingTenantCreated($tenant));
|
64 | 64 |
|
65 | 65 | return $tenant;
|
66 | 66 | }
|
67 | 67 |
|
68 |
| - /** Pull a pending tenant. */ |
69 |
| - public static function pullPending(): Model&Tenant |
| 68 | + /** |
| 69 | + * Pull a pending tenant from the pool or create a new one if the pool is empty. |
| 70 | + * |
| 71 | + * @param array $attributes The attributes to set on the tenant. |
| 72 | + */ |
| 73 | + public static function pullPending(array $attributes = []): Model&Tenant |
70 | 74 | {
|
71 | 75 | /** @var Model&Tenant $pendingTenant */
|
72 |
| - $pendingTenant = static::pullPendingFromPool(true); |
| 76 | + $pendingTenant = static::pullPendingFromPool(true, $attributes); |
73 | 77 |
|
74 | 78 | return $pendingTenant;
|
75 | 79 | }
|
76 | 80 |
|
77 |
| - /** Try to pull a tenant from the pool of pending tenants. */ |
78 |
| - public static function pullPendingFromPool(bool $firstOrCreate = true, array $attributes = []): ?Tenant |
| 81 | + /** |
| 82 | + * Try to pull a tenant from the pool of pending tenants. |
| 83 | + * |
| 84 | + * @param bool $firstOrCreate If true, a tenant will be *created* if the pool is empty. Otherwise null is returned. |
| 85 | + * @param array $attributes The attributes to set on the tenant. |
| 86 | + */ |
| 87 | + public static function pullPendingFromPool(bool $firstOrCreate = false, array $attributes = []): ?Tenant |
79 | 88 | {
|
80 |
| - /** @var (Model&Tenant)|null $tenant */ |
81 |
| - $tenant = static::onlyPending()->first(); |
| 89 | + $tenant = DB::transaction(function () use ($attributes): ?Tenant { |
| 90 | + /** @var (Model&Tenant)|null $tenant */ |
| 91 | + $tenant = static::onlyPending()->first(); |
| 92 | + |
| 93 | + if ($tenant !== null) { |
| 94 | + event(new PullingPendingTenant($tenant)); |
| 95 | + $tenant->update(array_merge($attributes, [ |
| 96 | + 'pending_since' => null, |
| 97 | + ])); |
| 98 | + } |
| 99 | + |
| 100 | + return $tenant; |
| 101 | + }); |
82 | 102 |
|
83 | 103 | if ($tenant === null) {
|
84 | 104 | return $firstOrCreate ? static::create($attributes) : null;
|
85 | 105 | }
|
86 | 106 |
|
87 |
| - event(new PullingPendingTenant($tenant)); |
88 |
| - |
89 |
| - $tenant->update(array_merge($attributes, [ |
90 |
| - 'pending_since' => null, |
91 |
| - ])); |
92 |
| - |
| 107 | + // Only triggered if a tenant that was pulled from the pool is returned |
93 | 108 | event(new PendingTenantPulled($tenant));
|
94 | 109 |
|
95 | 110 | return $tenant;
|
|
0 commit comments