<commit_before><?php

namespace ProxyFactory;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel;
use Symfony\Component\Routing;
use Symfony\Component\Routing\RouteCollection;
use Zend\Diactoros\Uri;

class ProxyFactory {

    /**
     * @var RouteCollection additional routes this proxy handles
     */
    protected $routes;

    /**
     * @var Routing\RequestContext the request context of this proxy request execution
     */
    protected $context;

    /**
     * @var HttpKernel\Controller\ControllerResolverInterface the resolver used by this proxy
     */
    protected $resolver;

    /**
     * @var Request
     */
    protected $symfonyRequest;

    /**
     * @var ServerRequestInterface
     */
    protected $request;

    /**
     * @var HttpFoundationFactory bridge used to convert PSR-7 requests/responses to Symfony HTTP foundation equivalents
     */
    protected $bridge;

    /**
     * @var DiactorosFactory factory to convert Symfony HTTP foundation requests to PSR-7 requests
     */
    protected $psr7Factory;

    /**
     * @var Uri the base URI of the proxied end-point
     */
    protected $proxyUri;

    /**
     * @param string $uri
     * @param array $arguments
     * @throws RuntimeException
     */
    function __construct($uri = "http://www.mocky.io/v2/", $arguments = array())
    {
        $this->proxyUri = new Uri($uri);

        if (!isset($arguments['routes'])) {
            $this->routes = new RouteCollection();
        } else {
            if (!$arguments['routes'] instanceof RouteCollection) {
                throw new RuntimeException("Invalid router");
            }
            $this->routes = $arguments['routes'];
        }

        $this->context = new Routing\RequestContext();
        $this->matcher = new Routing\Matcher\UrlMatcher($this->routes, $this->context);
        $this->resolver = new HttpKernel\Controller\ControllerResolver();

        $this->bridge = new HttpFoundationFactory();
        $this->psr7Factory = new DiactorosFactory();

        return $this;
    }

    /**
     * @param RequestInterface|ServerRequestInterface $request
     * @throws RuntimeException
     */
    function handleRequest(ServerRequestInterface $request = null) {
        if ($request == null) {
            $this->symfonyRequest = Request::createFromGlobals();
            $this->request = $this->psr7Factory->createRequest($this->symfonyRequest);
        } else {
            $this->request = $request;
            $this->symfonyRequest = $this->bridge->createRequest($this->request);
        }

        $this->context->fromRequest($this->symfonyRequest);

        $proxiedRequest = $this->convertToProxiedRequest();

        $client = $this->getHttpClient();

        try {
            //echo "<pre>";
            $response = $client->send($proxiedRequest,  ['debug' => false]);
            //echo "</pre>";
        } catch (RequestException $e) {
            if ($e->hasResponse()) {
                $response = $e->getResponse();
            }
            else {
                throw new \RuntimeException("Failed to connect to end-point", 0, $e);
            }

        } catch (ConnectException $e) {
            throw new RuntimeException("Failed to connect to end-point", $e);
        }

        $symfonyResponse = $this->bridge->createResponse($response);
        $symfonyResponse->send();
    }

    /**
     * @return \GuzzleHttp\Client the client used to communicate with the endpoint
     */
    protected function getHttpClient() {
        return new Client([<commit_msg>Fix exception not namespaced correctly<commit_after><?php

namespace ProxyFactory;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel;
use Symfony\Component\Routing;
use Symfony\Component\Routing\RouteCollection;
use Zend\Diactoros\Uri;

class ProxyFactory {

    /**
     * @var RouteCollection additional routes this proxy handles
     */
    protected $routes;

    /**
     * @var Routing\RequestContext the request context of this proxy request execution
     */
    protected $context;

    /**
     * @var HttpKernel\Controller\ControllerResolverInterface the resolver used by this proxy
     */
    protected $resolver;

    /**
     * @var Request
     */
    protected $symfonyRequest;

    /**
     * @var ServerRequestInterface
     */
    protected $request;

    /**
     * @var HttpFoundationFactory bridge used to convert PSR-7 requests/responses to Symfony HTTP foundation equivalents
     */
    protected $bridge;

    /**
     * @var DiactorosFactory factory to convert Symfony HTTP foundation requests to PSR-7 requests
     */
    protected $psr7Factory;

    /**
     * @var Uri the base URI of the proxied end-point
     */
    protected $proxyUri;

    /**
     * @param string $uri
     * @param array $arguments
     * @throws RuntimeException
     */
    function __construct($uri = "http://www.mocky.io/v2/", $arguments = array())
    {
        $this->proxyUri = new Uri($uri);

        if (!isset($arguments['routes'])) {
            $this->routes = new RouteCollection();
        } else {
            if (!$arguments['routes'] instanceof RouteCollection) {
                throw new RuntimeException("Invalid router");
            }
            $this->routes = $arguments['routes'];
        }

        $this->context = new Routing\RequestContext();
        $this->matcher = new Routing\Matcher\UrlMatcher($this->routes, $this->context);
        $this->resolver = new HttpKernel\Controller\ControllerResolver();

        $this->bridge = new HttpFoundationFactory();
        $this->psr7Factory = new DiactorosFactory();

        return $this;
    }

    /**
     * @param RequestInterface|ServerRequestInterface $request
     * @throws RuntimeException
     */
    function handleRequest(ServerRequestInterface $request = null) {
        if ($request == null) {
            $this->symfonyRequest = Request::createFromGlobals();
            $this->request = $this->psr7Factory->createRequest($this->symfonyRequest);
        } else {
            $this->request = $request;
            $this->symfonyRequest = $this->bridge->createRequest($this->request);
        }

        $this->context->fromRequest($this->symfonyRequest);

        $proxiedRequest = $this->convertToProxiedRequest();

        $client = $this->getHttpClient();

        try {
            //echo "<pre>";
            $response = $client->send($proxiedRequest,  ['debug' => false]);
            //echo "</pre>";
        } catch (RequestException $e) {
            if ($e->hasResponse()) {
                $response = $e->getResponse();
            }
            else {
                throw new \RuntimeException("Failed to connect to end-point", 0, $e);
            }

        } catch (ConnectException $e) {
            throw new \RuntimeException("Failed to connect to end-point", $e);
        }

        $symfonyResponse = $this->bridge->createResponse($response);
        $symfonyResponse->send();
    }

    /**
     * @return \GuzzleHttp\Client the client used to communicate with the endpoint
     */
    protected function getHttpClient() {
        return new Client([
{'fraction_non_alphanumeric': 0.12104730584366304, 'fraction_numerical': 0.0017708069820389577, 'mean_word_length': 2.539391226499552, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3786689', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2082, 'n_words': 487}