<commit_before><?php
/**
 * This file is part of the BEAR.Resource package
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 */
namespace BEAR\Resource\Adapter\Http;

use BEAR\Resource\Request;
use BEAR\Resource\ResourceObject;
use Guzzle\Cache\DoctrineCacheAdapter;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Cache\CachePlugin;
use Guzzle\Service\Client as GuzzleClient;
use Ray\Di\Di\Scope;

/**
 * Http resource
 *
 * @Scope("singleton")
 */
class Guzzle extends ResourceObject implements HttpClientInterface
{
    /**
     * HTTP Response
     *
     * @var RequestInterface
     */
    private $response;

    /**
     * Http client
     *
     * @var \Guzzle\Http\Client
     */
    private $guzzle;

    /**
     * @param GuzzleClient $guzzle
     */
    public function __construct(GuzzleClient $guzzle)
    {
        $this->guzzle = $guzzle;
        $cacheAdapter = (function_exists(
            'apc_cache_info'
        )) ? 'Doctrine\Common\Cache\ApcCache' : 'Doctrine\Common\Cache\ArrayCache';
        $adapter = new DoctrineCacheAdapter(new $cacheAdapter());
        $cache = new CachePlugin($adapter, true);
        $this->guzzle->getEventDispatcher()->addSubscriber($cache);
    }

    /**
     * {@inheritDoc}
     */
    public function onGet()
    {
        /** @noinspection PhpUndefinedMethodInspection */
        $this->response = $this->guzzle->get()->send();
        list($this->code, $this->headers, $this->body) = $this->parseResponse($this->response);

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function onPost()
    {
        /** @noinspection PhpUndefinedMethodInspection */
        $this->response = $this->guzzle->post()->send();
        list($this->code, $this->headers, $this->body) = $this->parseResponse($this->response);

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function onPut()
    {
        /** @noinspection PhpUndefinedMethodInspection */
        $this->response = $this->guzzle->put()->send();
        list($this->code, $this->headers, $this->body) = $this->parseResponse($this->response);

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function onDelete()
    {
        /** @noinspection PhpUndefinedMethodInspection */
        $this->response = $this->guzzle->delete()->send();
        list($this->code, $this->headers, $this->body) = $this->parseResponse($this->response);

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function onHead()
    {
        /** @noinspection PhpUndefinedMethodInspection */
        $this->response = $this->guzzle->head()->send();
        list($this->code, $this->headers, $this->body) = $this->parseResponse($this->response);

        return $this;
    }

    /**
     * Parse HTTP response
     *
     * @param \Guzzle\Http\Message\Response $response
     *
     * @return array
     */
    protected function parseResponse(Response $response)
    {
        /* @var $response \Guzzle\Http\Message\Response */
        $code = $response->getStatusCode();
        $headersArray = $response->getHeaders()->toArray();
        $body = $response->getBody(true);
        $headers = [];
        foreach ($headersArray as $key => $value) {
            $headers[strtolower($key)] = $value;
        }
        $contentType = $headers['content-type'];
        if (strpos($contentType[0], 'xml') !== false && $body) {
            $body = new \SimpleXMLElement($body);
        } elseif (strpos($contentType[0], 'json') !== false) {
            $body = json_decode($body);
        }

        return [$code, $headers, $body];
    }

    /**
     * {@inheritDoc}
     */
    public function onOptions()
    {
        /** @noinspection PhpUndefinedMethodInspection */<commit_msg>minor refactor<commit_after><?php
/**
 * This file is part of the BEAR.Resource package
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 */
namespace BEAR\Resource\Adapter\Http;

use BEAR\Resource\Request;
use BEAR\Resource\ResourceObject;
use Guzzle\Cache\DoctrineCacheAdapter;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response;
use Guzzle\Plugin\Cache\CachePlugin;
use Guzzle\Service\Client as GuzzleClient;
use Ray\Di\Di\Scope;

/**
 * Http resource
 *
 * @Scope("singleton")
 */
class Guzzle extends ResourceObject implements HttpClientInterface
{
    /**
     * HTTP Response
     *
     * @var RequestInterface
     */
    private $response;

    /**
     * Http client
     *
     * @var \Guzzle\Http\Client
     */
    private $guzzle;

    /**
     * @param GuzzleClient $guzzle
     */
    public function __construct(GuzzleClient $guzzle)
    {
        $this->guzzle = $guzzle;
        $cacheAdapter = (function_exists(
            'apc_cache_info'
        )) ? 'Doctrine\Common\Cache\ApcCache' : 'Doctrine\Common\Cache\ArrayCache';
        $adapter = new DoctrineCacheAdapter(new $cacheAdapter());
        $cache = new CachePlugin($adapter, true);
        $this->guzzle->getEventDispatcher()->addSubscriber($cache);
    }

    /**
     * {@inheritDoc}
     */
    public function onGet()
    {
        /** @noinspection PhpUndefinedMethodInspection */
        $this->response = $this->guzzle->get()->send();
        list($this->code, $this->headers, $this->body) = $this->parseResponse($this->response);

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function onPost()
    {
        /** @noinspection PhpUndefinedMethodInspection */
        $this->response = $this->guzzle->post()->send();
        list($this->code, $this->headers, $this->body) = $this->parseResponse($this->response);

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function onPut()
    {
        /** @noinspection PhpUndefinedMethodInspection */
        $this->response = $this->guzzle->put()->send();
        list($this->code, $this->headers, $this->body) = $this->parseResponse($this->response);

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function onDelete()
    {
        /** @noinspection PhpUndefinedMethodInspection */
        $this->response = $this->guzzle->delete()->send();
        list($this->code, $this->headers, $this->body) = $this->parseResponse($this->response);

        return $this;
    }

    /**
     * {@inheritDoc}
     */
    public function onHead()
    {
        /** @noinspection PhpUndefinedMethodInspection */
        $this->response = $this->guzzle->head()->send();
        list($this->code, $this->headers, $this->body) = $this->parseResponse($this->response);

        return $this;
    }

    /**
     * Parse HTTP response
     *
     * @param \Guzzle\Http\Message\Response $response
     *
     * @return array
     */
    protected function parseResponse(Response $response)
    {
        /* @var $response \Guzzle\Http\Message\Response */
        $code = $response->getStatusCode();
        $body = $response->getBody(true);
        $headersArray = $response->getHeaders()->toArray();
        $headers = array_change_key_case($headersArray);
        $contentType = $headers['content-type'];
        if (strpos($contentType[0], 'xml') !== false && $body) {
            $body = new \SimpleXMLElement($body);
        } elseif (strpos($contentType[0], 'json') !== false) {
            $body = json_decode($body);
        }

        return [$code, $headers, $body];
    }

    /**
     * {@inheritDoc}
     */
    public function onOptions()
    {
        /** @noinspection PhpUndefinedMethodInspection */
{'fraction_non_alphanumeric': 0.16267123287671234, 'fraction_numerical': 0.0005268703898840885, 'mean_word_length': 2.4466636404902404, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3857927', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2266, 'n_words': 397}