<commit_before><?php

/**
 * This file is part of ReactGuzzle.
 *
 ** (c) 2014 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace WyriHaximus\React\Guzzle\HttpClient;

use GuzzleHttp\Adapter\TransactionInterface;
use GuzzleHttp\Message\MessageFactory;
use React\HttpClient\Client as ReactHttpClient;
use React\HttpClient\Request as HttpRequest;
use React\HttpClient\Response as HttpResponse;
use React\Promise\Deferred;

/**
 * Class Request
 *
 * @package WyriHaximus\React\Guzzle\HttpClient
 */
class Request
{
    /**
     * @var HttpClient
     */
    protected $httpClient;
    
    /**
     * @var HttpResponse
     */
    protected $httpResponse;

    /**
     * @var string
     */
    protected $buffer = '';

    /**
     * @param HttpClient $httpClient
     */
    public function __construct(ReactHttpClient $httpClient) {
        $this->httpClient = $httpClient;
        $this->messageFactory = new MessageFactory();
    }

    /**
     * @param TransactionInterface $transaction
     *
     * @return \React\Promise\Promise
     */
    public function send(TransactionInterface $transaction) {
        $this->deferred = new Deferred();
        $request = $this->setupRequest($transaction);
        $this->setupListeners($request);
        $request->end();
        return $this->deferred->promise();
    }

    /**
     * @param TransactionInterface $transaction
     *
     * @return mixed
     */
    protected function setupRequest(TransactionInterface $transaction)
    {
        $request = $transaction->getRequest();
        $headers = [];
        foreach ($request->getHeaders() as $key => $values) {
            $headers[$key] = $request->getHeader($key);
        }
        return $this->httpClient->request($request->getMethod(), $request->getUrl(), $headers);
    }

    /**
     * @param HttpRequest $request
     * @param Deferred $deferred
     */
    protected function setupListeners(HttpRequest $request)
    {
        $request->on(
            'response',
            function (HttpResponse $response) {
                $this->onResponse($response);
            }
        );
        $request->on(
            'error',
            function ($error) {
                $this->onError($error);
            }
        );
        $request->on(
            'end',
            function () {
                $this->onEnd();
            }
        );
    }

    protected function onResponse(HttpResponse $response) {
        $response->on(
            'data',
            function ($data) {
                $this->onData($data);
            }
        );

        $this->deferred->progress([
            'event' => 'response',
            'response' => $response,
        ]);

        $this->httpResponse = $response;
    }

    protected function onData($data) {
        $this->buffer .= $data;

        $this->deferred->progress([
            'event' => 'data',
            'data' => $data,
        ]);
    }

    protected function onError($error) {
        $this->error = $error;
    }

    protected function onEnd() {<commit_msg>Getting the request flags from the transaction<commit_after><?php

/**
 * This file is part of ReactGuzzle.
 *
 ** (c) 2014 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace WyriHaximus\React\Guzzle\HttpClient;

use GuzzleHttp\Adapter\TransactionInterface;
use GuzzleHttp\Message\MessageFactory;
use React\HttpClient\Client as ReactHttpClient;
use React\HttpClient\Request as HttpRequest;
use React\HttpClient\Response as HttpResponse;
use React\Promise\Deferred;

/**
 * Class Request
 *
 * @package WyriHaximus\React\Guzzle\HttpClient
 */
class Request
{
    /**
     * @var HttpClient
     */
    protected $httpClient;
    
    /**
     * @var HttpResponse
     */
    protected $httpResponse;

    /**
     * @var string
     */
    protected $buffer = '';

    /**
     * @param HttpClient $httpClient
     */
    public function __construct(ReactHttpClient $httpClient) {
        $this->httpClient = $httpClient;
        $this->messageFactory = new MessageFactory();
    }

    /**
     * @param TransactionInterface $transaction
     *
     * @return \React\Promise\Promise
     */
    public function send(TransactionInterface $transaction) {
        $this->deferred = new Deferred();
        $request = $this->setupRequest($transaction);
        $this->setupListeners($request, $transaction);
        $request->end();
        return $this->deferred->promise();
    }

    /**
     * @param TransactionInterface $transaction
     *
     * @return mixed
     */
    protected function setupRequest(TransactionInterface $transaction)
    {
        $request = $transaction->getRequest();
        $headers = [];
        foreach ($request->getHeaders() as $key => $values) {
            $headers[$key] = $request->getHeader($key);
        }
        return $this->httpClient->request($request->getMethod(), $request->getUrl(), $headers);
    }

    /**
     * @param HttpRequest $request
     * @param Deferred $deferred
     */
    protected function setupListeners(HttpRequest $request, TransactionInterface $transaction)
    {
        $request->on(
            'response',
            function (HttpResponse $response) use ($transaction) {
                $this->onResponse($response, $transaction);
            }
        );
        $request->on(
            'error',
            function ($error) {
                $this->onError($error);
            }
        );
        $request->on(
            'end',
            function () {
                $this->onEnd();
            }
        );
    }

    protected function onResponse(HttpResponse $response, TransactionInterface $transaction) {
        $response->on(
            'data',
            function ($data) use ($transaction) {
                $this->onData($data, $transaction);
            }
        );

        $this->deferred->progress([
            'event' => 'response',
            'response' => $response,
        ]);

        $this->httpResponse = $response;
    }

    protected function onData($data, TransactionInterface $transaction) {
        if (!$transaction->getRequest()->getConfig()['stream']) {
            $this->buffer .= $data;
        }

        $this->deferred->progress([
            'event' => 'data',
            'data' => $data,
        ]);
    }

    protected function onError($error) {
        $this->error = $error;
    }

    protected function onEnd() {
{'fraction_non_alphanumeric': 0.13476680799515445, 'fraction_numerical': 0.0012113870381586917, 'mean_word_length': 2.055041628122109, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 77, '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': '2730539', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1762, 'n_words': 379}