<commit_before><?php
/**
 * This file is part of the BEAR.Package package.
 *
 * @license http://opensource.org/licenses/MIT MIT
 */
namespace BEAR\Package\Provide\Error;

use BEAR\AppMeta\AbstractAppMeta;
use BEAR\Package\Provide\Error\ErrorPage as CliErrorPage;
use BEAR\Resource\Code;
use BEAR\Resource\Exception\BadRequestException as BadRequest;
use BEAR\Resource\Exception\ResourceNotFoundException as NotFound;
use BEAR\Sunday\Extension\Error\ErrorInterface;
use BEAR\Sunday\Extension\Router\RouterMatch as Request;
use BEAR\Sunday\Extension\Transfer\TransferInterface;
use BEAR\Sunday\Provide\Error\ErrorPage as SundayErrorPage;

/**
 * vnd.error for BEAR.Package
 *
 * @see https://github.com/blongden/vnd.error
 */
class VndErrorHandler implements ErrorInterface
{
    /**
     * @var int
     */
    private $code;

    /**
     * @var array
     */
    private $body = ['message' => '', 'logref' => ''];

    /**
     * @var string
     */
    private $logDir;

    /**
     * @var ErrorPage
     */
    private $errorPage;

    /**
     * @var TransferInterface
     */
    private $responder;

    /**
     * @var string
     */
    private $lastErrorFile;

    /**
     * @var ExceptionAsString
     */
    private $exceptionString;

    public function __construct(AbstractAppMeta $appMeta, TransferInterface $responder)
    {
        $this->logDir = $appMeta->logDir;
        $this->lastErrorFile = $this->logDir . '/last_error.log';
        $this->responder = $responder;
        $this->exceptionString = new ExceptionAsString;
    }

    /**
     * {@inheritdoc}
     */
    public function handle(\Exception $e, Request $request)
    {
        $isCodeError = ($e instanceof NotFound || $e instanceof BadRequest);
        $this->errorPage = $this->getErrorPage($e, $this->lastErrorFile);
        if ($isCodeError) {
            list($this->code, $this->body) = $this->codeError($e);
            $this->log($e, $request);

            return $this;
        }
        // 500 exception
        $this->code = 500;
        $logRef = $this->log($e, $request);
        $this->body = [
            'message' => '500 Server Error',
            'logref' => $logRef
        ];

        return $this;
    }

    /**
     * @param \Exception $e
     * @param string     $lastErrorFile
     *
     * @return CliErrorPage|SundayErrorPage
     */
    private function getErrorPage(\Exception $e, $lastErrorFile)
    {
        return PHP_SAPI === 'cli' ? new CliErrorPage($this->exceptionString->summery($e, $lastErrorFile)) : new SundayErrorPage;
    }

    /**
     * {@inheritdoc}
     */
    public function transfer()
    {
        $ro = $this->errorPage;
        $ro->code = $this->code;
        $ro->headers['content-type'] = 'application/vnd.error+json';<commit_msg>fix phpdoc type<commit_after><?php
/**
 * This file is part of the BEAR.Package package.
 *
 * @license http://opensource.org/licenses/MIT MIT
 */
namespace BEAR\Package\Provide\Error;

use BEAR\AppMeta\AbstractAppMeta;
use BEAR\Package\Provide\Error\ErrorPage as CliErrorPage;
use BEAR\Resource\Code;
use BEAR\Resource\Exception\BadRequestException as BadRequest;
use BEAR\Resource\Exception\ResourceNotFoundException as NotFound;
use BEAR\Resource\ResourceObject;
use BEAR\Sunday\Extension\Error\ErrorInterface;
use BEAR\Sunday\Extension\Router\RouterMatch as Request;
use BEAR\Sunday\Extension\Transfer\TransferInterface;
use BEAR\Sunday\Provide\Error\ErrorPage as SundayErrorPage;

/**
 * vnd.error for BEAR.Package
 *
 * @see https://github.com/blongden/vnd.error
 */
class VndErrorHandler implements ErrorInterface
{
    /**
     * @var int
     */
    private $code;

    /**
     * @var array
     */
    private $body = ['message' => '', 'logref' => ''];

    /**
     * @var string
     */
    private $logDir;

    /**
     * @var ResourceObject
     */
    private $errorPage;

    /**
     * @var TransferInterface
     */
    private $responder;

    /**
     * @var string
     */
    private $lastErrorFile;

    /**
     * @var ExceptionAsString
     */
    private $exceptionString;

    public function __construct(AbstractAppMeta $appMeta, TransferInterface $responder)
    {
        $this->logDir = $appMeta->logDir;
        $this->lastErrorFile = $this->logDir . '/last_error.log';
        $this->responder = $responder;
        $this->exceptionString = new ExceptionAsString;
    }

    /**
     * {@inheritdoc}
     */
    public function handle(\Exception $e, Request $request)
    {
        $isCodeError = ($e instanceof NotFound || $e instanceof BadRequest);
        $this->errorPage = $this->getErrorPage($e, $this->lastErrorFile);
        if ($isCodeError) {
            list($this->code, $this->body) = $this->codeError($e);
            $this->log($e, $request);

            return $this;
        }
        // 500 exception
        $this->code = 500;
        $logRef = $this->log($e, $request);
        $this->body = [
            'message' => '500 Server Error',
            'logref' => $logRef
        ];

        return $this;
    }

    /**
     * @param \Exception $e
     * @param string     $lastErrorFile
     *
     * @return ResourceObject
     */
    private function getErrorPage(\Exception $e, $lastErrorFile)
    {
        return PHP_SAPI === 'cli' ? new CliErrorPage($this->exceptionString->summery($e, $lastErrorFile)) : new SundayErrorPage;
    }

    /**
     * {@inheritdoc}
     */
    public function transfer()
    {
        $ro = $this->errorPage;
        $ro->code = $this->code;
        $ro->headers['content-type'] = 'application/vnd.error+json';
{'fraction_non_alphanumeric': 0.14375336081735077, 'fraction_numerical': 0.003226384656748521, 'mean_word_length': 2.4212139791538934, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '7643857', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1686, 'n_words': 343}