casts['pending_since'] = 'timestamp'; } /** * Determine if the model instance is in a pending state. * * @return bool */ public function pending() { return ! is_null($this->pending_since); } /** Create a pending tenant. */ public static function createPending($attributes = []): Tenant { $tenant = static::create($attributes); event(new CreatingPendingTenant($tenant)); // Update the pending_since value only after the tenant is created so it's // Not marked as pending until finishing running the migrations, seeders, etc. $tenant->update([ 'pending_since' => now()->timestamp, ]); event(new PendingTenantCreated($tenant)); return $tenant; } /** Pull a pending tenant. */ public static function pullPending(): Tenant { return static::pullPendingFromPool(true); } /** Try to pull a tenant from the pool of pending tenants. */ public static function pullPendingFromPool(bool $firstOrCreate = false): ?Tenant { if (! static::onlyPending()->exists()) { if (! $firstOrCreate) { return null; } static::createPending(); } // A pending tenant is surely available at this point $tenant = static::onlyPending()->first(); event(new PullingPendingTenant($tenant)); $tenant->update([ 'pending_since' => null, ]); event(new PendingTenantPulled($tenant)); return $tenant; } }