HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/ste.komma.pro/vendor/komma/feedback-company/src/FeedbackCompanyApi.php
<?php


namespace Komma\FeedbackCompany;

use Komma\FeedbackCompany\Endpoints\AuthenticationEndpoint;
use Komma\FeedbackCompany\Endpoints\QuestionEndpoint;
use Komma\FeedbackCompany\Endpoints\ReviewEndpoint;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\ResponseInterface;

class FeedbackCompanyApi
{

    /**
     * Endpoints of the remote API.
     */
    const API_ENDPOINT = 'https://www.feedbackcompany.com/api/';

    /**
     * Version of the remote API.
     */
    const API_VERSION = "v2";

    /**
     * By enabling the ResourceFactory will notify missing attribute of the resources
     *
     * @var bool
     */
    public static bool $debug = false;

    public static string $clientId;
    public static string $clientSecret;
    public static ?string $accessToken = null;

    protected ClientInterface $client;

    /**
     * List of Endpoints
     */
    private AuthenticationEndpoint $authentication;
    public ReviewEndpoint $reviews;
    public QuestionEndpoint $questions;

    public function __construct()
    {
        $this->client = new Client([
            'base_uri' => $this->getBaseUriEndPoint(),
        ]);

        $this->loadCredentialsByLaravelSettings();
        $this->initializeEndpoints();
        if(!isset(self::$accessToken)) $this->authentication->setAccessToken();
    }

    /**
     * Try to set the API credentials by loading the laravel config
     */
    private function loadCredentialsByLaravelSettings(): void
    {
        if(!function_exists('config')) return;
        self::$clientId = config('services.feedback_company.id');
        self::$clientSecret = config('services.feedback_company.secret');
        self::$debug = config('services.feedback_company.debug');
    }

    /**
     * Bind the endpoints
     */
    private function initializeEndpoints()
    {
        $this->authentication = new AuthenticationEndpoint($this);
        $this->reviews = new ReviewEndpoint($this);
        $this->questions = new QuestionEndpoint($this);
    }

    /**
     * Get the base Uri endpoint
     *
     * @return string
     */
    private function getBaseUriEndPoint(): string
    {
        return self::API_ENDPOINT . '/' . self::API_VERSION . '/';
    }

    /**
     * Send the request to the client.
     *
     * @param  Request  $request
     * @return object
     */
    public function send(Request $request): object
    {
        try {
            $response = $this->client->send($request, ['http_errors' => false]);
        } catch (GuzzleException $e) {
            throw new \RuntimeException($e);
        }

        if (!$response) {
            throw new \RuntimeException('Did not receive API response.');
        }

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

    /**
     * Parse the PSR-7 Response body
     *
     * @param ResponseInterface $response
     * @return \stdClass|array|null
     * @throws \RuntimeException
     */
    private function parseResponseBody(ResponseInterface $response): object
    {
        $body = (string) $response->getBody();
        $object = @json_decode($body);

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \RuntimeException("Unable to decode response: '{$body}'.");
        }

        if($response->getStatusCode() >= 400) {

            // If we get a 401 then we clear the stored access token
            if($response->getStatusCode() === 401) $this->authentication->clearAccessToken();

            if(isset($object) && isset($object->message)) $errorMessage = 'Error executing API call - ' . $object->message;
            elseif(isset($object) && isset($object->description)) $errorMessage = 'Error executing API call - ' . $object->description;
            elseif(isset($object) && isset($object->error)) $errorMessage = 'Error executing API call - ' . $object->error;
            else $errorMessage = 'Error executing API call';

            throw new \RuntimeException($errorMessage, $response->getStatusCode());
        }

        return $object;
    }
}