<commit_before><?php
namespace Core\Framework\Error;

use Core\Framework\Core;
use Psr\Log\LoggerInterface;

/**
 * ErrorHandler.php
 *
 * @author Michael "Tekkla" Zorn <<EMAIL>>
 * @copyright 2016
 * @license MIT
 */
class ErrorHandler
{

    /**
     *
     * @var Core
     */
    private $core;

    /**
     *
     * @var \Throwable
     */
    private $throwable;

    /**
     *
     * @var string
     */
    private $result = '';

    /**
     *
     * @var array
     */
    private $log_to = [];

    /**
     *
     * @var bool
     */
    private $ajax = false;

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

    /**
     *
     * @var bool
     */
    private $public = false;

    /**
     *
     * @var int
     */
    private $http_response_code = 500;

    /**
     *
     * @var bool
     */
    private $fatal = false;

    /**
     *
     * @var int
     */
    private $level = 0;

    /**
     * Constructor
     *
     * @param Core $core
     */
    public function __construct(\Throwable $throwable)
    {
        $this->throwable = $throwable;
    }

    /**
     * Sets the error to handle
     *
     * @param \Throwable $throwable
     */
    public function setThrowable(\Throwable $throwable)
    {
        $this->throwable = $throwable;
    }

    /**
     *
     * @param LoggerInterface $logger
     */
    public function setLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     *
     * @param bool $ajax
     */
    public function setAjax(bool $ajax)
    {
        $this->ajax = $ajax;
    }

    /**
     *
     * @param bool $public
     */
    public function setPublic(bool $public)
    {
        $this->public = $public;
    }

    /**
     *
     * @param int $http_response_code
     */
    public function setHttpResponseCode(int $http_response_code)
    {
        $this->http_response_code = $http_response_code;
    }

    /**
     *
     * @param int $level
     */
    public function setLevel(int $level)
    {
        $this->level = $level;
    }

    /**
     *
     * @param bool $fatal
     */
    public function setFatal(bool $fatal)
    {
        $this->fatal = $fatal;
    }

    /**
     *
     * @param int $level
     */
    public function handle()
    {
        switch ($this->level) {

            case 1:
                $this->result = $this->high();

            default:
            case 0:
                $this->result = $this->low();
        }

        if (isset($this->logger)) {
            $this->logger->error($this->throwable->getMessage() . ' (File: ' . $this->throwable->getFile() . ':' . $this->throwable->getLine(). ')');
        }

        http_response_code($this->http_response_code);

        if ($this->fatal) {
            die($this->result);
        }
    }

    private function low()
    {
        if (!$this->ajax) {

            $html = '
            <html>
                <head>
                    <title>Error</title>
                    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
                    <style type="text/css">
                       * { margin: 0; padding: 0; }
                        body { background-color: #aaa; color: #eee; font-family: Sans-Serif; }
                        h1 { margin: 3px 0 7px; }
                        p, pre { margin-bottom: 7px; }
                        pre { padding: 5px; border: 1px solid #333; max-height: 400px; overflow-y: scroll; background-color: #fff; display: block; }
                    </style>
                </head>
                <body>' . $html . '</body>
            </html>';
        }

        return $html;
    }

    private function getHeadline(): string
    {

        return '<h1>Error occured (' . $this->throwable->getCode() . ')</h1>';
    }

    private function getMessage(): string
    {
        return '<p><strong>' . $this->throwable->getMessage() . '</strong></p>';
    }

    private function getFileinfo(): string
    {
        return '<p>in ' . $this->getFile() . ' (Line: ' . $this->throwable->getLine() . ')</p>';
    }

    private function getTrace(): string
    {
        return '<pre>' . $this->throwable->getTrace() . '</pre>';<commit_msg>WIP ErrorHandler<commit_after><?php
namespace Core\Framework\Error;

use Core\Framework\Core;
use Psr\Log\LoggerInterface;

/**
 * ErrorHandler.php
 *
 * @author Michael "Tekkla" Zorn <<EMAIL>>
 * @copyright 2016
 * @license MIT
 */
class ErrorHandler
{

    /**
     *
     * @var Core
     */
    private $core;

    /**
     *
     * @var \Throwable
     */
    private $throwable;

    /**
     *
     * @var string
     */
    private $result = '';

    /**
     *
     * @var array
     */
    private $log_to = [];

    /**
     *
     * @var bool
     */
    private $ajax = false;

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

    /**
     *
     * @var bool
     */
    private $public = false;

    /**
     *
     * @var int
     */
    private $http_response_code = 500;

    /**
     *
     * @var bool
     */
    private $fatal = false;

    /**
     *
     * @var int
     */
    private $level = 0;

    /**
     * Constructor
     *
     * @param Core $core
     */
    public function __construct(\Throwable $throwable)
    {
        $this->throwable = $throwable;
    }

    /**
     * Sets the error to handle
     *
     * @param \Throwable $throwable
     */
    public function setThrowable(\Throwable $throwable)
    {
        $this->throwable = $throwable;
    }

    /**
     *
     * @param LoggerInterface $logger
     */
    public function setLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     *
     * @param bool $ajax
     */
    public function setAjax(bool $ajax)
    {
        $this->ajax = $ajax;
    }

    /**
     *
     * @param bool $public
     */
    public function setPublic(bool $public)
    {
        $this->public = $public;
    }

    /**
     *
     * @param int $http_response_code
     */
    public function setHttpResponseCode(int $http_response_code)
    {
        $this->http_response_code = $http_response_code;
    }

    /**
     *
     * @param int $level
     */
    public function setLevel(int $level)
    {
        $this->level = $level;
    }

    /**
     *
     * @param bool $fatal
     */
    public function setFatal(bool $fatal)
    {
        $this->fatal = $fatal;
    }

    /**
     *
     * @param int $level
     */
    public function handle()
    {
        switch ($this->level) {

            case 1:
                $this->result = $this->high();

            default:
            case 0:
                $this->result = $this->low();
        }

        if (isset($this->logger)) {
            $this->logger->error($this->throwable->getMessage() . ' (File: ' . $this->throwable->getFile() . ':' . $this->throwable->getLine(). ')');
        }

        http_response_code($this->http_response_code);

        if ($this->fatal) {
            die($this->result);
        }
    }

    private function low()
    {
        if (!$this->ajax) {

            $html = '
            <html>
                <head>
                    <title>Error</title>
                    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
                    <style type="text/css">
                       * { margin: 0; padding: 0; }
                        body { background-color: #aaa; color: #eee; font-family: Sans-Serif; }
                        h1 { margin: 3px 0 7px; }
                        p, pre { margin-bottom: 7px; }
                        pre { padding: 5px; border: 1px solid #333; max-height: 400px; overflow-y: scroll; background-color: #fff; display: block; }
                    </style>
                </head>
                <body>' . $this->createErrorHtml(false) . '</body>
            </html>';
        }

        return $html;
    }

    private function getHeadline(): string
    {

        return '<h1>Error occured (' . $this->throwable->getCode() . ')</h1>';
    }

    private function getMessage(): string
    {
        return '<p><strong>' . $this->throwable->getMessage() . '</strong></p>';
    }

    private function getFileinfo(): string
    {
        return '<p>in ' . $this->getFile() . ' (Line: ' . $this->throwable->getLine() . ')</p>';
    }

    private function getTrace(): string
    {
        return '<pre>' . $this->throwable->getTrace() . '</pre>';
{'fraction_non_alphanumeric': 0.15617305663032008, 'fraction_numerical': 0.007034822370735139, 'mean_word_length': 1.4546762589928057, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 120, 'https://': 2, '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': '2121531', 'n_tokens_mistral': 2745, 'n_tokens_neox': 2592, 'n_words': 522}