diff --git a/src/Framework/ErrorAggregation/ErrorAggregationInitializer.php b/src/Framework/ErrorAggregation/ErrorAggregationInitializer.php index e0b4e03c..a14a02cf 100644 --- a/src/Framework/ErrorAggregation/ErrorAggregationInitializer.php +++ b/src/Framework/ErrorAggregation/ErrorAggregationInitializer.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Framework\ErrorAggregation; use App\Framework\Cache\Cache; +use App\Framework\Config\Environment; use App\Framework\Database\ConnectionInterface; use App\Framework\DateTime\Clock; use App\Framework\DI\Container; @@ -25,7 +26,8 @@ final readonly class ErrorAggregationInitializer #[Initializer] public function initialize(Container $container): void { - $enabled = (bool) ($_ENV['ERROR_AGGREGATION_ENABLED'] ?? true); + $env = $container->get(Environment::class); + $enabled = $env->getBool('ERROR_AGGREGATION_ENABLED', true); // Storage $container->bind(ErrorStorageInterface::class, function (Container $container) use ($enabled) { @@ -47,14 +49,15 @@ final readonly class ErrorAggregationInitializer return new NullErrorAggregator(); } + $env = $container->get(Environment::class); return new ErrorAggregator( storage: $container->get(ErrorStorageInterface::class), cache: $container->get(Cache::class), clock: $container->get(Clock::class), alertQueue: $container->get(Queue::class), logger: $container->get(Logger::class), - batchSize: (int) ($_ENV['ERROR_AGGREGATION_BATCH_SIZE'] ?? 100), - maxRetentionDays: (int) ($_ENV['ERROR_AGGREGATION_MAX_RETENTION_DAYS'] ?? 90) + batchSize: $env->getInt('ERROR_AGGREGATION_BATCH_SIZE', 100), + maxRetentionDays: $env->getInt('ERROR_AGGREGATION_MAX_RETENTION_DAYS', 90) ); }); @@ -64,29 +67,32 @@ final readonly class ErrorAggregationInitializer throw new \RuntimeException('ErrorAggregator is disabled. Use ErrorAggregatorInterface instead.'); } + $env = $container->get(Environment::class); return new ErrorAggregator( storage: $container->get(ErrorStorageInterface::class), cache: $container->get(Cache::class), clock: $container->get(Clock::class), alertQueue: $container->get(Queue::class), logger: $container->get(Logger::class), - batchSize: (int) ($_ENV['ERROR_AGGREGATION_BATCH_SIZE'] ?? 100), - maxRetentionDays: (int) ($_ENV['ERROR_AGGREGATION_MAX_RETENTION_DAYS'] ?? 90) + batchSize: $env->getInt('ERROR_AGGREGATION_BATCH_SIZE', 100), + maxRetentionDays: $env->getInt('ERROR_AGGREGATION_MAX_RETENTION_DAYS', 90) ); }); // Alert Manager $container->bind(AlertManager::class, function (Container $container) { + $env = $container->get(Environment::class); $channels = []; // Email channel (if configured) - if (! empty($_ENV['ALERT_EMAIL_RECIPIENTS'])) { - $recipients = explode(',', $_ENV['ALERT_EMAIL_RECIPIENTS']); + $emailRecipients = $env->get('ALERT_EMAIL_RECIPIENTS'); + if (! empty($emailRecipients)) { + $recipients = explode(',', $emailRecipients); $channels[] = new EmailAlertChannel( transport: $container->get(TransportInterface::class), recipients: array_map('trim', $recipients), - fromEmail: $_ENV['ALERT_FROM_EMAIL'] ?? 'alerts@example.com', - fromName: $_ENV['ALERT_FROM_NAME'] ?? 'Error Alert System' + fromEmail: $env->get('ALERT_FROM_EMAIL', 'alerts@example.com'), + fromName: $env->get('ALERT_FROM_NAME', 'Error Alert System') ); } @@ -97,19 +103,19 @@ final readonly class ErrorAggregationInitializer logger: $container->get(Logger::class), channels: $channels, throttleConfig: [ - 'urgent' => (int) ($_ENV['ALERT_THROTTLE_URGENT'] ?? 300), - 'high' => (int) ($_ENV['ALERT_THROTTLE_HIGH'] ?? 900), - 'medium' => (int) ($_ENV['ALERT_THROTTLE_MEDIUM'] ?? 3600), - 'low' => (int) ($_ENV['ALERT_THROTTLE_LOW'] ?? 86400), + 'urgent' => $env->getInt('ALERT_THROTTLE_URGENT', 300), + 'high' => $env->getInt('ALERT_THROTTLE_HIGH', 900), + 'medium' => $env->getInt('ALERT_THROTTLE_MEDIUM', 3600), + 'low' => $env->getInt('ALERT_THROTTLE_LOW', 86400), ] ); }); // Middleware - $container->bind(ErrorAggregationMiddleware::class, function (Container $container) { + $container->bind(ErrorAggregationMiddleware::class, function (Container $container) use ($enabled) { return new ErrorAggregationMiddleware( errorAggregator: $container->get(ErrorAggregator::class), - enabled: (bool) ($_ENV['ERROR_AGGREGATION_ENABLED'] ?? true) + enabled: $enabled ); }); }