fix(ErrorAggregation): use Environment class instead of $_ENV superglobal

- Replace all $_ENV references with Environment::get() calls
- Fixes ErrorAggregatorInterface binding in production where $_ENV is not populated
- Environment class properly loads from .env file which is mounted in containers
This commit is contained in:
2025-10-26 13:53:15 +01:00
parent fc3d7e6357
commit a90263d3be

View File

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