TenantAtlas/apps/platform/app/Services/TenantConfiguration/IdentityConflictDiagnosticsBuilder.php
Ahmed Darrazi 5ceecdeb62
Some checks failed
PR Fast Feedback / fast-feedback (pull_request) Failing after 6m40s
feat: implement canonical identity engine
2026-06-26 08:46:18 +02: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;
}
}