TenantAtlas/app/Services/Tenants/TenantOperabilityService.php
ahmido 641bb4afde feat: implement tenant lifecycle operability semantics (#172)
## Summary
- implement Spec 143 tenant lifecycle, operability, and tenant-context semantics across chooser, tenant management, onboarding, and canonical operation viewers
- add centralized tenant lifecycle and operability support types, audit action coverage, and lifecycle-aware badge and action handling
- add feature and unit coverage for tenant chooser eligibility, global search scoping, canonical operation access, onboarding authorization, and lifecycle presentation

## Testing
- vendor/bin/sail artisan test --compact
- vendor/bin/sail bin pint --dirty --format agent

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #172
2026-03-15 09:08:36 +00:00

77 lines
2.5 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 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);
}
}