<commit_before><?php

/*
 * This file is part of Contao Manager.
 *
 * Copyright (c) 2016-2017 Contao Association
 *
 * @license LGPL-3.0+
 */

namespace Contao\ManagerApi\EventListener;

use Contao\ManagerApi\HttpKernel\ApiProblemResponse;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class ExceptionListener
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var bool
     */
    private $debug;

    /**
     * Constructor.
     *
     * @param LoggerInterface $logger
     * @param bool            $debug
     */
    public function __construct(LoggerInterface $logger, $debug = false)
    {
        $this->logger = $logger;
        $this->debug = $debug;
    }

    /**
     * Responds with application/problem+json on kernel.exception.
     *
     * @param GetResponseForExceptionEvent $event
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        if ('json' !== $event->getRequest()->getContentType()) {
            return;
        }

        $exception = $event->getException();

        $this->logException($exception);

        $event->setResponse(
            ApiProblemResponse::createFromException(
                $exception,
                $this->debug
            )
        );
    }

    /**
     * Logs the exception if a logger is available.
     *
     * @param \Exception $exception
     */
    private function logException(\Exception $exception)
    {
        if (null === $this->logger) {
            return;
        }<commit_msg>Check acceptable type not content type for api problem response<commit_after><?php

/*
 * This file is part of Contao Manager.
 *
 * Copyright (c) 2016-2017 Contao Association
 *
 * @license LGPL-3.0+
 */

namespace Contao\ManagerApi\EventListener;

use Contao\ManagerApi\HttpKernel\ApiProblemResponse;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class ExceptionListener
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var bool
     */
    private $debug;

    /**
     * Constructor.
     *
     * @param LoggerInterface $logger
     * @param bool            $debug
     */
    public function __construct(LoggerInterface $logger, $debug = false)
    {
        $this->logger = $logger;
        $this->debug = $debug;
    }

    /**
     * Responds with application/problem+json on kernel.exception.
     *
     * @param GetResponseForExceptionEvent $event
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        if (!in_array('application/json', $event->getRequest()->getAcceptableContentTypes(), true)) {
            return;
        }

        $exception = $event->getException();

        $this->logException($exception);

        $event->setResponse(
            ApiProblemResponse::createFromException(
                $exception,
                $this->debug
            )
        );
    }

    /**
     * Logs the exception if a logger is available.
     *
     * @param \Exception $exception
     */
    private function logException(\Exception $exception)
    {
        if (null === $this->logger) {
            return;
        }
{'fraction_non_alphanumeric': 0.11366991437850606, 'fraction_numerical': 0.00590493061706525, 'mean_word_length': 2.2235965746907707, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '2542875', 'n_tokens_mistral': 953, 'n_tokens_neox': 913, 'n_words': 198}