<commit_before><?php

namespace Application\Listener;

use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class SendErrorToMailListener
{
    /**
     * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
     */
    public function onKernelException(GetResponseEvent $event)
    {
        $date = date('d/m/Y H:i:s');
        $messages = array();

        $server = array();
        $allow_parameter = array(
            'HTTP_HOST',
            'REQUEST_URI',
            'REQUEST_METHOD',
            'HTTP_REFERER',
            'HTTP_USER_AGENT',
            'HTTP_ACCEPT_LANGUAGE',
            'HTTP_COOKIE',
            'SERVER_SOFTWARE',
            'SERVER_NAME',
            'SERVER_ADDR',
            'REMOTE_ADDR',

            'DOCUMENT_ROOT',
            'CONTENT_TYPE',
            'CONTENT_LENGTH',
            'REQUEST_SCHEME',
            'REQUEST_TIME',
        );

        foreach ($allow_parameter as $p) {

            if (isset($_SERVER['REQUEST_SCHEME']) && isset($_SERVER['HTTP_HOST']) && isset($_SERVER['REQUEST_URI'])) {
                $server['URL'] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            }

            if (isset($_SERVER[$p])) {
                $server[$p] = $_SERVER[$p];
            }
        }

        $messages[] = $this->getArrayForatView(array('$_DATE' => $date));
        $messages[] = $this->getArrayForatView(array('$_ERROR MESSAGE' => $event->getException()->getMessage()));
        $messages[] = $this->getArrayForatView(array('$_SERVER' => $server));

        if (!empty($_GET)) {
            $messages[] = $this->getArrayForatView(array('$_GET' => $_GET));
        }

        if (!empty($_POST)) {
            $messages[] = $this->getArrayForatView(array('$_POST' => $_POST));
        }

        $content = implode("\n\n", $messages);

        $message = \Swift_Message::newInstance()
            ->setSubject('Eurotax error ' . $date)
            ->setFrom('<EMAIL>')
            ->setTo(array('<EMAIL>', '<EMAIL>'))
            ->setBody($content);

        /** @var $mailer \Swift_Mailer */
        $mailer = \AppKernel::getStaticContainer()->get('mailer');
        $mailer->send($message);
    }

    /**
     * @param $array
     * @param int $count
     * @return string
     */
    protected function getArrayForatView($array, $count = 0)
    {
        $messages = array();
        foreach ($array as $key => $value) {

            if (is_array($value)) {
                $messages[] = str_repeat(' ', $count) . $key . "\n" . $this->getArrayForatView($value, $count + 2);
            } else {
                $messages[] = str_repeat(' ', $count) . $key . ' => ' . $value;
            }

        }
        return implode("\n", $messages);
    }
}<commit_msg>Error reports to mail, create deny filter<commit_after><?php

namespace Application\Listener;

use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\HttpKernel\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class SendErrorToMailListener
{
    protected $_deny_filter = array(
        '\Symfony\Component\HttpKernel\Exception\NotFoundHttpException',
        '\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException',
    );

    /**
     * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
     */
    public function onKernelException(GetResponseEvent $event)
    {
        $date = date('d/m/Y H:i:s');
        $messages = array();

        $server = array();
        $allow_parameter = array(
            'HTTP_HOST',
            'REQUEST_URI',
            'REQUEST_METHOD',
            'HTTP_REFERER',
            'HTTP_USER_AGENT',
            'HTTP_ACCEPT_LANGUAGE',
            'HTTP_COOKIE',
            'SERVER_SOFTWARE',
            'SERVER_NAME',
            'SERVER_ADDR',
            'REMOTE_ADDR',

            'DOCUMENT_ROOT',
            'CONTENT_TYPE',
            'CONTENT_LENGTH',
            'REQUEST_SCHEME',
            'REQUEST_TIME',
        );

        foreach ($allow_parameter as $p) {

            if (isset($_SERVER['REQUEST_SCHEME']) && isset($_SERVER['HTTP_HOST']) && isset($_SERVER['REQUEST_URI'])) {
                $server['URL'] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            }

            if (isset($_SERVER[$p])) {
                $server[$p] = $_SERVER[$p];
            }
        }


        //DBALException
        //if( instanceof NotFoundHttpException)
        /** @var $exception \Exception */
        $exception = $event->getException();

        if ($this->filterExceptionInstanceOf($exception)) {
            return;
        }


        $messages[] = $this->getArrayForatView(array('$_DATE' => $date));
        $messages[] = $this->getArrayForatView(array('$_ERROR MESSAGE' => $exception->getMessage()));
        $messages[] = $this->getArrayForatView(array('$_SERVER' => $server));

        if (!empty($_GET)) {
            $messages[] = $this->getArrayForatView(array('$_GET' => $_GET));
        }

        if (!empty($_POST)) {
            $messages[] = $this->getArrayForatView(array('$_POST' => $_POST));
        }

        $content = implode("\n\n", $messages);

        $message = \Swift_Message::newInstance()
            ->setSubject('Eurotax error ' . $date)
            ->setFrom('<EMAIL>')
            ->setTo(array('<EMAIL>', '<EMAIL>'))
            ->setBody($content);

        /** @var $mailer \Swift_Mailer */
        $mailer = \AppKernel::getStaticContainer()->get('mailer');
        $mailer->send($message);
    }

    /**
     * @param \Exception $e
     * @return bool
     */
    function filterExceptionInstanceOf(\Exception $e)
    {
        foreach ($this->_deny_filter as $name) {
            if ($e instanceof $name) {
                return true;
            }
        }

        return false;
    }

    /**
     * @param $array
     * @param int $count
     * @return string
     */
    protected function getArrayForatView($array, $count = 0)
    {
        $messages = array();
        foreach ($array as $key => $value) {

            if (is_array($value)) {
                $messages[] = str_repeat(' ', $count) . $key . "\n" . $this->getArrayForatView($value, $count + 2);
            } else {
                $messages[] = str_repeat(' ', $count) . $key . ' => ' . $value;
            }

        }
        return implode("\n", $messages);
    }
}
{'fraction_non_alphanumeric': 0.16352958891733171, 'fraction_numerical': 0.0006023189278723084, 'mean_word_length': 2.0608294930875575, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 53, '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': '1081483', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1922, 'n_words': 317}