82 lines
1.9 KiB
PHP
82 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Runbooks;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
final readonly class FindingsLifecycleBackfillScope
|
|
{
|
|
public const string MODE_ALL_TENANTS = 'all_tenants';
|
|
|
|
public const string MODE_SINGLE_TENANT = 'single_tenant';
|
|
|
|
private function __construct(
|
|
public string $mode,
|
|
public ?int $tenantId,
|
|
) {}
|
|
|
|
public static function allTenants(): self
|
|
{
|
|
return new self(
|
|
mode: self::MODE_ALL_TENANTS,
|
|
tenantId: null,
|
|
);
|
|
}
|
|
|
|
public static function singleTenant(int $tenantId): self
|
|
{
|
|
$tenantId = (int) $tenantId;
|
|
|
|
if ($tenantId <= 0) {
|
|
throw ValidationException::withMessages([
|
|
'scope.tenant_id' => 'Select a valid tenant.',
|
|
]);
|
|
}
|
|
|
|
return new self(
|
|
mode: self::MODE_SINGLE_TENANT,
|
|
tenantId: $tenantId,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public static function fromArray(array $data): self
|
|
{
|
|
$mode = trim((string) ($data['mode'] ?? ''));
|
|
|
|
if ($mode === '' || $mode === self::MODE_ALL_TENANTS) {
|
|
return self::allTenants();
|
|
}
|
|
|
|
if ($mode !== self::MODE_SINGLE_TENANT) {
|
|
throw ValidationException::withMessages([
|
|
'scope.mode' => 'Select a valid scope mode.',
|
|
]);
|
|
}
|
|
|
|
$tenantId = $data['tenant_id'] ?? null;
|
|
|
|
if (! is_numeric($tenantId)) {
|
|
throw ValidationException::withMessages([
|
|
'scope.tenant_id' => 'Select a tenant.',
|
|
]);
|
|
}
|
|
|
|
return self::singleTenant((int) $tenantId);
|
|
}
|
|
|
|
public function isAllTenants(): bool
|
|
{
|
|
return $this->mode === self::MODE_ALL_TENANTS;
|
|
}
|
|
|
|
public function isSingleTenant(): bool
|
|
{
|
|
return $this->mode === self::MODE_SINGLE_TENANT;
|
|
}
|
|
}
|