<?php

namespace App\Framework\CommandBus\Middleware;

use App\Framework\CommandBus\Middleware;
use App\Framework\Performance\PerformanceCategory;
use App\Framework\Performance\PerformanceMeter;

final readonly class PerformanceMonitoringMiddleware implements Middleware
{
    public function __construct(
        private PerformanceMeter $meter
    ) {}

    public function handle(object $command, callable $next): mixed
    {
        $this->meter->startMeasure($command::class, PerformanceCategory::SYSTEM);

        $result = $next($command);

        $this->meter->endMeasure($command::class);

        return $result;
    }
}
