Files
michaelschiemer/src/Framework/CommandBus/Middleware/PerformanceMonitoringMiddleware.php
2025-11-24 21:28:25 +01:00

49 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\CommandBus\Middleware;
use App\Framework\CommandBus\Middleware;
use App\Framework\DI\Container;
use App\Framework\Performance\Contracts\PerformanceCollectorInterface;
use App\Framework\Performance\PerformanceCategory;
final readonly class PerformanceMonitoringMiddleware implements Middleware
{
public function __construct(
private Container $container
) {
}
public function handle(object $command, callable $next): mixed
{
// Performance Monitoring ist optional - nur ausführen wenn Collector verfügbar
if (!$this->container->has(PerformanceCollectorInterface::class)) {
return $next($command);
}
$collector = $this->container->get(PerformanceCollectorInterface::class);
// Prüfe ob Performance Tracking aktiviert ist
if (!$collector->isEnabled()) {
return $next($command);
}
$commandKey = 'command_' . basename(str_replace('\\', '/', $command::class));
$collector->startTiming($commandKey, PerformanceCategory::SYSTEM, [
'command_class' => $command::class,
]);
try {
$result = $next($command);
$collector->endTiming($commandKey);
return $result;
} catch (\Throwable $e) {
$collector->endTiming($commandKey);
throw $e;
}
}
}