<commit_before><?php

namespace OpenConext\EngineBlockBundle\EventListener;

use OpenConext\EngineBlockBridge\ErrorReporter;
use OpenConext\EngineBlockBundle\Exception\AddExecutionTimePadding;
use OpenConext\EngineBlockBundle\Value\ExecutionTime;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ErrorResponseTimePaddingListener
{
    /**
     * @var ExecutionTimeTracker
     */
    private $executionTimeTracker;

    /**
     * @var UrlGeneratorInterface
     */
    private $urlGenerator;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var ErrorReporter
     */
    private $errorReporter;

    /**
     * @var ExecutionTime
     */
    private $minimumExecutionTime;

    public function __construct(
        ExecutionTimeTracker $executionTimeTracker,
        UrlGeneratorInterface $urlGenerator,
        LoggerInterface $logger,
        ErrorReporter $errorReporter,
        $minimumExecutionTimeWhenProcessingFails
    ) {
        $this->executionTimeTracker = $executionTimeTracker;
        $this->urlGenerator         = $urlGenerator;
        $this->logger               = $logger;
        $this->errorReporter        = $errorReporter;
        $this->minimumExecutionTime = $minimumExecutionTimeWhenProcessingFails;
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();

        if (!$exception instanceof AddExecutionTimePadding) {
            return;
        }

        if (!$this->executionTimeTracker->isTracking()) {
            return;
        }

        $this->logger->warning(
            sprintf('Handling exception: "%s": "%s"', get_class($exception), $exception->getMessage())
        );

        if ($this->executionTimeTracker->currentExecutionTimeExceeds($this->minimumExecutionTime)) {
            $this->logger->warning(sprintf(
                'Not padding response time: it exceeds the configured padded response time (%d seconds)',
                $this->minimumExecutionTime
            ));
        } else {
            $requiredPadding = $this->executionTimeTracker->timeRemainingUntil($this->minimumExecutionTime);

            $this->logger->info(sprintf(
                'Padding response time with %d milliseconds',<commit_msg>Correct log message and used parameter<commit_after><?php

namespace OpenConext\EngineBlockBundle\EventListener;

use OpenConext\EngineBlockBridge\ErrorReporter;
use OpenConext\EngineBlockBundle\Exception\AddExecutionTimePadding;
use OpenConext\EngineBlockBundle\Value\ExecutionTime;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ErrorResponseTimePaddingListener
{
    /**
     * @var ExecutionTimeTracker
     */
    private $executionTimeTracker;

    /**
     * @var UrlGeneratorInterface
     */
    private $urlGenerator;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var ErrorReporter
     */
    private $errorReporter;

    /**
     * @var ExecutionTime
     */
    private $minimumExecutionTime;

    public function __construct(
        ExecutionTimeTracker $executionTimeTracker,
        UrlGeneratorInterface $urlGenerator,
        LoggerInterface $logger,
        ErrorReporter $errorReporter,
        $minimumExecutionTimeWhenProcessingFails
    ) {
        $this->executionTimeTracker = $executionTimeTracker;
        $this->urlGenerator         = $urlGenerator;
        $this->logger               = $logger;
        $this->errorReporter        = $errorReporter;
        $this->minimumExecutionTime = $minimumExecutionTimeWhenProcessingFails;
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();

        if (!$exception instanceof AddExecutionTimePadding) {
            return;
        }

        if (!$this->executionTimeTracker->isTracking()) {
            return;
        }

        $this->logger->warning(
            sprintf('Handling exception: "%s": "%s"', get_class($exception), $exception->getMessage())
        );

        if ($this->executionTimeTracker->currentExecutionTimeExceeds($this->minimumExecutionTime)) {
            $this->logger->warning(sprintf(
                'Not padding response time: it exceeds the configured padded response time (%d milliseconds)',
                $this->minimumExecutionTime->getExecutionTime()
            ));
        } else {
            $requiredPadding = $this->executionTimeTracker->timeRemainingUntil($this->minimumExecutionTime);

            $this->logger->info(sprintf(
                'Padding response time with %d milliseconds',
{'fraction_non_alphanumeric': 0.10016155088852989, 'fraction_numerical': 0.0, 'mean_word_length': 2.872556684910086, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3144558', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1234, 'n_words': 222}