<commit_before><?php

declare(strict_types=1);

namespace ErrorHeroModule\Handler;

use ErrorException;
use ErrorHeroModule\HeroConstant;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Throwable;
use Zend\Console\Request as ConsoleRequest;
use Zend\Http\PhpEnvironment\Request as HttpRequest;
use Zend\Log\Logger;
use Zend\Log\Writer\Db;
use Zend\Mail\Message;
use Zend\Mail\Transport\TransportInterface;
use Zend\Psr7Bridge\Psr7ServerRequest;
use Zend\Stdlib\RequestInterface;

class Logging
{
    /**
     * @var Logger
     */
    private $logger;

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

    /**
     * @var RequestInterface|null
     */
    private $request;

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

    /**
     * @var array
     */
    private $configLoggingSettings;

    /**
     * @var array
     */
    private $logWritersConfig;

    /**
     * @var Message|null
     */
    private $mailMessageService;

    /**
     * @var TransportInterface|null
     */
    private $mailMessageTransport;

    /**
     * @var array
     */
    private $emailReceivers;

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

    public function __construct(
        Logger             $logger,
        string             $serverUrl,
        RequestInterface   $request = null,
        string             $requestUri = '',
        array              $errorHeroModuleLocalConfig,
        array              $logWritersConfig,
        Message            $mailMessageService = null,
        TransportInterface $mailMessageTransport = null
    ) {
        $this->logger                = $logger;
        $this->serverUrl             = $serverUrl;
        $this->request               = $request;
        $this->requestUri            = $requestUri;
        $this->configLoggingSettings = $errorHeroModuleLocalConfig['logging-settings'];
        $this->logWritersConfig      = $logWritersConfig;
        $this->mailMessageService    = $mailMessageService;
        $this->mailMessageTransport  = $mailMessageTransport;
        $this->emailReceivers        = $errorHeroModuleLocalConfig['email-notification-settings']['email-to-send'];
        $this->emailSender           = $errorHeroModuleLocalConfig['email-notification-settings']['email-from'];
    }

    /**
     * Set Server Request for expressive
     */
    public function setServerRequestandRequestUri(ServerRequestInterface $request) : void
    {
        $this->request    = Psr7ServerRequest::toZend($request);
        $uri              = $this->request->getUri();
        $this->serverUrl  = $uri->getScheme() . '://' . $uri->getHost();
        $this->requestUri = $this->request->getRequestUri();
    }

    /**
     * @throws RuntimeException when cannot connect to DB in the first place
     */
    private function isExists(string $errorFile, int $errorLine, string $errorMessage, string $url, string $errorType) : bool
    {
        $writers = $this->logger->getWriters()->toArray();
        foreach ($writers as $writer) {
            if ($writer instanceof Db) {
                try {
                    $handlerWriterDb = new Writer\Checker\Db($writer, $this->configLoggingSettings, $this->logWritersConfig);
                    if ($handlerWriterDb->isExists($errorFile, $errorLine, $errorMessage, $url, $errorType)) {
                        return true;
                    }
                    break;
                } catch (RuntimeException $e) {
                    // use \Zend\Db\Adapter\Exception\RuntimeException but do here
                    // to avoid too much deep trace from Zend\Db classes
                    $exceptionClass = \get_class($e);<commit_msg>use local $request variable when possible<commit_after><?php

declare(strict_types=1);

namespace ErrorHeroModule\Handler;

use ErrorException;
use ErrorHeroModule\HeroConstant;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Throwable;
use Zend\Console\Request as ConsoleRequest;
use Zend\Http\PhpEnvironment\Request as HttpRequest;
use Zend\Log\Logger;
use Zend\Log\Writer\Db;
use Zend\Mail\Message;
use Zend\Mail\Transport\TransportInterface;
use Zend\Psr7Bridge\Psr7ServerRequest;
use Zend\Stdlib\RequestInterface;

class Logging
{
    /**
     * @var Logger
     */
    private $logger;

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

    /**
     * @var RequestInterface|null
     */
    private $request;

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

    /**
     * @var array
     */
    private $configLoggingSettings;

    /**
     * @var array
     */
    private $logWritersConfig;

    /**
     * @var Message|null
     */
    private $mailMessageService;

    /**
     * @var TransportInterface|null
     */
    private $mailMessageTransport;

    /**
     * @var array
     */
    private $emailReceivers;

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

    public function __construct(
        Logger             $logger,
        string             $serverUrl,
        RequestInterface   $request = null,
        string             $requestUri = '',
        array              $errorHeroModuleLocalConfig,
        array              $logWritersConfig,
        Message            $mailMessageService = null,
        TransportInterface $mailMessageTransport = null
    ) {
        $this->logger                = $logger;
        $this->serverUrl             = $serverUrl;
        $this->request               = $request;
        $this->requestUri            = $requestUri;
        $this->configLoggingSettings = $errorHeroModuleLocalConfig['logging-settings'];
        $this->logWritersConfig      = $logWritersConfig;
        $this->mailMessageService    = $mailMessageService;
        $this->mailMessageTransport  = $mailMessageTransport;
        $this->emailReceivers        = $errorHeroModuleLocalConfig['email-notification-settings']['email-to-send'];
        $this->emailSender           = $errorHeroModuleLocalConfig['email-notification-settings']['email-from'];
    }

    /**
     * Set Server Request for expressive
     */
    public function setServerRequestandRequestUri(ServerRequestInterface $request) : void
    {
        $this->request    = $request = Psr7ServerRequest::toZend($request);
        $uri              = $request->getUri();
        $this->serverUrl  = $uri->getScheme() . '://' . $uri->getHost();
        $this->requestUri = $request->getRequestUri();
    }

    /**
     * @throws RuntimeException when cannot connect to DB in the first place
     */
    private function isExists(string $errorFile, int $errorLine, string $errorMessage, string $url, string $errorType) : bool
    {
        $writers = $this->logger->getWriters()->toArray();
        foreach ($writers as $writer) {
            if ($writer instanceof Db) {
                try {
                    $handlerWriterDb = new Writer\Checker\Db($writer, $this->configLoggingSettings, $this->logWritersConfig);
                    if ($handlerWriterDb->isExists($errorFile, $errorLine, $errorMessage, $url, $errorType)) {
                        return true;
                    }
                    break;
                } catch (RuntimeException $e) {
                    // use \Zend\Db\Adapter\Exception\RuntimeException but do here
                    // to avoid too much deep trace from Zend\Db classes
                    $exceptionClass = \get_class($e);
{'fraction_non_alphanumeric': 0.11319994628709547, 'fraction_numerical': 0.001074258090506244, 'mean_word_length': 2.077685950413223, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, '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': '82741', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1967, 'n_words': 417}