<commit_before><?php namespace Ordercloud\Requests\Handlers;

use Ordercloud\Requests\OrdercloudRequest;
use Ordercloud\Support\Http\Response;
use Ordercloud\Support\Reflection\EntityReflector;

abstract class AbstractRequestHandler extends OrdercloudRequestHandler
{
    protected $method;
    private $url;
    private $parameters = [];
    private $headers = [];
    private $entityClass = 'Ordercloud\Entities\Users\UserProfile';
    private $entityDataKey = null;

    /**
     * @param $request
     */
    abstract protected function configure($request);

    /**
     * @param $request
     *
     * @return mixed|\Ordercloud\Support\Http\Response
     *
     * @throws \Ordercloud\Requests\Exceptions\UnauthorizedRequestException
     * @throws \Ordercloud\Requests\Exceptions\OrdercloudRequestException
     */
    public function handle($request)
    {
        $this->configure($request);

        $response = parent::handle(
            new OrdercloudRequest($this->method, $this->url, $this->parameters, $this->headers)
        );

        return $this->transformResponse($response);
    }

    /**
     * @param Response $response
     *
     * @return mixed
     */
    protected function transformResponse($response)
    {
        // TODO this needs to get out

        if ($this->entityDataKey) {
            return EntityReflector::parseAll($this->entityClass, $response->getData($this->entityDataKey));
        }

        return EntityReflector::parse($this->entityClass, $response->getData());
    }

    /**
     * Set the url. Optionally apply the sprintf function to
     * the url supplied by a variable number of arguments.
     *
     * @param string $url
     * @param mixed  $args [optional]
     *
     * @return static
     */
    protected function setUrl($url, $args = null)
    {<commit_msg>Do not reflect response if no entity class is set<commit_after><?php namespace Ordercloud\Requests\Handlers;

use Ordercloud\Requests\OrdercloudRequest;
use Ordercloud\Support\Http\Response;
use Ordercloud\Support\Reflection\EntityReflector;

abstract class AbstractRequestHandler extends OrdercloudRequestHandler
{
    protected $method;
    private $url;
    private $parameters = [];
    private $headers = [];
    private $entityClass;
    private $entityDataKey = null;

    /**
     * @param $request
     */
    abstract protected function configure($request);

    /**
     * @param $request
     *
     * @return mixed|\Ordercloud\Support\Http\Response
     *
     * @throws \Ordercloud\Requests\Exceptions\UnauthorizedRequestException
     * @throws \Ordercloud\Requests\Exceptions\OrdercloudRequestException
     */
    public function handle($request)
    {
        $this->configure($request);

        $response = parent::handle(
            new OrdercloudRequest($this->method, $this->url, $this->parameters, $this->headers)
        );

        return $this->transformResponse($response);
    }

    /**
     * @param Response $response
     *
     * @return mixed
     */
    protected function transformResponse($response)
    {
        // TODO this needs to get out

        if ($this->entityDataKey) {
            return EntityReflector::parseAll($this->entityClass, $response->getData($this->entityDataKey));
        }

        if ($this->entityClass) {
            return EntityReflector::parse($this->entityClass, $response->getData());
        }

        return $response;
    }

    /**
     * Set the url. Optionally apply the sprintf function to
     * the url supplied by a variable number of arguments.
     *
     * @param string $url
     * @param mixed  $args [optional]
     *
     * @return static
     */
    protected function setUrl($url, $args = null)
    {
{'fraction_non_alphanumeric': 0.11541565778853914, 'fraction_numerical': 0.0, 'mean_word_length': 2.7142857142857144, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, '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': '5893456', 'n_tokens_mistral': 983, 'n_tokens_neox': 988, 'n_words': 230}