## Summary - introduce a shared reason-translation contract with envelopes, presenter helpers, fallback handling, and provider translation support - adopt translated operator-facing reason presentation across operation runs, notifications, provider guidance, tenant operability, and RBAC-related surfaces - add Spec 157 design artifacts and targeted regression coverage for translation quality, diagnostics retention, and authorization-safe guidance ## Validation - `vendor/bin/sail bin pint --dirty --format agent` - `vendor/bin/sail artisan test --compact tests/Architecture/ReasonTranslationPrimarySurfaceGuardTest.php tests/Unit/Support/ReasonTranslation/ReasonResolutionEnvelopeTest.php tests/Unit/Support/ReasonTranslation/ExecutionDenialReasonTranslationTest.php tests/Unit/Support/ReasonTranslation/TenantOperabilityReasonTranslationTest.php tests/Unit/Support/ReasonTranslation/RbacReasonTranslationTest.php tests/Unit/Support/ReasonTranslation/ProviderReasonTranslationTest.php tests/Feature/Notifications/OperationRunNotificationTest.php tests/Feature/Operations/OperationRunBlockedExecutionPresentationTest.php tests/Feature/Operations/TenantlessOperationRunViewerTest.php tests/Feature/ReasonTranslation/GovernanceReasonPresentationTest.php tests/Feature/Authorization/ReasonTranslationScopeSafetyTest.php tests/Feature/Monitoring/OperationRunBlockedSpec081Test.php tests/Feature/ProviderConnections/ProviderOperationBlockedGuidanceSpec081Test.php tests/Feature/ProviderConnections/ProviderGatewayRuntimeSmokeSpec081Test.php` ## Notes - Livewire v4.0+ compliance remains unchanged within the existing Filament v5 stack. - No new panel was added; provider registration remains in `bootstrap/providers.php`. - No new globally searchable resource was introduced. - No new destructive action family was introduced. - No new assets were added; the existing `filament:assets` deployment behavior remains unchanged. Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #187
165 lines
5.5 KiB
PHP
165 lines
5.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Support\ReasonTranslation;
|
|
|
|
use App\Models\OperationRun;
|
|
use App\Models\ProviderConnection;
|
|
use App\Models\Tenant;
|
|
use App\Support\Operations\ExecutionDenialReasonCode;
|
|
use App\Support\Providers\ProviderReasonCodes;
|
|
use App\Support\Providers\ProviderReasonTranslator;
|
|
use App\Support\RbacReason;
|
|
use App\Support\Tenants\TenantOperabilityReasonCode;
|
|
|
|
final class ReasonPresenter
|
|
{
|
|
public function __construct(
|
|
private readonly ReasonTranslator $reasonTranslator,
|
|
) {}
|
|
|
|
public function forOperationRun(OperationRun $run, string $surface = 'detail'): ?ReasonResolutionEnvelope
|
|
{
|
|
$context = is_array($run->context) ? $run->context : [];
|
|
$storedTranslation = is_array($context['reason_translation'] ?? null) ? $context['reason_translation'] : null;
|
|
|
|
if ($storedTranslation !== null) {
|
|
$storedEnvelope = ReasonResolutionEnvelope::fromArray($storedTranslation);
|
|
|
|
if ($storedEnvelope instanceof ReasonResolutionEnvelope) {
|
|
if ($storedEnvelope->nextSteps === [] && is_array($context['next_steps'] ?? null)) {
|
|
return $storedEnvelope->withNextSteps(NextStepOption::collect($context['next_steps']));
|
|
}
|
|
|
|
return $storedEnvelope;
|
|
}
|
|
}
|
|
|
|
$contextReasonCode = data_get($context, 'execution_legitimacy.reason_code')
|
|
?? data_get($context, 'reason_code');
|
|
|
|
if (is_string($contextReasonCode) && trim($contextReasonCode) !== '') {
|
|
return $this->translateOperationRunReason(trim($contextReasonCode), $surface, $context);
|
|
}
|
|
|
|
$failureReasonCode = data_get($run->failure_summary, '0.reason_code');
|
|
|
|
if (! is_string($failureReasonCode) || trim($failureReasonCode) === '') {
|
|
return null;
|
|
}
|
|
|
|
$failureReasonCode = trim($failureReasonCode);
|
|
|
|
if (! $this->isDirectlyTranslatableOperationReason($failureReasonCode)) {
|
|
return null;
|
|
}
|
|
|
|
$envelope = $this->translateOperationRunReason($failureReasonCode, $surface, $context);
|
|
|
|
if (! $envelope instanceof ReasonResolutionEnvelope) {
|
|
return null;
|
|
}
|
|
|
|
if ($envelope->nextSteps !== []) {
|
|
return $envelope;
|
|
}
|
|
|
|
$legacyNextSteps = is_array($context['next_steps'] ?? null) ? NextStepOption::collect($context['next_steps']) : [];
|
|
|
|
return $legacyNextSteps !== [] ? $envelope->withNextSteps($legacyNextSteps) : $envelope;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $context
|
|
*/
|
|
private function translateOperationRunReason(
|
|
string $reasonCode,
|
|
string $surface,
|
|
array $context,
|
|
): ?ReasonResolutionEnvelope {
|
|
return $this->reasonTranslator->translate($reasonCode, surface: $surface, context: $context);
|
|
}
|
|
|
|
private function isDirectlyTranslatableOperationReason(string $reasonCode): bool
|
|
{
|
|
if ($reasonCode === ProviderReasonCodes::UnknownError) {
|
|
return false;
|
|
}
|
|
|
|
return ProviderReasonCodes::isKnown($reasonCode)
|
|
|| ExecutionDenialReasonCode::tryFrom($reasonCode) instanceof ExecutionDenialReasonCode
|
|
|| TenantOperabilityReasonCode::tryFrom($reasonCode) instanceof TenantOperabilityReasonCode
|
|
|| RbacReason::tryFrom($reasonCode) instanceof RbacReason;
|
|
}
|
|
|
|
public function forProviderReason(
|
|
Tenant $tenant,
|
|
string $reasonCode,
|
|
?ProviderConnection $connection = null,
|
|
string $surface = 'detail',
|
|
): ?ReasonResolutionEnvelope {
|
|
return $this->reasonTranslator->translate(
|
|
reasonCode: $reasonCode,
|
|
artifactKey: ProviderReasonTranslator::ARTIFACT_KEY,
|
|
surface: $surface,
|
|
context: [
|
|
'tenant' => $tenant,
|
|
'connection' => $connection,
|
|
],
|
|
);
|
|
}
|
|
|
|
public function forTenantOperabilityReason(
|
|
TenantOperabilityReasonCode|string|null $reasonCode,
|
|
string $surface = 'detail',
|
|
): ?ReasonResolutionEnvelope {
|
|
$normalizedCode = $reasonCode instanceof TenantOperabilityReasonCode ? $reasonCode->value : $reasonCode;
|
|
|
|
return $this->reasonTranslator->translate(
|
|
reasonCode: $normalizedCode,
|
|
artifactKey: ReasonTranslator::TENANT_OPERABILITY_ARTIFACT,
|
|
surface: $surface,
|
|
);
|
|
}
|
|
|
|
public function forRbacReason(RbacReason|string|null $reasonCode, string $surface = 'detail'): ?ReasonResolutionEnvelope
|
|
{
|
|
$normalizedCode = $reasonCode instanceof RbacReason ? $reasonCode->value : $reasonCode;
|
|
|
|
return $this->reasonTranslator->translate(
|
|
reasonCode: $normalizedCode,
|
|
artifactKey: ReasonTranslator::RBAC_ARTIFACT,
|
|
surface: $surface,
|
|
);
|
|
}
|
|
|
|
public function diagnosticCode(?ReasonResolutionEnvelope $envelope): ?string
|
|
{
|
|
return $envelope?->diagnosticCode();
|
|
}
|
|
|
|
public function primaryLabel(?ReasonResolutionEnvelope $envelope): ?string
|
|
{
|
|
return $envelope?->operatorLabel;
|
|
}
|
|
|
|
public function shortExplanation(?ReasonResolutionEnvelope $envelope): ?string
|
|
{
|
|
return $envelope?->shortExplanation;
|
|
}
|
|
|
|
public function guidance(?ReasonResolutionEnvelope $envelope): ?string
|
|
{
|
|
return $envelope?->guidanceText();
|
|
}
|
|
|
|
/**
|
|
* @return array<int, string>
|
|
*/
|
|
public function bodyLines(?ReasonResolutionEnvelope $envelope, bool $includeGuidance = true): array
|
|
{
|
|
return $envelope?->toBodyLines($includeGuidance) ?? [];
|
|
}
|
|
}
|