<commit_before><?php namespace Arcanedev\Stripe\Utilities;

use Arcanedev\Stripe\Contracts\Utilities\ApiErrorsHandlerInterface;
use Arcanedev\Stripe\Exceptions\ApiException;
use Arcanedev\Stripe\Exceptions\AuthenticationException;
use Arcanedev\Stripe\Exceptions\CardException;
use Arcanedev\Stripe\Exceptions\InvalidRequestException;
use Arcanedev\Stripe\Exceptions\RateLimitException;

class ErrorsHandler implements ApiErrorsHandlerInterface
{
    /* ------------------------------------------------------------------------------------------------
     |  Properties
     | ------------------------------------------------------------------------------------------------
     */
    /** @var string */
    private $respBody;

    /** @var int */
    private $respCode;

    /** @var array */
    private $response   = [];

    /** @var array */
    private static $exceptions = [
        400 => '\\Arcanedev\\Stripe\\Exceptions\\InvalidRequestException',
        401 => '\\Arcanedev\\Stripe\\Exceptions\\AuthenticationException',
        402 => '\\Arcanedev\\Stripe\\Exceptions\\CardException',
        404 => '\\Arcanedev\\Stripe\\Exceptions\\InvalidRequestException',
        429 => '\\Arcanedev\\Stripe\\Exceptions\\RateLimitException',
        500 => '\\Arcanedev\\Stripe\\Exceptions\\ApiException',
    ];

    /* ------------------------------------------------------------------------------------------------
     |  Getters & Setters
     | ------------------------------------------------------------------------------------------------
     */
    /**
     * Set Response body (JSON)
     *
     * @param string $respBody
     *
     * @return ErrorsHandler
     */
    private function setRespBody($respBody)
    {
        $this->respBody = $respBody;

        return $this;
    }

    /**
     * Set Response Code
     *
     * @param int $respCode
     *
     * @return ErrorsHandler
     */
    private function setRespCode($respCode)
    {
        $this->respCode = $respCode;

        return $this;
    }

    /**
     * Set Response
     *
     * @param array $response
     *
     * @return $this
     */
    private function setResponse($response)
    {
        $this->checkResponse($response);

        $this->response = $response;

        return $this;
    }

    /**
     * Get Exception class
     *
     * @return string
     */
    private function getException()
    {
        return $this->hasException()
            ? $this->getExceptionByCode($this->respCode)
            : $this->getExceptionByCode(500);
    }

    /**
     * Get Exception class by status code
     *
     * @param int $code
     *
     * @return string
     */
    private function getExceptionByCode($code)
    {
        return self::$exceptions[$code];
    }

    /* ------------------------------------------------------------------------------------------------
     |  Main Functions
     | ------------------------------------------------------------------------------------------------
     */
    /**
     * Handle API Errors
     *
     * @param string $respBody
     * @param int    $respCode
     * @param array  $response
     *
     * @throws ApiException
     * @throws AuthenticationException
     * @throws CardException
     * @throws InvalidRequestException
     * @throws RateLimitException
     *
     * @return void
     */
    public function handle($respBody, $respCode, $response)
    {
        if ($respCode >= 200 or $respCode < 300) {
            return;
        }

        $this->setRespBody($respBody);
        $this->setRespCode($respCode);
        $this->setResponse($response);

        list($message, $type, $stripeCode, $params) = $this->parseResponseError();

        $exception = $this->getException();

        if ($this->respCode === 400 and $stripeCode === 'rate_limit') {
            $this->setRespCode(429);
            $exception = $this->getExceptionByCode(429);
        }

        throw new $exception(
            $message,
            $this->respCode,
            $type,
            $stripeCode,
            $this->respBody,
            $this->response,
            $params<commit_msg>Fixing ErrorsHandler, handle method condition<commit_after><?php namespace Arcanedev\Stripe\Utilities;

use Arcanedev\Stripe\Contracts\Utilities\ApiErrorsHandlerInterface;
use Arcanedev\Stripe\Exceptions\ApiException;
use Arcanedev\Stripe\Exceptions\AuthenticationException;
use Arcanedev\Stripe\Exceptions\CardException;
use Arcanedev\Stripe\Exceptions\InvalidRequestException;
use Arcanedev\Stripe\Exceptions\RateLimitException;

class ErrorsHandler implements ApiErrorsHandlerInterface
{
    /* ------------------------------------------------------------------------------------------------
     |  Properties
     | ------------------------------------------------------------------------------------------------
     */
    /** @var string */
    private $respBody;

    /** @var int */
    private $respCode;

    /** @var array */
    private $response   = [];

    /** @var array */
    private static $exceptions = [
        400 => '\\Arcanedev\\Stripe\\Exceptions\\InvalidRequestException',
        401 => '\\Arcanedev\\Stripe\\Exceptions\\AuthenticationException',
        402 => '\\Arcanedev\\Stripe\\Exceptions\\CardException',
        404 => '\\Arcanedev\\Stripe\\Exceptions\\InvalidRequestException',
        429 => '\\Arcanedev\\Stripe\\Exceptions\\RateLimitException',
        500 => '\\Arcanedev\\Stripe\\Exceptions\\ApiException',
    ];

    /* ------------------------------------------------------------------------------------------------
     |  Getters & Setters
     | ------------------------------------------------------------------------------------------------
     */
    /**
     * Set Response body (JSON)
     *
     * @param string $respBody
     *
     * @return ErrorsHandler
     */
    private function setRespBody($respBody)
    {
        $this->respBody = $respBody;

        return $this;
    }

    /**
     * Set Response Code
     *
     * @param int $respCode
     *
     * @return ErrorsHandler
     */
    private function setRespCode($respCode)
    {
        $this->respCode = $respCode;

        return $this;
    }

    /**
     * Set Response
     *
     * @param array $response
     *
     * @return $this
     */
    private function setResponse($response)
    {
        $this->checkResponse($response);

        $this->response = $response;

        return $this;
    }

    /**
     * Get Exception class
     *
     * @return string
     */
    private function getException()
    {
        return $this->hasException()
            ? $this->getExceptionByCode($this->respCode)
            : $this->getExceptionByCode(500);
    }

    /**
     * Get Exception class by status code
     *
     * @param int $code
     *
     * @return string
     */
    private function getExceptionByCode($code)
    {
        return self::$exceptions[$code];
    }

    /* ------------------------------------------------------------------------------------------------
     |  Main Functions
     | ------------------------------------------------------------------------------------------------
     */
    /**
     * Handle API Errors
     *
     * @param string $respBody
     * @param int    $respCode
     * @param array  $response
     *
     * @throws ApiException
     * @throws AuthenticationException
     * @throws CardException
     * @throws InvalidRequestException
     * @throws RateLimitException
     *
     * @return void
     */
    public function handle($respBody, $respCode, $response)
    {
        if ($respCode >= 200 and $respCode < 300) {
            return;
        }

        $this->setRespBody($respBody);
        $this->setRespCode($respCode);
        $this->setResponse($response);

        list($message, $type, $stripeCode, $params) = $this->parseResponseError();

        $exception = $this->getException();

        if ($this->respCode === 400 and $stripeCode === 'rate_limit') {
            $this->setRespCode(429);
            $exception = $this->getExceptionByCode(429);
        }

        throw new $exception(
            $message,
            $this->respCode,
            $type,
            $stripeCode,
            $this->respBody,
            $this->response,
            $params
{'fraction_non_alphanumeric': 0.2497289483194796, 'fraction_numerical': 0.008673653776653415, 'mean_word_length': 1.3511753044463324, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 55, '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': '5606287', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2110, 'n_words': 387}