Your IP : 216.73.216.86


Current Path : /var/www/homesaver/www/bitrix/classes/general/ferma-api/
Upload File :
Current File : /var/www/homesaver/www/bitrix/classes/general/ferma-api/CFermaApiClient.php

<?php

class CFermaApiClient
{
    /**
     * @var string
     */
    private $apiHost;
    /**
     * @var string
     */
    private $errorsEmail;
    /**
     * @var int
     */
    private $timeout;

    private static $instances = [];

    public function __construct($apiHost, $errorsEmail, $timeout = 15)
    {
        $this->apiHost = $apiHost;
        $this->errorsEmail = $errorsEmail;
        $this->timeout = $timeout;
    }

    /**
     * @param string $apiHost
     * @param string $errorsEmail
     * @param int $timeout
     * @return CFermaApiClient
     */
    public static function instance($apiHost, $errorsEmail, $timeout = 15)
    {
        $hash = sha1($apiHost . '/' . $errorsEmail . '/' . $timeout);

        if (!isset(static::$instances[$hash])) {
            static::$instances[$hash] = new static($apiHost, $errorsEmail, $timeout);
        }

        return static::$instances[$hash];
    }

    /**
     * @param string $uri
     * @param array $data
     * @param string|null $accessToken
     * @return array
     * @throws \CFermaApiException
     */
    public function post($uri, $data = [], $accessToken = null)
    {
        $encodedData = json_encode($data);

        $ch = curl_init($this->getFullUri($uri, $accessToken));
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json',
            'Content-Length: ' . mb_strlen($encodedData, 'utf-8'),
        ]);

        $result = curl_exec($ch);

        $result = json_decode($result, true);

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new CFermaApiException('Failed parse response.');
        }

        return $result;
    }

    private function sendErrorReport(\Exception $e)
    {
        if (empty($this->errorsEmail)) {
            return;
        }

        $date = date('d.m.Y H:i');

        $message = implode('<br>', [
            'Здравствуйте.',
            'При отправке запроса в api фермы OFD.ru произошла ошибка:',
            '<code>%s</code>',
            '',
            'Если проблема повторяется, сообщите о ней по адресу support@ofd.ru.',
            '',
            'С уважением, команда OFD.ru Ferma.',
        ]);

        $message = sprintf($message, $e->getMessage());

        bxmail($this->errorsEmail, 'OFD.ru Ferma error report ' . $date, $message);
    }

    private function getFullUri($uri, $accessToken = null)
    {
        $authToken = '';

        if ($accessToken) {
            $isArgs = strpos($uri, '?') !== false ? '&' : '?';
            $authToken = $isArgs . 'AuthToken=' . $accessToken;
        }

        return rtrim($this->apiHost, '/') . '/' . ltrim($uri, '/') . $authToken;
    }
}