92 lines
2.9 KiB
PHP
92 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Tenants;
|
|
|
|
use App\Models\Tenant;
|
|
use App\Support\Tenants\TenantLifecycle;
|
|
use App\Support\Tenants\TenantOperabilityDecision;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class TenantOperabilityService
|
|
{
|
|
public function decisionFor(Tenant $tenant): TenantOperabilityDecision
|
|
{
|
|
$lifecycle = TenantLifecycle::fromTenant($tenant);
|
|
$isArchived = $tenant->trashed() || $lifecycle === TenantLifecycle::Archived;
|
|
|
|
return new TenantOperabilityDecision(
|
|
lifecycle: $lifecycle,
|
|
canViewTenantSurface: $lifecycle->canViewTenantSurface(),
|
|
canSelectAsContext: ! $tenant->trashed() && $lifecycle->canSelectAsContext(),
|
|
canOperate: ! $tenant->trashed() && $lifecycle->canOperate(),
|
|
canArchive: ! $isArchived && $lifecycle->canArchive(),
|
|
canRestore: $isArchived || $lifecycle->canRestore(),
|
|
canResumeOnboarding: ! $tenant->trashed() && $lifecycle->canResumeOnboarding(),
|
|
canReferenceInWorkspaceMonitoring: $lifecycle->canReferenceInWorkspaceMonitoring(),
|
|
);
|
|
}
|
|
|
|
public function lifecycleFor(Tenant $tenant): TenantLifecycle
|
|
{
|
|
return $this->decisionFor($tenant)->lifecycle;
|
|
}
|
|
|
|
public function canSelectAsContext(Tenant $tenant): bool
|
|
{
|
|
return $this->decisionFor($tenant)->canSelectAsContext;
|
|
}
|
|
|
|
public function canViewTenantSurface(Tenant $tenant): bool
|
|
{
|
|
return $this->decisionFor($tenant)->canViewTenantSurface;
|
|
}
|
|
|
|
public function canResumeOnboarding(Tenant $tenant): bool
|
|
{
|
|
return $this->decisionFor($tenant)->canResumeOnboarding;
|
|
}
|
|
|
|
public function canArchive(Tenant $tenant): bool
|
|
{
|
|
return $this->decisionFor($tenant)->canArchive;
|
|
}
|
|
|
|
public function canRestore(Tenant $tenant): bool
|
|
{
|
|
return $this->decisionFor($tenant)->canRestore;
|
|
}
|
|
|
|
public function primaryManagementActionKey(Tenant $tenant, bool $preferOnboarding = false): ?string
|
|
{
|
|
return $this->decisionFor($tenant)->primaryManagementActionKey($preferOnboarding);
|
|
}
|
|
|
|
public function canReferenceInWorkspaceMonitoring(Tenant $tenant): bool
|
|
{
|
|
return $this->decisionFor($tenant)->canReferenceInWorkspaceMonitoring;
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, Tenant> $tenants
|
|
* @return Collection<int, Tenant>
|
|
*/
|
|
public function filterSelectable(Collection $tenants): Collection
|
|
{
|
|
return $tenants
|
|
->filter(fn (mixed $tenant): bool => $tenant instanceof Tenant && $this->canSelectAsContext($tenant))
|
|
->values();
|
|
}
|
|
|
|
public function applySelectableScope(Builder $query, ?string $table = null): Builder
|
|
{
|
|
$prefix = $table !== null && $table !== '' ? "{$table}." : '';
|
|
|
|
return $query
|
|
->whereNull("{$prefix}deleted_at")
|
|
->where("{$prefix}status", TenantLifecycle::Active->value);
|
|
}
|
|
}
|