<?php
 declare(strict_types=1); namespace Square\Apis; use Square\Http\HttpCallBack; use Square\Http\HttpResponse; use Square\ConfigurationInterface; use Square\AuthManagerInterface; use Unirest\Request; class BaseApi { protected $config; private $authManagers = []; private $httpCallBack; protected $internalUserAgent; private static $userAgent = 'Square-PHP-SDK/19.1.1.20220616 ({api-version}) {engine}/{engine-version} ({os-info}) {detail}'; protected function __construct(ConfigurationInterface $config, array $authManagers, ?HttpCallBack $httpCallBack) { $this->config = $config; $this->authManagers = $authManagers; $this->httpCallBack = $httpCallBack; $this->updateUserAgent(); $this->internalUserAgent = str_replace( ['{api-version}', '{detail}'], [$config->getSquareVersion(), rawurlencode($config->getUserAgentDetail())], self::$userAgent ); Request::timeout($config->getTimeout()); Request::enableRetries($config->shouldEnableRetries()); Request::maxNumberOfRetries($config->getNumberOfRetries()); Request::retryInterval($config->getRetryInterval()); Request::backoffFactor($config->getBackOffFactor()); Request::maximumRetryWaitTime($config->getMaximumRetryWaitTime()); Request::retryOnTimeout($config->shouldRetryOnTimeout()); Request::httpMethodsToRetry($config->getHttpMethodsToRetry()); Request::httpStatusCodesToRetry($config->getHttpStatusCodesToRetry()); } private function updateUserAgent(): void { if (preg_match('({engine}|{engine-version}|{os-info})', self::$userAgent) === 1) { $placeHolders = [ '{engine}' => !empty(zend_version()) ? 'Zend' : '', '{engine-version}' => zend_version(), '{os-info}' => PHP_OS_FAMILY !== 'Unknown' ? PHP_OS_FAMILY . '-' . php_uname('r') : '', ]; self::$userAgent = str_replace( array_keys($placeHolders), array_values($placeHolders), self::$userAgent ); } } protected function getAuthManager(string $key): AuthManagerInterface { return $this->authManagers[$key]; } public function getHttpCallBack(): ?HttpCallBack { return $this->httpCallBack; } protected function isValidResponse(HttpResponse $response): bool { return $response->getStatusCode() >= 200 && $response->getStatusCode() < 300; } } 