TenantAtlas/apps/platform/app/Services/TenantConfiguration/IdentityConflictDiagnosticsBuilder.php
ahmido 8cbf1f7fe3 feat: implement canonical identity engine (#484)
Automated PR provided by Codex via Gitea API.

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #484
2026-06-26 06:50:25 +00:00

67 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\TenantConfiguration;
use App\Support\TenantConfiguration\CanonicalKeyKind;
use App\Support\TenantConfiguration\IdentityState;
final class IdentityConflictDiagnosticsBuilder
{
public function __construct(
private readonly CoveragePayloadRedactor $redactor,
) {}
/**
* @param list<string> $missingFields
* @param array<string, mixed> $metadata
* @return array<string, mixed>
*/
public function build(
string $reasonCode,
IdentityState $identityState,
CanonicalKeyKind $keyKind,
array $missingFields = [],
array $metadata = [],
): array {
$diagnostics = [
'reason_code' => $reasonCode,
'identity_state' => $identityState->value,
'key_kind' => $keyKind->value,
];
if ($missingFields !== []) {
$diagnostics['missing_fields'] = array_values(array_unique($missingFields));
}
foreach ($metadata as $key => $value) {
$key = (string) $key;
$redacted = $this->redactor->redact([$key => $value]);
$diagnostics[$key] = $this->bounded(is_array($redacted) ? ($redacted[$key] ?? null) : $redacted);
}
return $diagnostics;
}
private function bounded(mixed $value): mixed
{
if (is_string($value)) {
return mb_substr($value, 0, 240);
}
if (is_array($value)) {
return collect($value)
->take(16)
->map(fn (mixed $nested): mixed => $this->bounded($nested))
->all();
}
if (is_scalar($value) || $value === null) {
return $value;
}
return (string) $value;
}
}