<commit_before><?php declare(strict_types = 1);

namespace ekinhbayar\GitAmp\Response;

use Amp\Artax\Response;
use ekinhbayar\GitAmp\Events\Factory as EventFactory;
use ekinhbayar\GitAmp\Events\UnknownEventException;
use ExceptionalJSON\DecodeErrorException;
use Psr\Log\LoggerInterface;

class Results
{
    private $eventFactory;

    private $logger;

    private $events = [];

    public function __construct(EventFactory $eventFactory, LoggerInterface $logger)
    {
        $this->eventFactory = $eventFactory;
        $this->logger       = $logger;
    }

    public function appendResponse(Response $response)
    {
        try {
            $events = json_try_decode($response->getBody(), true);
        } catch (DecodeErrorException $e) {
            $this->logger->emergency('Failed to decode response body as JSON', ['exception' => $e]);

            throw new DecodingFailedException('Failed to decode response body as JSON', $e->getCode(), $e);
        }

        foreach ($events as $event) {
            $this->appendEvent($event);
        }
    }

    private function appendEvent(array $event)
    {
        try {
            $this->events[] = $this->eventFactory->build($event);
        } catch (UnknownEventException $e) {
            $this->logger->debug('Unknown event encountered', ['exception' => $e]);
        }
    }

    public function hasEvents(): bool<commit_msg>Prevent filling logs with useless events<commit_after><?php declare(strict_types = 1);

namespace ekinhbayar\GitAmp\Response;

use Amp\Artax\Response;
use ekinhbayar\GitAmp\Events\Factory as EventFactory;
use ekinhbayar\GitAmp\Events\UnknownEventException;
use ExceptionalJSON\DecodeErrorException;
use Psr\Log\LoggerInterface;

class Results
{
    private $eventFactory;

    private $logger;

    private $events = [];

    public function __construct(EventFactory $eventFactory, LoggerInterface $logger)
    {
        $this->eventFactory = $eventFactory;
        $this->logger       = $logger;
    }

    public function appendResponse(Response $response)
    {
        try {
            $events = json_try_decode($response->getBody(), true);
        } catch (DecodeErrorException $e) {
            $this->logger->emergency('Failed to decode response body as JSON', ['exception' => $e]);

            throw new DecodingFailedException('Failed to decode response body as JSON', $e->getCode(), $e);
        }

        foreach ($events as $event) {
            $this->appendEvent($event);
        }
    }

    private function appendEvent(array $event)
    {
        try {
            $this->events[] = $this->eventFactory->build($event);
        } catch (UnknownEventException $e) {
            //$this->logger->debug('Unknown event encountered', ['exception' => $e]);
        }
    }

    public function hasEvents(): bool
{'fraction_non_alphanumeric': 0.12273855977296914, 'fraction_numerical': 0.000709471443774388, 'mean_word_length': 2.863013698630137, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '482383', 'n_tokens_mistral': 806, 'n_tokens_neox': 775, 'n_words': 184}