diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fdcde94..56ced4a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,28 @@ # bitrix24-php-sdk change log +## 0.4.0 (16.07.2016) +* remove all exceptions in namespace `\Exceptions` see issue [Move all exceptions in namespace «Exceptions» #46](https://github.com/mesilov/bitrix24-php-sdk/issues/46) +* add class `Bitrix24\Exceptions\Bitrix24Exception` +* add class `Bitrix24\Exceptions\Bitrix24IoException` +* add class `Bitrix24\Exceptions\Bitrix24EmptyResponseException` +* add class `Bitrix24\Exceptions\Bitrix24ApiException` +* add class `Bitrix24\Exceptions\Bitrix24WrongClientException` +* add class `Bitrix24\Exceptions\Bitrix24MethodNotFoundException` +* add class `Bitrix24\Exceptions\Bitrix24TokenIsInvalidException` +* add class `Bitrix24\Exceptions\Bitrix24TokenIsExpiredException` +* add class `Bitrix24\Exceptions\Bitrix24PortalDeletedException` +* add class `Bitrix24\Exceptions\Bitrix24PaymentRequiredException` +* add class `Bitrix24\Exceptions\Bitrix24SecurityException` +* updated class `Bitrix24\Bitrix24Exception` mark as **deprecated** +* updated class `Bitrix24\Bitrix24IoException` mark as **deprecated** +* updated class `Bitrix24\Bitrix24EmptyResponseException` mark as **deprecated** +* updated class `Bitrix24\Bitrix24ApiException` mark as **deprecated** +* updated class `Bitrix24\Bitrix24WrongClientException` mark as **deprecated** +* updated class `Bitrix24\Bitrix24MethodNotFoundException` mark as **deprecated** +* updated class `Bitrix24\Bitrix24TokenIsInvalid` mark as **deprecated** +* updated class `Bitrix24\Bitrix24TokenIsExpired` mark as **deprecated** +* updated class `Bitrix24\Bitrix24PortalDeleted` mark as **deprecated** +* updated class `Bitrix24\Bitrix24SecurityException` mark as **deprecated** + ## 0.3.4 (06.06.2016) * add exception class `Bitrix24EmptyResponseException` * in class `Bitrix24` add debug information for some error types diff --git a/src/bitrix24.php b/src/bitrix24.php index 753a5179..71232438 100644 --- a/src/bitrix24.php +++ b/src/bitrix24.php @@ -3,6 +3,18 @@ use Bitrix24\Contracts\iBitrix24; +use Bitrix24\Exceptions\Bitrix24Exception; +use Bitrix24\Exceptions\Bitrix24IoException; +use Bitrix24\Exceptions\Bitrix24PaymentRequiredException; +use Bitrix24\Exceptions\Bitrix24EmptyResponseException; +use Bitrix24\Exceptions\Bitrix24ApiException; +use Bitrix24\Exceptions\Bitrix24TokenIsInvalidException; +use Bitrix24\Exceptions\Bitrix24WrongClientException; +use Bitrix24\Exceptions\Bitrix24MethodNotFoundException; +use Bitrix24\Exceptions\Bitrix24TokenIsExpiredException; +use Bitrix24\Exceptions\Bitrix24PortalDeletedException; +use Bitrix24\Exceptions\Bitrix24SecurityException; + use Psr\Log\NullLogger; use Psr\Log\LoggerInterface; @@ -13,612 +25,588 @@ */ class Bitrix24 implements iBitrix24 { - /** - * @var string SDK version - */ - const VERSION = '1.0'; - - /** - * @var string OAuth server - */ - const OAUTH_SERVER = 'oauth.bitrix.info'; - - /** - * @var string access token - */ - protected $accessToken; - - /** - * @var string refresh token - */ - protected $refreshToken; - - /** - * @var string domain - */ - protected $domain; - - /** - * @var array scope - */ - protected $applicationScope = array(); - - /** - * @var string application id - */ - protected $applicationId; - - /** - * @var string application secret - */ - protected $applicationSecret; - - /** - * @var array raw request, contain all cURL options array and API query - */ - protected $rawRequest; - - /** - * @var array, contain all api-method parameters, vill be available after call method - */ - protected $methodParameters; - - /** - * @var array request info data structure акщь curl_getinfo function - */ - protected $requestInfo; - - /** - * @var bool if true raw response from bitrix24 will be available from method getRawResponse, this is debug mode - */ - protected $isSaveRawResponse = false; - - /** - * @var array raw response from bitrix24 - */ - protected $rawResponse; - - /** - * @var string redirect URI from application settings - */ - protected $redirectUri; - - /** - * @var string portal GUID - */ - protected $memberId; - - /** - * @var array custom options for cURL - */ - protected $customCurlOptions; - - /** - * @see https://github.com/Seldaek/monolog - * @var \Monolog\Logger PSR-3 compatible logger, use only from wrappers methods log* - */ - protected $log; - - /** - * @var integer CURL request count retries - */ - protected $retriesToConnectCount; - - /** - * @var integer retries to connect timeout in microseconds - */ - protected $retriesToConnectTimeout; - - /** - * Create a object to work with Bitrix24 REST API service - * - * @param bool $isSaveRawResponse - if true raw response from bitrix24 will be available from method getRawResponse, this is debug mode - * @param null|LoggerInterface $obLogger - instance of \Monolog\Logger - * - * @throws Bitrix24Exception - * - * @return Bitrix24 - */ - public function __construct($isSaveRawResponse = false, LoggerInterface $obLogger = null) - { - if (!extension_loaded('curl')) - { - throw new Bitrix24Exception('cURL extension must be installed to use this library'); - } - if(!is_bool($isSaveRawResponse)) - { - throw new Bitrix24Exception('isSaveRawResponse flag must be boolean'); - } - $this->isSaveRawResponse = $isSaveRawResponse; - if($obLogger !== null) - { - /** - * @var \Monolog\Logger - */ - $this->log = clone $obLogger; - } - else - { - // dev/null logger - /** - * @var \Monolog\Logger - */ - $this->log = new NullLogger(); - } - $this->setRetriesToConnectCount(1); - $this->setRetriesToConnectTimeout(1000000); - } - - /** - * Get a random string to sign protected api-call. Use salt for argument "state" in secure api-call - * random string is a result of mt_rand function - * - * @return int - */ - public function getSecuritySignSalt() - { - return mt_rand(); - } - - /** - * Set member ID — portal GUID - * - * @param string $memberId - * - * @throws Bitrix24Exception - * - * @return true - */ - public function setMemberId($memberId) - { - if('' === $memberId) - { - throw new Bitrix24Exception('memberId is empty'); - } - elseif(null === $memberId) - { - throw new Bitrix24Exception('memberId is null'); - } - $this->memberId = $memberId; - return true; - } - - /** - * Get memeber ID - * - * @return string | null - */ - public function getMemberId() - { - return $this->memberId; - } - - /** - * Set redirect URI - * - * @param string $redirectUri - * - * @throws Bitrix24Exception - * - * @return true; - */ - public function setRedirectUri($redirectUri) - { - if('' === $redirectUri) - { - throw new Bitrix24Exception('redirect URI is empty'); - } - $this->redirectUri = $redirectUri; - return true; - } - - /** - * Get redirect URI - * - * @return string | null - */ - public function getRedirectUri() - { - return $this->redirectUri; - } - - /** - * Set access token - * - * @param string $accessToken - * - * @throws Bitrix24Exception - * - * @return true - */ - public function setAccessToken($accessToken) - { - if('' === $accessToken) - { - throw new Bitrix24Exception('access token is empty'); - } - $this->accessToken = $accessToken; - return true; - } - - /** - * Get access token - * - * @return string | null - */ - public function getAccessToken() - { - return $this->accessToken; - } - - /** - * Set refresh token - * - * @param $refreshToken - * - * @throws Bitrix24Exception - * - * @return true; - */ - public function setRefreshToken($refreshToken) - { - if('' === $refreshToken) - { - throw new Bitrix24Exception('refresh token is empty'); - } - $this->refreshToken = $refreshToken; - return true; - } - - /** - * Get refresh token - * - * @return string - */ - public function getRefreshToken() - { - return $this->refreshToken; - } - - /** - * Set domain - * - * @param $domain - * - * @throws Bitrix24Exception - * - * @return true; - */ - public function setDomain($domain) - { - if('' === $domain) - { - throw new Bitrix24Exception('domain is empty'); - } - $this->domain = $domain; - return true; - } - - /** - * Get domain - * - * @return string | null - */ - public function getDomain() - { - return $this->domain; - } - - /** - * Set application scope - * - * @param array $applicationScope - * - * @return boolean - * - * @throws Bitrix24Exception - */ - public function setApplicationScope(array $applicationScope) - { - if(is_array($applicationScope) && count($applicationScope)> 0) - { - $this->applicationScope = $applicationScope; - return true; - } - else - { - throw new Bitrix24Exception('application scope not set'); - } - } - - /** - * Get application scope - * - * @return string - */ - public function getApplicationScope() - { - return $this->applicationScope; - } - - /** - * Set application id - * - * @param string $applicationId - * - * @throws Bitrix24Exception - * - * @return true; - */ - public function setApplicationId($applicationId) - { - if('' === $applicationId) - { - throw new Bitrix24Exception('application id is empty'); - } - $this->applicationId = $applicationId; - return true; - }// end of SetApplicationId - - /** - * Get application id - * - * @return string - */ - public function getApplicationId() - { - return $this->applicationId; - } - - /** - * Set application secret - * - * @param string $applicationSecret - * - * @throws Bitrix24Exception - * - * @return true; - */ - public function setApplicationSecret($applicationSecret) - { - if('' === $applicationSecret) - { - throw new Bitrix24Exception('application secret is empty'); - } - $this->applicationSecret = $applicationSecret; - return true; - } - - /** - * Get application secret - * - * @return string - */ - public function getApplicationSecret() - { - return $this->applicationSecret; - } - - /** - * Set custom cURL options, overriding default ones - * - * @link http://php.net/manual/en/function.curl-setopt.php - * - * @param array $options - array(CURLOPT_XXX => value1, CURLOPT_XXX2 => value2,...) - * - * @return bool - */ - public function setCustomCurlOptions($options) - { - $this->customCurlOptions = $options; - - return true; - } - - /** - * Return raw request, contain all cURL options array and API query. Data available after you try to call method call - * numbers of array keys is const of cURL module. Example: CURLOPT_RETURNTRANSFER = 19913 - * - * @return array | null - */ - public function getRawRequest() - { - return $this->rawRequest; - } - - /** - * Return result from function curl_getinfo. Data available after you try to call method call - * - * @return array | null - */ - public function getRequestInfo() - { - return $this->requestInfo; - } - - /** - * Return additional parameters of last api-call. Data available after you try to call method call - * - * @return array | null - */ - public function getMethodParameters() - { - return $this->methodParameters; - } - - /** - * get error context - * - * @return array - */ - protected function getErrorContext() - { - return array( - // portal specific settings - 'B24_DOMAIN' => $this->getDomain(), - 'B24_MEMBER_ID' => $this->getMemberId(), - 'B24_ACCESS_TOKEN' => $this->getAccessToken(), - 'B24_REFRESH_TOKEN' => $this->getRefreshToken(), - // application settings - 'APPLICATION_SCOPE' => $this->getApplicationScope(), - 'APPLICATION_ID' => $this->getApplicationId(), - 'APPLICATION_SECRET' => $this->getApplicationSecret(), - 'REDIRECT_URI' => $this->getRedirectUri(), - // network - 'RAW_REQUEST' => $this->getRawRequest(), - 'CURL_REQUEST_INFO' => $this->getRequestInfo(), - 'RAW_RESPONSE' => $this->getRawResponse() - ); - } - - /** - * Execute a request API to Bitrix24 using cURL - * - * @param string $url - * @param array $additionalParameters - * - * @throws Bitrix24Exception - * @throws Bitrix24PortalDeleted - * @throws Bitrix24IoException - * @throws Bitrix24EmptyResponseException - * - * @return array - */ - protected function executeRequest($url, array $additionalParameters = array()) - { - $retryableErrorCodes = array( - CURLE_COULDNT_RESOLVE_HOST, - CURLE_COULDNT_CONNECT, - CURLE_HTTP_NOT_FOUND, - CURLE_READ_ERROR, - CURLE_OPERATION_TIMEOUTED, - CURLE_HTTP_POST_ERROR, - CURLE_SSL_CONNECT_ERROR - ); - - $curlOptions = array( - CURLOPT_RETURNTRANSFER => true, - CURLINFO_HEADER_OUT => true, - CURLOPT_VERBOSE => true, - CURLOPT_CONNECTTIMEOUT => 5, - CURLOPT_TIMEOUT => 5, - CURLOPT_USERAGENT => strtolower(__CLASS__.'-PHP-SDK/v'.self::VERSION), - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => http_build_query($additionalParameters), - CURLOPT_URL => $url - ); - - if(is_array($this->customCurlOptions)) { - foreach($this->customCurlOptions as $customCurlOptionKey => $customCurlOptionValue) { - $curlOptions[$customCurlOptionKey] = $customCurlOptionValue; - } - } - - $this->rawRequest = $curlOptions; - $curl = curl_init(); - curl_setopt_array($curl, $curlOptions); - - $curlResult = false; - $retriesCnt = $this->retriesToConnectCount; - while($retriesCnt--){ - $this->log->debug(sprintf('try [%s] to connect to host [%s]', $retriesCnt, $this->getDomain())); - $curlResult = curl_exec($curl); - // handling network I/O errors - if(false === $curlResult) - { - $curlErrorNumber = curl_errno($curl); - $errorMsg = sprintf('in try[%s] cURL error (code %s): %s'.PHP_EOL, $retriesCnt, $curlErrorNumber, curl_error($curl)); - if (false === in_array($curlErrorNumber, $retryableErrorCodes, true) || !$retriesCnt) { - $this->log->error($errorMsg, $this->getErrorContext()); - curl_close($curl); - throw new Bitrix24IoException($errorMsg); - } - else - { - $this->log->warning($errorMsg, $this->getErrorContext()); - } - usleep($this->getRetriesToConnectTimeout()); - continue; - } - $this->requestInfo = curl_getinfo($curl); - $this->rawResponse = $curlResult; - $this->log->debug('cURL request info', array($this->getRequestInfo())); - curl_close($curl); - break; - } - - // handling URI level resource errors - switch ($this->requestInfo['http_code']) - { - case 403: - $errorMsg = sprintf('portal [%s] deleted, query aborted', $this->getDomain()); - $this->log->error($errorMsg, $this->getErrorContext()); - throw new Bitrix24PortalDeleted($errorMsg); - break; - } - - // handling server-side API errors: empty response from bitrix24 portal - if($curlResult === '') - { - $errorMsg = sprintf('empty response from portal [%s]', $this->getDomain()); - $this->log->error($errorMsg, $this->getErrorContext()); - throw new Bitrix24EmptyResponseException($errorMsg); - } - - // handling json_decode errors - $jsonResult = json_decode($curlResult, true); - unset($curlResult); - $jsonErrorCode = json_last_error(); - if(null === $jsonResult && (JSON_ERROR_NONE !== $jsonErrorCode)) - { - /** - * @todo add function json_last_error_msg() - */ - $errorMsg = 'fatal error in function json_decode.'.PHP_EOL.'Error code: '.$jsonErrorCode.PHP_EOL; - $this->log->error($errorMsg, $this->getErrorContext()); - throw new Bitrix24Exception($errorMsg); - } - return $jsonResult; - } - - /** - * Execute Bitrix24 REST API method - * - * @param string $methodName - * @param array $additionalParameters - * - * @throws Bitrix24Exception - * @throws Bitrix24ApiException - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24WrongClientException - * @throws Bitrix24MethodNotFoundException - * @throws Bitrix24SecurityException - * @throws Bitrix24PortalDeleted - * @throws Bitrix24IoException - * @throws Bitrix24EmptyResponseException - * - * @return array - */ - public function call($methodName, array $additionalParameters = array()) - { + /** + * @var string SDK version + */ + const VERSION = '1.0'; + + /** + * @var string OAuth server + */ + const OAUTH_SERVER = 'oauth.bitrix.info'; + + /** + * @var string access token + */ + protected $accessToken; + + /** + * @var string refresh token + */ + protected $refreshToken; + + /** + * @var string domain + */ + protected $domain; + + /** + * @var array scope + */ + protected $applicationScope = array(); + + /** + * @var string application id + */ + protected $applicationId; + + /** + * @var string application secret + */ + protected $applicationSecret; + + /** + * @var array raw request, contain all cURL options array and API query + */ + protected $rawRequest; + + /** + * @var array, contain all api-method parameters, vill be available after call method + */ + protected $methodParameters; + + /** + * @var array request info data structure акщь curl_getinfo function + */ + protected $requestInfo; + + /** + * @var bool if true raw response from bitrix24 will be available from method getRawResponse, this is debug mode + */ + protected $isSaveRawResponse = false; + + /** + * @var array raw response from bitrix24 + */ + protected $rawResponse; + + /** + * @var string redirect URI from application settings + */ + protected $redirectUri; + + /** + * @var string portal GUID + */ + protected $memberId; + + /** + * @var array custom options for cURL + */ + protected $customCurlOptions; + + /** + * @see https://github.com/Seldaek/monolog + * @var \Monolog\Logger PSR-3 compatible logger, use only from wrappers methods log* + */ + protected $log; + + /** + * @var integer CURL request count retries + */ + protected $retriesToConnectCount; + + /** + * @var integer retries to connect timeout in microseconds + */ + protected $retriesToConnectTimeout; + + /** + * Create a object to work with Bitrix24 REST API service + * + * @param bool $isSaveRawResponse - if true raw response from bitrix24 will be available from method getRawResponse, this is debug mode + * @param null|LoggerInterface $obLogger - instance of \Monolog\Logger + * + * @throws Bitrix24Exception + * + * @return Bitrix24 + */ + public function __construct($isSaveRawResponse = false, LoggerInterface $obLogger = null) + { + if (!extension_loaded('curl')) { + throw new Bitrix24Exception('cURL extension must be installed to use this library'); + } + if (!is_bool($isSaveRawResponse)) { + throw new Bitrix24Exception('isSaveRawResponse flag must be boolean'); + } + $this->isSaveRawResponse = $isSaveRawResponse; + if ($obLogger !== null) { + /** + * @var \Monolog\Logger + */ + $this->log = clone $obLogger; + } else { + // dev/null logger + /** + * @var \Monolog\Logger + */ + $this->log = new NullLogger(); + } + $this->setRetriesToConnectCount(1); + $this->setRetriesToConnectTimeout(1000000); + } + + /** + * Get a random string to sign protected api-call. Use salt for argument "state" in secure api-call + * random string is a result of mt_rand function + * + * @return int + */ + public function getSecuritySignSalt() + { + return mt_rand(); + } + + /** + * Set member ID — portal GUID + * + * @param string $memberId + * + * @throws Bitrix24Exception + * + * @return true + */ + public function setMemberId($memberId) + { + if ('' === $memberId) { + throw new Bitrix24Exception('memberId is empty'); + } elseif (null === $memberId) { + throw new Bitrix24Exception('memberId is null'); + } + $this->memberId = $memberId; + return true; + } + + /** + * Get memeber ID + * + * @return string | null + */ + public function getMemberId() + { + return $this->memberId; + } + + /** + * Set redirect URI + * + * @param string $redirectUri + * + * @throws Bitrix24Exception + * + * @return true; + */ + public function setRedirectUri($redirectUri) + { + if ('' === $redirectUri) { + throw new Bitrix24Exception('redirect URI is empty'); + } + $this->redirectUri = $redirectUri; + return true; + } + + /** + * Get redirect URI + * + * @return string | null + */ + public function getRedirectUri() + { + return $this->redirectUri; + } + + /** + * Set access token + * + * @param string $accessToken + * + * @throws Bitrix24Exception + * + * @return true + */ + public function setAccessToken($accessToken) + { + if ('' === $accessToken) { + throw new Bitrix24Exception('access token is empty'); + } + $this->accessToken = $accessToken; + return true; + } + + /** + * Get access token + * + * @return string | null + */ + public function getAccessToken() + { + return $this->accessToken; + } + + /** + * Set refresh token + * + * @param $refreshToken + * + * @throws Bitrix24Exception + * + * @return true; + */ + public function setRefreshToken($refreshToken) + { + if ('' === $refreshToken) { + throw new Bitrix24Exception('refresh token is empty'); + } + $this->refreshToken = $refreshToken; + return true; + } + + /** + * Get refresh token + * + * @return string + */ + public function getRefreshToken() + { + return $this->refreshToken; + } + + /** + * Set domain + * + * @param $domain + * + * @throws Bitrix24Exception + * + * @return true; + */ + public function setDomain($domain) + { + if ('' === $domain) { + throw new Bitrix24Exception('domain is empty'); + } + $this->domain = $domain; + return true; + } + + /** + * Get domain + * + * @return string | null + */ + public function getDomain() + { + return $this->domain; + } + + /** + * Set application scope + * + * @param array $applicationScope + * + * @return boolean + * + * @throws Bitrix24Exception + */ + public function setApplicationScope(array $applicationScope) + { + if (is_array($applicationScope) && count($applicationScope) > 0) { + $this->applicationScope = $applicationScope; + return true; + } else { + throw new Bitrix24Exception('application scope not set'); + } + } + + /** + * Get application scope + * + * @return string + */ + public function getApplicationScope() + { + return $this->applicationScope; + } + + /** + * Set application id + * + * @param string $applicationId + * + * @throws Bitrix24Exception + * + * @return true; + */ + public function setApplicationId($applicationId) + { + if ('' === $applicationId) { + throw new Bitrix24Exception('application id is empty'); + } + $this->applicationId = $applicationId; + return true; + }// end of SetApplicationId + + /** + * Get application id + * + * @return string + */ + public function getApplicationId() + { + return $this->applicationId; + } + + /** + * Set application secret + * + * @param string $applicationSecret + * + * @throws Bitrix24Exception + * + * @return true; + */ + public function setApplicationSecret($applicationSecret) + { + if ('' === $applicationSecret) { + throw new Bitrix24Exception('application secret is empty'); + } + $this->applicationSecret = $applicationSecret; + return true; + } + + /** + * Get application secret + * + * @return string + */ + public function getApplicationSecret() + { + return $this->applicationSecret; + } + + /** + * Set custom cURL options, overriding default ones + * + * @link http://php.net/manual/en/function.curl-setopt.php + * + * @param array $options - array(CURLOPT_XXX => value1, CURLOPT_XXX2 => value2,...) + * + * @return bool + */ + public function setCustomCurlOptions($options) + { + $this->customCurlOptions = $options; + + return true; + } + + /** + * Return raw request, contain all cURL options array and API query. Data available after you try to call method call + * numbers of array keys is const of cURL module. Example: CURLOPT_RETURNTRANSFER = 19913 + * + * @return array | null + */ + public function getRawRequest() + { + return $this->rawRequest; + } + + /** + * Return result from function curl_getinfo. Data available after you try to call method call + * + * @return array | null + */ + public function getRequestInfo() + { + return $this->requestInfo; + } + + /** + * Return additional parameters of last api-call. Data available after you try to call method call + * + * @return array | null + */ + public function getMethodParameters() + { + return $this->methodParameters; + } + + /** + * get error context + * + * @return array + */ + protected function getErrorContext() + { + return array( + // portal specific settings + 'B24_DOMAIN' => $this->getDomain(), + 'B24_MEMBER_ID' => $this->getMemberId(), + 'B24_ACCESS_TOKEN' => $this->getAccessToken(), + 'B24_REFRESH_TOKEN' => $this->getRefreshToken(), + // application settings + 'APPLICATION_SCOPE' => $this->getApplicationScope(), + 'APPLICATION_ID' => $this->getApplicationId(), + 'APPLICATION_SECRET' => $this->getApplicationSecret(), + 'REDIRECT_URI' => $this->getRedirectUri(), + // network + 'RAW_REQUEST' => $this->getRawRequest(), + 'CURL_REQUEST_INFO' => $this->getRequestInfo(), + 'RAW_RESPONSE' => $this->getRawResponse() + ); + } + + /** + * Execute a request API to Bitrix24 using cURL + * + * @param string $url + * @param array $additionalParameters + * + * @throws Bitrix24Exception + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * + * @return array + */ + protected function executeRequest($url, array $additionalParameters = array()) + { + $retryableErrorCodes = array( + CURLE_COULDNT_RESOLVE_HOST, + CURLE_COULDNT_CONNECT, + CURLE_HTTP_NOT_FOUND, + CURLE_READ_ERROR, + CURLE_OPERATION_TIMEOUTED, + CURLE_HTTP_POST_ERROR, + CURLE_SSL_CONNECT_ERROR + ); + + $curlOptions = array( + CURLOPT_RETURNTRANSFER => true, + CURLINFO_HEADER_OUT => true, + CURLOPT_VERBOSE => true, + CURLOPT_CONNECTTIMEOUT => 5, + CURLOPT_TIMEOUT => 5, + CURLOPT_USERAGENT => strtolower(__CLASS__ . '-PHP-SDK/v' . self::VERSION), + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => http_build_query($additionalParameters), + CURLOPT_URL => $url + ); + + if (is_array($this->customCurlOptions)) { + foreach ($this->customCurlOptions as $customCurlOptionKey => $customCurlOptionValue) { + $curlOptions[$customCurlOptionKey] = $customCurlOptionValue; + } + } + + $this->rawRequest = $curlOptions; + $curl = curl_init(); + curl_setopt_array($curl, $curlOptions); + + $curlResult = false; + $retriesCnt = $this->retriesToConnectCount; + while ($retriesCnt--) { + $this->log->debug(sprintf('try [%s] to connect to host [%s]', $retriesCnt, $this->getDomain())); + $curlResult = curl_exec($curl); + // handling network I/O errors + if (false === $curlResult) { + $curlErrorNumber = curl_errno($curl); + $errorMsg = sprintf('in try[%s] cURL error (code %s): %s' . PHP_EOL, $retriesCnt, $curlErrorNumber, + curl_error($curl)); + if (false === in_array($curlErrorNumber, $retryableErrorCodes, true) || !$retriesCnt) { + $this->log->error($errorMsg, $this->getErrorContext()); + curl_close($curl); + throw new Bitrix24IoException($errorMsg); + } else { + $this->log->warning($errorMsg, $this->getErrorContext()); + } + usleep($this->getRetriesToConnectTimeout()); + continue; + } + $this->requestInfo = curl_getinfo($curl); + $this->rawResponse = $curlResult; + $this->log->debug('cURL request info', array($this->getRequestInfo())); + curl_close($curl); + break; + } + + // handling URI level resource errors + switch ($this->requestInfo['http_code']) { + case 403: + $errorMsg = sprintf('portal [%s] deleted, query aborted', $this->getDomain()); + $this->log->error($errorMsg, $this->getErrorContext()); + throw new Bitrix24PortalDeletedException($errorMsg); + break; + } + + // handling server-side API errors: empty response from bitrix24 portal + if ($curlResult === '') { + $errorMsg = sprintf('empty response from portal [%s]', $this->getDomain()); + $this->log->error($errorMsg, $this->getErrorContext()); + throw new Bitrix24EmptyResponseException($errorMsg); + } + + // handling json_decode errors + $jsonResult = json_decode($curlResult, true); + unset($curlResult); + $jsonErrorCode = json_last_error(); + if (null === $jsonResult && (JSON_ERROR_NONE !== $jsonErrorCode)) { + /** + * @todo add function json_last_error_msg() + */ + $errorMsg = 'fatal error in function json_decode.' . PHP_EOL . 'Error code: ' . $jsonErrorCode . PHP_EOL; + $this->log->error($errorMsg, $this->getErrorContext()); + throw new Bitrix24Exception($errorMsg); + } + return $jsonResult; + } + + /** + * Execute Bitrix24 REST API method + * + * @param string $methodName + * @param array $additionalParameters + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * @throws Bitrix24SecurityException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * + * @return array + */ + public function call($methodName, array $additionalParameters = array()) + { // $arAuthServerMethods = array( // 'app.info', // 'app.stat' // ); - if(null === $this->getDomain()) - { - throw new Bitrix24Exception('domain not found, you must call setDomain method before'); - } - if(null === $this->getAccessToken()) - { - throw new Bitrix24Exception('access token not found, you must call setAccessToken method before'); - } - if('' === $methodName) - { - throw new Bitrix24Exception('method name not found, you must set method name'); - } + if (null === $this->getDomain()) { + throw new Bitrix24Exception('domain not found, you must call setDomain method before'); + } + if (null === $this->getAccessToken()) { + throw new Bitrix24Exception('access token not found, you must call setAccessToken method before'); + } + if ('' === $methodName) { + throw new Bitrix24Exception('method name not found, you must set method name'); + } // if(in_array(strtolower($methodName), $arAuthServerMethods, true)) // { @@ -626,210 +614,198 @@ public function call($methodName, array $additionalParameters = array()) // } // else // { - $url = 'https://'.$this->domain.'/rest/'.$methodName; + $url = 'https://' . $this->domain . '/rest/' . $methodName; // } - $additionalParameters['auth'] = $this->accessToken; - // save method parameters for debug - $this->methodParameters = $additionalParameters; - // is secure api-call? - $isSecureCall = false; - if(array_key_exists('state', $additionalParameters)) - { - $isSecureCall = true; - } - // execute request - $this->log->info('call bitrix24 method', array( - 'BITRIX24_DOMAIN' => $this->domain, - 'METHOD_NAME' => $methodName, - 'METHOD_PARAMETERS' => $additionalParameters - )); - $requestResult = $this->executeRequest($url, $additionalParameters); - // check errors and throw exception if errors exists - $this->handleBitrix24APILevelErrors($requestResult, $methodName, $additionalParameters); - // handling security sign for secure api-call - if($isSecureCall) - { - if(array_key_exists('signature', $requestResult)) - { - // check signature structure - if (strpos($requestResult['signature'], '.') === false) - { - throw new Bitrix24SecurityException('security signature is corrupted'); - } - if(null === $this->getMemberId()) - { - throw new Bitrix24Exception('member-id not found, you must call setMemberId method before'); - } - if(null === $this->getApplicationSecret()) - { - throw new Bitrix24Exception('application secret not found, you must call setApplicationSecret method before'); - } - // prepare - $key = md5($this->getMemberId().$this->getApplicationSecret()); - $delimiterPosition = strrpos($requestResult['signature'], '.'); - $dataToDecode = substr($requestResult['signature'], 0, $delimiterPosition); - $signature = base64_decode(substr($requestResult['signature'], $delimiterPosition + 1)); - // compare signatures - $hash = hash_hmac('sha256', $dataToDecode, $key, true); - if ($hash !== $signature) - { - throw new Bitrix24SecurityException('security signatures not same, bad request'); - } - // decode - $arClearData = json_decode(base64_decode($dataToDecode), true); - // handling json_decode errors - $jsonErrorCode = json_last_error(); - if(null === $arClearData && (JSON_ERROR_NONE !== $jsonErrorCode)) - { - /** - * @todo add function json_last_error_msg() - */ - $errorMsg = 'fatal error in function json_decode.'.PHP_EOL.'Error code: '.$jsonErrorCode.PHP_EOL; - throw new Bitrix24Exception($errorMsg); - } - // merge dirty and clear data - unset($arClearData['state']); - $requestResult ['result'] = array_merge($requestResult ['result'], $arClearData); - } - else - { - throw new Bitrix24SecurityException('security signature in api-response not found'); - } - } - return $requestResult; - } - - /** - * Handling bitrix24 api-level errors - * - * @param $arRequestResult - * @param $methodName - * @param array $additionalParameters - * - * @return null - * - * @throws Bitrix24ApiException - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24WrongClientException - * @throws Bitrix24MethodNotFoundException - */ - protected function handleBitrix24APILevelErrors($arRequestResult, $methodName, array $additionalParameters = array()) - { - if (array_key_exists('error', $arRequestResult)) - { - $errorMsg = sprintf('%s - %s in call [%s] for domain [%s]', - $arRequestResult['error'], - (array_key_exists('error_description', $arRequestResult) ? $arRequestResult['error_description'] : ''), - $methodName, - $this->getDomain()); - $this->log->error($errorMsg, $this->getErrorContext()); - // throw specific API-level exceptions - switch(strtoupper(trim($arRequestResult['error']))) - { - case 'WRONG_CLIENT': - throw new Bitrix24WrongClientException($errorMsg); - case 'ERROR_METHOD_NOT_FOUND': - throw new Bitrix24MethodNotFoundException($errorMsg); - case 'INVALID_TOKEN': - throw new Bitrix24TokenIsInvalid($errorMsg); - case 'EXPIRED_TOKEN': - throw new Bitrix24TokenIsExpired($errorMsg); - default: - throw new Bitrix24ApiException($errorMsg); - } - } - return null; - } - - /** - * Get raw response from Bitrix24 before json_decode call, method available only in debug mode. - * To activate debug mode you must before set to true flag isSaveRawResponse in class construct - * - * @return string | null - */ - public function getRawResponse() - { - return $this->rawResponse; - } - - /** - * Get new access token - * - * @return array - * - * @throws Bitrix24Exception - * @throws Bitrix24ApiException - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24WrongClientException - * @throws Bitrix24MethodNotFoundException - * @throws Bitrix24PortalDeleted - * @throws Bitrix24IoException - * @throws Bitrix24EmptyResponseException - * - */ - public function getNewAccessToken() - { - $applicationId = $this->getApplicationId(); - $applicationSecret = $this->getApplicationSecret(); - $refreshToken = $this->getRefreshToken(); - $applicationScope = $this->getApplicationScope(); - $redirectUri = $this->getRedirectUri(); - - if(null === $applicationId) - { - throw new Bitrix24Exception('application id not found, you must call setApplicationId method before'); - } - elseif(null === $applicationSecret) - { - throw new Bitrix24Exception('application id not found, you must call setApplicationSecret method before'); - } - elseif(null === $refreshToken) - { - throw new Bitrix24Exception('application id not found, you must call setRefreshToken method before'); - } - elseif(0 === count($applicationScope)) - { - throw new Bitrix24Exception('application scope not found, you must call setApplicationScope method before'); - } - elseif(null === $redirectUri) - { - throw new Bitrix24Exception('application redirect URI not found, you must call setRedirectUri method before'); - } + $additionalParameters['auth'] = $this->accessToken; + // save method parameters for debug + $this->methodParameters = $additionalParameters; + // is secure api-call? + $isSecureCall = false; + if (array_key_exists('state', $additionalParameters)) { + $isSecureCall = true; + } + // execute request + $this->log->info('call bitrix24 method', array( + 'BITRIX24_DOMAIN' => $this->domain, + 'METHOD_NAME' => $methodName, + 'METHOD_PARAMETERS' => $additionalParameters + )); + $requestResult = $this->executeRequest($url, $additionalParameters); + // check errors and throw exception if errors exists + $this->handleBitrix24APILevelErrors($requestResult, $methodName, $additionalParameters); + // handling security sign for secure api-call + if ($isSecureCall) { + if (array_key_exists('signature', $requestResult)) { + // check signature structure + if (strpos($requestResult['signature'], '.') === false) { + throw new Bitrix24SecurityException('security signature is corrupted'); + } + if (null === $this->getMemberId()) { + throw new Bitrix24Exception('member-id not found, you must call setMemberId method before'); + } + if (null === $this->getApplicationSecret()) { + throw new Bitrix24Exception('application secret not found, you must call setApplicationSecret method before'); + } + // prepare + $key = md5($this->getMemberId() . $this->getApplicationSecret()); + $delimiterPosition = strrpos($requestResult['signature'], '.'); + $dataToDecode = substr($requestResult['signature'], 0, $delimiterPosition); + $signature = base64_decode(substr($requestResult['signature'], $delimiterPosition + 1)); + // compare signatures + $hash = hash_hmac('sha256', $dataToDecode, $key, true); + if ($hash !== $signature) { + throw new Bitrix24SecurityException('security signatures not same, bad request'); + } + // decode + $arClearData = json_decode(base64_decode($dataToDecode), true); + // handling json_decode errors + $jsonErrorCode = json_last_error(); + if (null === $arClearData && (JSON_ERROR_NONE !== $jsonErrorCode)) { + /** + * @todo add function json_last_error_msg() + */ + $errorMsg = 'fatal error in function json_decode.' . PHP_EOL . 'Error code: ' . $jsonErrorCode . PHP_EOL; + throw new Bitrix24Exception($errorMsg); + } + // merge dirty and clear data + unset($arClearData['state']); + $requestResult ['result'] = array_merge($requestResult ['result'], $arClearData); + } else { + throw new Bitrix24SecurityException('security signature in api-response not found'); + } + } + return $requestResult; + } + + /** + * Handling bitrix24 api-level errors + * + * @param $arRequestResult + * @param $methodName + * @param array $additionalParameters + * + * @return null + * + * @throws Bitrix24ApiException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + */ + protected function handleBitrix24APILevelErrors( + $arRequestResult, + $methodName, + array $additionalParameters = array() + ) { + if (array_key_exists('error', $arRequestResult)) { + $errorMsg = sprintf('%s - %s in call [%s] for domain [%s]', + $arRequestResult['error'], + (array_key_exists('error_description', $arRequestResult) ? $arRequestResult['error_description'] : ''), + $methodName, + $this->getDomain()); + $this->log->error($errorMsg, $this->getErrorContext()); + // throw specific API-level exceptions + switch (strtoupper(trim($arRequestResult['error']))) { + case 'WRONG_CLIENT': + case 'ERROR_OAUTH': + throw new Bitrix24WrongClientException($errorMsg); + case 'ERROR_METHOD_NOT_FOUND': + throw new Bitrix24MethodNotFoundException($errorMsg); + case 'INVALID_TOKEN': + case 'INVALID_GRANT': + throw new Bitrix24TokenIsInvalidException($errorMsg); + case 'EXPIRED_TOKEN': + throw new Bitrix24TokenIsExpiredException($errorMsg); + case 'PAYMENT_REQUIRED': + throw new Bitrix24PaymentRequiredException($errorMsg); + default: + throw new Bitrix24ApiException($errorMsg); + } + } + return null; + } + + /** + * Get raw response from Bitrix24 before json_decode call, method available only in debug mode. + * To activate debug mode you must before set to true flag isSaveRawResponse in class construct + * + * @return string | null + */ + public function getRawResponse() + { + return $this->rawResponse; + } + + /** + * Get new access token + * + * @return array + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * + */ + public function getNewAccessToken() + { + $applicationId = $this->getApplicationId(); + $applicationSecret = $this->getApplicationSecret(); + $refreshToken = $this->getRefreshToken(); + $applicationScope = $this->getApplicationScope(); + $redirectUri = $this->getRedirectUri(); + + if (null === $applicationId) { + throw new Bitrix24Exception('application id not found, you must call setApplicationId method before'); + } elseif (null === $applicationSecret) { + throw new Bitrix24Exception('application id not found, you must call setApplicationSecret method before'); + } elseif (null === $refreshToken) { + throw new Bitrix24Exception('application id not found, you must call setRefreshToken method before'); + } elseif (0 === count($applicationScope)) { + throw new Bitrix24Exception('application scope not found, you must call setApplicationScope method before'); + } elseif (null === $redirectUri) { + throw new Bitrix24Exception('application redirect URI not found, you must call setRedirectUri method before'); + } // $url = 'https://'.self::OAUTH_SERVER.'/oauth/token/'. - $url = 'https://'.$this->getDomain().'/oauth/token/'. - '?client_id='.urlencode($applicationId). - '&grant_type=refresh_token'. - '&client_secret='.$applicationSecret. - '&refresh_token='.$refreshToken. - '&redirect_uri='.urlencode($redirectUri); - $requestResult = $this->executeRequest($url); - // handling bitrix24 api-level errors - $this->handleBitrix24APILevelErrors($requestResult, 'refresh access token'); - return $requestResult; - } - - /** - * Authorize and get first access token - * - * @param $code - * - * @return array - * - * @throws Bitrix24ApiException - * @throws Bitrix24Exception - * @throws Bitrix24IoException - * @throws Bitrix24MethodNotFoundException - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24WrongClientException - * @throws Bitrix24PortalDeleted - * @throws Bitrix24IoException - * @throws Bitrix24EmptyResponseException - * - */ + $url = 'https://' . $this->getDomain() . '/oauth/token/' . + '?client_id=' . urlencode($applicationId) . + '&grant_type=refresh_token' . + '&client_secret=' . $applicationSecret . + '&refresh_token=' . $refreshToken . + '&redirect_uri=' . urlencode($redirectUri); + $requestResult = $this->executeRequest($url); + // handling bitrix24 api-level errors + $this->handleBitrix24APILevelErrors($requestResult, 'refresh access token'); + return $requestResult; + } + + /** + * Authorize and get first access token + * + * @param $code + * + * @return array + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * + */ public function getFirstAccessToken($code) { $applicationId = $this->getApplicationId(); @@ -837,30 +813,23 @@ public function getFirstAccessToken($code) $applicationScope = $this->getApplicationScope(); $redirectUri = $this->getRedirectUri(); - if(null === $applicationId) - { + if (null === $applicationId) { throw new Bitrix24Exception('application id not found, you must call setApplicationId method before'); - } - elseif(null === $applicationSecret) - { + } elseif (null === $applicationSecret) { throw new Bitrix24Exception('application id not found, you must call setApplicationSecret method before'); - } - elseif(0 === count($applicationScope)) - { + } elseif (0 === count($applicationScope)) { throw new Bitrix24Exception('application scope not found, you must call setApplicationScope method before'); - } - elseif(null === $redirectUri) - { + } elseif (null === $redirectUri) { throw new Bitrix24Exception('application redirect URI not found, you must call setRedirectUri method before'); } // $url = 'https://'.self::OAUTH_SERVER.'/oauth/token/'. - $url = 'https://'.$this->getDomain().'/oauth/token/'. - '?client_id='.urlencode($applicationId). - '&grant_type=authorization_code'. - '&client_secret='.$applicationSecret. - '&redirect_uri='.urlencode($redirectUri). - '&code='.urlencode($code); + $url = 'https://' . $this->getDomain() . '/oauth/token/' . + '?client_id=' . urlencode($applicationId) . + '&grant_type=authorization_code' . + '&client_secret=' . $applicationSecret . + '&redirect_uri=' . urlencode($redirectUri) . + '&code=' . urlencode($code); $requestResult = $this->executeRequest($url); // handling bitrix24 api-level errors @@ -868,187 +837,167 @@ public function getFirstAccessToken($code) return $requestResult; } - /** - * Check is access token expire, call list of all available api-methods from B24 portal with current access token - * if we have an error code expired_token then return true else return false - * - * @throws Bitrix24Exception - * @throws Bitrix24ApiException - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24WrongClientException - * @throws Bitrix24MethodNotFoundException - * @throws Bitrix24PortalDeleted - * @throws Bitrix24IoException - * @throws Bitrix24EmptyResponseException - * - * @return boolean - */ - public function isAccessTokenExpire() - { - $isTokenExpire = false; - $accessToken = $this->getAccessToken(); - $domain = $this->getDomain(); - - if(null === $domain) - { - throw new Bitrix24Exception('domain not found, you must call setDomain method before'); - } - elseif(null === $accessToken) - { - throw new Bitrix24Exception('application id not found, you must call setAccessToken method before'); - } + /** + * Check is access token expire, call list of all available api-methods from B24 portal with current access token + * if we have an error code expired_token then return true else return false + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * + * @return boolean + */ + public function isAccessTokenExpire() + { + $isTokenExpire = false; + $accessToken = $this->getAccessToken(); + $domain = $this->getDomain(); + + if (null === $domain) { + throw new Bitrix24Exception('domain not found, you must call setDomain method before'); + } elseif (null === $accessToken) { + throw new Bitrix24Exception('application id not found, you must call setAccessToken method before'); + } // $url = 'https://'.self::OAUTH_SERVER.'/rest/app.info?auth='.$accessToken; - $url = 'https://'.$domain.'/rest/app.info?auth='.$accessToken; - $requestResult = $this->executeRequest($url); - if(in_array($requestResult['error'], array('expired_token', 'invalid_token', 'WRONG_TOKEN'), false)) - { - $isTokenExpire = true; - } - else - { - // handle other errors - $this->handleBitrix24APILevelErrors($requestResult, 'app.info'); - } - return $isTokenExpire; - }// end of isTokenExpire - - /** - * Get list of all methods available for current application - * - * @param array | null $applicationScope - * @param bool $isFull - * - * @return array - * - * @throws Bitrix24Exception - * @throws Bitrix24Exception - * @throws Bitrix24PortalDeleted - * @throws Bitrix24IoException - * @throws Bitrix24EmptyResponseException - */ - public function getAvailableMethods(array $applicationScope = array(), $isFull = false) - { - $accessToken = $this->getAccessToken(); - $domain = $this->getDomain(); - - if(null === $domain) - { - throw new Bitrix24Exception('domain not found, you must call setDomain method before'); - } - elseif(null === $accessToken) - { - throw new Bitrix24Exception('application id not found, you must call setAccessToken method before'); - } - - $showAll = ''; - if(TRUE === $isFull) - { - $showAll = '&full=true'; - } - $scope=''; - if(null === $applicationScope) - { - $scope = '&scope'; - } - elseif(count(array_unique($applicationScope)) > 0) - { - $scope = '&scope='.implode(',', array_map('urlencode', array_unique($applicationScope))); - } - $url = 'https://'.$domain.'/rest/methods.json?auth='.$accessToken.$showAll.$scope; - return $this->executeRequest($url); - } - - /** - * get list of scope for current application from bitrix24 api - * - * @param bool $isFull - * - * @throws Bitrix24Exception - * @throws Bitrix24Exception - * @throws Bitrix24PortalDeleted - * @throws Bitrix24IoException - * @throws Bitrix24EmptyResponseException - * - * @return array - */ - public function getScope($isFull=false) - { - $accessToken = $this->getAccessToken(); - $domain = $this->getDomain(); - - if(null === $domain) - { - throw new Bitrix24Exception('domain not found, you must call setDomain method before'); - } - elseif(null === $accessToken) - { - throw new Bitrix24Exception('application id not found, you must call setAccessToken method before'); - } - $showAll = ''; - if(TRUE === $isFull) - { - $showAll = '&full=true'; - } - $url = 'https://'.$domain.'/rest/scope.json?auth='.$accessToken.$showAll; - return $this->executeRequest($url); - } - - /** - * set CURL request count retries - * @param $retriesCnt - * - * @return boolean - * - * @throws Bitrix24Exception - */ - public function setRetriesToConnectCount($retriesCnt = 1) - { - $this->log->debug(sprintf('set retries to connect count %s', $retriesCnt)); - if(!is_int($retriesCnt)) - { - throw new Bitrix24Exception('retries to connect count must be an integer'); - } - $this->retriesToConnectCount = (int) $retriesCnt; - return true; - } - - /** - * set retries to connect timeout in microseconds - * @param int $microseconds - - * @return bool - - * @throws Bitrix24Exception - */ - public function setRetriesToConnectTimeout($microseconds = 1000000) - { - $this->log->debug(sprintf('set retries to connect count %s', $microseconds)); - if(!is_numeric($microseconds)) - { - throw new Bitrix24Exception('retries to connect count must be an integer'); - } - $this->retriesToConnectTimeout = $microseconds; - return true; - } - - /** - * get CURL request count retries - * - * @return int - */ - public function getRetriesToConnectCount() - { - return $this->retriesToConnectCount; - } - - /** - * get retries to connect timeout in microseconds - * - * @return mixed - */ - public function getRetriesToConnectTimeout() - { - return $this->retriesToConnectTimeout; - } + $url = 'https://' . $domain . '/rest/app.info?auth=' . $accessToken; + $requestResult = $this->executeRequest($url); + if (in_array($requestResult['error'], array('expired_token', 'invalid_token', 'WRONG_TOKEN'), false)) { + $isTokenExpire = true; + } else { + // handle other errors + $this->handleBitrix24APILevelErrors($requestResult, 'app.info'); + } + return $isTokenExpire; + }// end of isTokenExpire + + /** + * Get list of all methods available for current application + * + * @param array | null $applicationScope + * @param bool $isFull + * + * @return array + * + * @throws Bitrix24Exception + * @throws Bitrix24Exception + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + */ + public function getAvailableMethods(array $applicationScope = array(), $isFull = false) + { + $accessToken = $this->getAccessToken(); + $domain = $this->getDomain(); + + if (null === $domain) { + throw new Bitrix24Exception('domain not found, you must call setDomain method before'); + } elseif (null === $accessToken) { + throw new Bitrix24Exception('application id not found, you must call setAccessToken method before'); + } + + $showAll = ''; + if (true === $isFull) { + $showAll = '&full=true'; + } + $scope = ''; + if (null === $applicationScope) { + $scope = '&scope'; + } elseif (count(array_unique($applicationScope)) > 0) { + $scope = '&scope=' . implode(',', array_map('urlencode', array_unique($applicationScope))); + } + $url = 'https://' . $domain . '/rest/methods.json?auth=' . $accessToken . $showAll . $scope; + return $this->executeRequest($url); + } + + /** + * get list of scope for current application from bitrix24 api + * + * @param bool $isFull + * + * @throws Bitrix24Exception + * @throws Bitrix24Exception + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * + * @return array + */ + public function getScope($isFull = false) + { + $accessToken = $this->getAccessToken(); + $domain = $this->getDomain(); + + if (null === $domain) { + throw new Bitrix24Exception('domain not found, you must call setDomain method before'); + } elseif (null === $accessToken) { + throw new Bitrix24Exception('application id not found, you must call setAccessToken method before'); + } + $showAll = ''; + if (true === $isFull) { + $showAll = '&full=true'; + } + $url = 'https://' . $domain . '/rest/scope.json?auth=' . $accessToken . $showAll; + return $this->executeRequest($url); + } + + /** + * set CURL request count retries + * @param $retriesCnt + * + * @return boolean + * + * @throws Bitrix24Exception + */ + public function setRetriesToConnectCount($retriesCnt = 1) + { + $this->log->debug(sprintf('set retries to connect count %s', $retriesCnt)); + if (!is_int($retriesCnt)) { + throw new Bitrix24Exception('retries to connect count must be an integer'); + } + $this->retriesToConnectCount = (int)$retriesCnt; + return true; + } + + /** + * set retries to connect timeout in microseconds + * @param int $microseconds + * @return bool + * @throws Bitrix24Exception + */ + public function setRetriesToConnectTimeout($microseconds = 1000000) + { + $this->log->debug(sprintf('set retries to connect count %s', $microseconds)); + if (!is_numeric($microseconds)) { + throw new Bitrix24Exception('retries to connect count must be an integer'); + } + $this->retriesToConnectTimeout = $microseconds; + return true; + } + + /** + * get CURL request count retries + * + * @return int + */ + public function getRetriesToConnectCount() + { + return $this->retriesToConnectCount; + } + + /** + * get retries to connect timeout in microseconds + * + * @return mixed + */ + public function getRetriesToConnectTimeout() + { + return $this->retriesToConnectTimeout; + } } \ No newline at end of file diff --git a/src/bitrix24exception.php b/src/bitrix24exception.php index f3cef761..81292f1f 100644 --- a/src/bitrix24exception.php +++ b/src/bitrix24exception.php @@ -15,13 +15,73 @@ * \Bitrix24SecurityException — Security errors for protected methods */ namespace Bitrix24; + +/** + * Class Bitrix24Exception + * @package Bitrix24 + * @deprecated use \Bitrix24\Exceptions\Bitrix24Exception + */ class Bitrix24Exception extends \Exception {} + +/** + * Class Bitrix24IoException + * @package Bitrix24 + * @deprecated use \Bitrix24\Exceptions\Bitrix24IoException + */ class Bitrix24IoException extends Bitrix24Exception {} + +/** + * Class Bitrix24EmptyResponseException + * @package Bitrix24 + * @deprecated use \Bitrix24\Exceptions\Bitrix24EmptyResponseException + */ class Bitrix24EmptyResponseException extends Bitrix24IoException {} + +/** + * Class Bitrix24ApiException + * @package Bitrix24 + * @deprecated use \Bitrix24\Exceptions\Bitrix24ApiException + */ class Bitrix24ApiException extends Bitrix24Exception {} + +/** + * Class Bitrix24WrongClientException + * @package Bitrix24 + * @deprecated use \Bitrix24\Exceptions\Bitrix24WrongClientException + */ class Bitrix24WrongClientException extends Bitrix24ApiException {} + +/** + * Class Bitrix24MethodNotFoundException + * @package Bitrix24 + * @deprecated use \Bitrix24\Exceptions\Bitrix24MethodNotFoundException + */ class Bitrix24MethodNotFoundException extends Bitrix24ApiException {} + +/** + * Class Bitrix24TokenIsInvalid + * @package Bitrix24 + * @deprecated use \Bitrix24\Exceptions\Bitrix24TokenIsInvalidException + */ class Bitrix24TokenIsInvalid extends Bitrix24ApiException {} + +/** + * Class Bitrix24TokenIsExpired + * @package Bitrix24 + * @deprecated use \Bitrix24\Exceptions\Bitrix24TokenIsExpiredException + */ class Bitrix24TokenIsExpired extends Bitrix24ApiException {} + +/** + * Class Bitrix24PortalDeleted + * @package Bitrix24 + * @deprecated use \Bitrix24\Exceptions\Bitrix24PortalDeletedException + */ class Bitrix24PortalDeleted extends Bitrix24ApiException {} + +/** + * Class Bitrix24SecurityException + * @package Bitrix24 + * @deprecated use \Bitrix24\Exceptions\Bitrix24SecurityException + */ class Bitrix24SecurityException extends Bitrix24Exception {} \ No newline at end of file diff --git a/src/contracts/ibitrix24.php b/src/contracts/ibitrix24.php index 31f63566..f980caa7 100644 --- a/src/contracts/ibitrix24.php +++ b/src/contracts/ibitrix24.php @@ -8,16 +8,19 @@ namespace Bitrix24\Contracts; use Bitrix24\Bitrix24; -use Bitrix24\Bitrix24Exception; -use Bitrix24\Bitrix24IoException; -use Bitrix24\Bitrix24ApiException; -use Bitrix24\Bitrix24WrongClientException; -use Bitrix24\Bitrix24MethodNotFoundException; -use Bitrix24\Bitrix24TokenIsInvalid; -use Bitrix24\Bitrix24TokenIsExpired; -use Bitrix24\Bitrix24SecurityException; - -use Bitrix24\Stub\Logger; +use Bitrix24\Exceptions\Bitrix24Exception; +use Bitrix24\Exceptions\Bitrix24IoException; +use Bitrix24\Exceptions\Bitrix24PaymentRequiredException; +use Bitrix24\Exceptions\Bitrix24EmptyResponseException; +use Bitrix24\Exceptions\Bitrix24ApiException; +use Bitrix24\Exceptions\Bitrix24TokenIsInvalidException; +use Bitrix24\Exceptions\Bitrix24WrongClientException; +use Bitrix24\Exceptions\Bitrix24MethodNotFoundException; +use Bitrix24\Exceptions\Bitrix24TokenIsExpiredException; +use Bitrix24\Exceptions\Bitrix24PortalDeletedException; +use Bitrix24\Exceptions\Bitrix24SecurityException; + + use Psr\Log\LoggerInterface; /** @@ -176,19 +179,26 @@ public function getRequestInfo(); */ public function getMethodParameters(); - /** - * Execute Bitrix24 REST API method - * @param string $methodName - * @param array $additionalParameters - * @throws Bitrix24Exception - * @throws Bitrix24ApiException - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24WrongClientException - * @throws Bitrix24MethodNotFoundException - * @throws Bitrix24SecurityException - * @return array - */ + /** + * Execute Bitrix24 REST API method + * + * @param string $methodName + * @param array $additionalParameters + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * @throws Bitrix24SecurityException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * + * @return array + */ public function call($methodName, array $additionalParameters = array()); /** @@ -199,43 +209,63 @@ public function call($methodName, array $additionalParameters = array()); */ public function getRawResponse(); - /** - * Get new access token - * @return array - * @throws Bitrix24Exception - * @throws Bitrix24ApiException - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24WrongClientException - * @throws Bitrix24MethodNotFoundException - */ + /** + * Get new access token + * + * @return array + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * + */ public function getNewAccessToken(); - /** - * Authorize and get first access token - * @param $code - * @return array - * @throws Bitrix24ApiException - * @throws Bitrix24Exception - * @throws Bitrix24IoException - * @throws Bitrix24MethodNotFoundException - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24WrongClientException - */ + /** + * Authorize and get first access token + * + * @param $code + * + * @return array + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * + */ public function getFirstAccessToken($code); - /** - * Check is access token expire, call list of all available api-methods from B24 portal with current access token - * if we have an error code expired_token then return true else return false - * @throws Bitrix24Exception - * @throws Bitrix24ApiException - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24WrongClientException - * @throws Bitrix24MethodNotFoundException - * @return boolean - */ + /** + * Check is access token expire, call list of all available api-methods from B24 portal with current access token + * if we have an error code expired_token then return true else return false + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * + * @return boolean + */ public function isAccessTokenExpire(); /** diff --git a/src/exceptions/bitrix24apiexception.php b/src/exceptions/bitrix24apiexception.php new file mode 100644 index 00000000..04c2b405 --- /dev/null +++ b/src/exceptions/bitrix24apiexception.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Bitrix24\Exceptions; + +/** + * Class Bitrix24PaymentRequiredException + * @package Bitrix24 + */ +class Bitrix24ApiException extends Bitrix24Exception +{ +} \ No newline at end of file diff --git a/src/exceptions/bitrix24emptyresponseexception.php b/src/exceptions/bitrix24emptyresponseexception.php new file mode 100644 index 00000000..1342d227 --- /dev/null +++ b/src/exceptions/bitrix24emptyresponseexception.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Bitrix24\Exceptions; + +/** + * Class Bitrix24IOException + * @package Bitrix24 + */ +class Bitrix24EmptyResponseException extends Bitrix24IoException +{ +} \ No newline at end of file diff --git a/src/exceptions/bitrix24exception.php b/src/exceptions/bitrix24exception.php new file mode 100644 index 00000000..9b867bc3 --- /dev/null +++ b/src/exceptions/bitrix24exception.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Bitrix24\Exceptions; + +/** + * Class Bitrix24Exception + * + * \Exception + * \Bitrix24Exception — base class + * \Bitrix24IoException — I/O network errors + * \Bitrix24EmptyResponseException — empty response from Bitrix24 portal + * \Bitrix24ApiException — API level errors + * \Bitrix24WrongClientException — Wrong client or application will be deleted from portal + * \Bitrix24MethodNotFoundException — API-method not found + * \Bitrix24TokenIsInvalidException — The access token provided is invalid + * \Bitrix24TokenIsExpiredException — The access token provided has expired + * \Bitrix24PortalDeletedException — Bitrix24 portal deleted + * \Bitrix24PaymentRequiredException — Bitrix24 application payment required + * \Bitrix24SecurityException — Security errors for protected methods + * + * @package Bitrix24\Exceptions + */ +class Bitrix24Exception extends \Exception +{ +} \ No newline at end of file diff --git a/src/exceptions/bitrix24ioexception.php b/src/exceptions/bitrix24ioexception.php new file mode 100644 index 00000000..5817969b --- /dev/null +++ b/src/exceptions/bitrix24ioexception.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Bitrix24\Exceptions; + +/** + * Class Bitrix24IoException + * @package Bitrix24 + */ +class Bitrix24IoException extends Bitrix24Exception +{ +} \ No newline at end of file diff --git a/src/exceptions/bitrix24methodnotfoundexception.php b/src/exceptions/bitrix24methodnotfoundexception.php new file mode 100644 index 00000000..78116a25 --- /dev/null +++ b/src/exceptions/bitrix24methodnotfoundexception.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Bitrix24\Exceptions; + +/** + * Class Bitrix24WrongClientException + * @package Bitrix24 + */ +class Bitrix24MethodNotFoundException extends Bitrix24ApiException +{ +} \ No newline at end of file diff --git a/src/exceptions/bitrix24paymentrequiredexception.php b/src/exceptions/bitrix24paymentrequiredexception.php new file mode 100644 index 00000000..dce1d1a0 --- /dev/null +++ b/src/exceptions/bitrix24paymentrequiredexception.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Bitrix24\Exceptions; + +/** + * Class Bitrix24PaymentRequiredException + * @package Bitrix24 + */ +class Bitrix24PaymentRequiredException extends Bitrix24ApiException +{ +} \ No newline at end of file diff --git a/src/exceptions/bitrix24portaldeletedexception.php b/src/exceptions/bitrix24portaldeletedexception.php new file mode 100644 index 00000000..9001a5cc --- /dev/null +++ b/src/exceptions/bitrix24portaldeletedexception.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Bitrix24\Exceptions; + +/** + * Class Bitrix24PortalDeletedException + * @package Bitrix24 + */ +class Bitrix24PortalDeletedException extends Bitrix24ApiException +{ +} \ No newline at end of file diff --git a/src/exceptions/bitrix24securityexception.php b/src/exceptions/bitrix24securityexception.php new file mode 100644 index 00000000..aeda62ce --- /dev/null +++ b/src/exceptions/bitrix24securityexception.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Bitrix24\Exceptions; + +/** + * Class Bitrix24SecurityException + * @package Bitrix24 + */ +class Bitrix24SecurityException extends Bitrix24Exception +{ +} \ No newline at end of file diff --git a/src/exceptions/bitrix24tokenisexpiredexception.php b/src/exceptions/bitrix24tokenisexpiredexception.php new file mode 100644 index 00000000..9c6fa130 --- /dev/null +++ b/src/exceptions/bitrix24tokenisexpiredexception.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Bitrix24\Exceptions; + +/** + * Class Bitrix24TokenIsExpiredException + * @package Bitrix24 + */ +class Bitrix24TokenIsExpiredException extends Bitrix24ApiException +{ +} \ No newline at end of file diff --git a/src/exceptions/bitrix24tokenisinvalidexception.php b/src/exceptions/bitrix24tokenisinvalidexception.php new file mode 100644 index 00000000..28c18571 --- /dev/null +++ b/src/exceptions/bitrix24tokenisinvalidexception.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Bitrix24\Exceptions; + +/** + * Class Bitrix24TokenIsInvalidException + * @package Bitrix24 + */ +class Bitrix24TokenIsInvalidException extends Bitrix24ApiException +{ +} \ No newline at end of file diff --git a/src/exceptions/bitrix24wrongclientexception.php b/src/exceptions/bitrix24wrongclientexception.php new file mode 100644 index 00000000..92c280a9 --- /dev/null +++ b/src/exceptions/bitrix24wrongclientexception.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Bitrix24\Exceptions; + +/** + * Class Bitrix24WrongClientException + * @package Bitrix24 + */ +class Bitrix24WrongClientException extends Bitrix24ApiException +{ +} \ No newline at end of file diff --git a/src/stub/bitrix24.php b/src/stub/bitrix24.php index 2b99b576..ceeadaf8 100644 --- a/src/stub/bitrix24.php +++ b/src/stub/bitrix24.php @@ -4,14 +4,17 @@ use Bitrix24\Contracts\iBitrix24; use Psr\Log\LoggerInterface; -use Bitrix24\Bitrix24Exception; -use Bitrix24\Bitrix24IoException; -use Bitrix24\Bitrix24ApiException; -use Bitrix24\Bitrix24WrongClientException; -use Bitrix24\Bitrix24MethodNotFoundException; -use Bitrix24\Bitrix24TokenIsInvalid; -use Bitrix24\Bitrix24TokenIsExpired; -use Bitrix24\Bitrix24SecurityException; +use Bitrix24\Exceptions\Bitrix24Exception; +use Bitrix24\Exceptions\Bitrix24IoException; +use Bitrix24\Exceptions\Bitrix24PaymentRequiredException; +use Bitrix24\Exceptions\Bitrix24EmptyResponseException; +use Bitrix24\Exceptions\Bitrix24ApiException; +use Bitrix24\Exceptions\Bitrix24TokenIsInvalidException; +use Bitrix24\Exceptions\Bitrix24WrongClientException; +use Bitrix24\Exceptions\Bitrix24MethodNotFoundException; +use Bitrix24\Exceptions\Bitrix24TokenIsExpiredException; +use Bitrix24\Exceptions\Bitrix24PortalDeletedException; +use Bitrix24\Exceptions\Bitrix24SecurityException; /** * Class Bitrix24 @@ -266,22 +269,26 @@ public function getMethodParameters() { } - /** - * Execute Bitrix24 REST API method - * - * @param string $methodName - * @param array $additionalParameters - * - * @throws Bitrix24Exception - * @throws Bitrix24ApiException - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24WrongClientException - * @throws Bitrix24MethodNotFoundException - * @throws Bitrix24SecurityException - * - * @return array - */ + /** + * Execute Bitrix24 REST API method + * + * @param string $methodName + * @param array $additionalParameters + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * @throws Bitrix24SecurityException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * + * @return array + */ public function call($methodName, array $additionalParameters = array()) { } @@ -298,54 +305,67 @@ public function getRawResponse() { } - /** - * Get new access token - * - * @return array - * - * @throws Bitrix24Exception - * @throws Bitrix24ApiException - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24WrongClientException - * @throws Bitrix24MethodNotFoundException - */ + /** + * Get new access token + * + * @return array + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * + */ public function getNewAccessToken() { } - /** - * Authorize and get first access token - * - * @param $code - * - * @return array - * - * @throws Bitrix24ApiException - * @throws Bitrix24Exception - * @throws Bitrix24IoException - * @throws Bitrix24MethodNotFoundException - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24WrongClientException - */ + /** + * Authorize and get first access token + * + * @param $code + * + * @return array + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * + */ public function getFirstAccessToken($code) { } - /** - * Check is access token expire, call list of all available api-methods from B24 portal with current access token - * if we have an error code expired_token then return true else return false - * - * @throws Bitrix24Exception - * @throws Bitrix24ApiException - * @throws Bitrix24TokenIsInvalid - * @throws Bitrix24TokenIsExpired - * @throws Bitrix24WrongClientException - * @throws Bitrix24MethodNotFoundException - * - * @return boolean - */ + /** + * Check is access token expire, call list of all available api-methods from B24 portal with current access token + * if we have an error code expired_token then return true else return false + * + * @throws Bitrix24Exception + * @throws Bitrix24ApiException + * @throws Bitrix24PortalDeletedException + * @throws Bitrix24IoException + * @throws Bitrix24EmptyResponseException + * @throws Bitrix24TokenIsInvalidException + * @throws Bitrix24TokenIsExpiredException + * @throws Bitrix24WrongClientException + * @throws Bitrix24MethodNotFoundException + * @throws Bitrix24PaymentRequiredException + * + * @return boolean + */ public function isAccessTokenExpire() { } diff --git a/tests/src/Bitrix24Test.php b/tests/src/Bitrix24Test.php index af9d8d85..5b1198d2 100644 --- a/tests/src/Bitrix24Test.php +++ b/tests/src/Bitrix24Test.php @@ -7,7 +7,6 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ - namespace Bitrix24; use \Psr\Log\NullLogger; @@ -23,7 +22,7 @@ class Bitrix24Test extends \PHPUnit_Framework_TestCase */ public function testConstructorWithFirstArgumentIsNotBoolean() { - $this->setExpectedException('\Bitrix24\Bitrix24Exception'); + $this->setExpectedException('\Bitrix24\Exceptions\Bitrix24Exception'); $obBitrix24 = new Bitrix24(array()); } /** @@ -54,7 +53,7 @@ public function testSetMemberIdWithValidArgument() */ public function testSetMemberIdWithNullArgument() { - $this->setExpectedException('\Bitrix24\Bitrix24Exception'); + $this->setExpectedException('\Bitrix24\Exceptions\Bitrix24Exception'); $obBitrix24 = new Bitrix24(false, new NullLogger()); $result = $obBitrix24->setMemberId(null); $this->assertTrue($result); @@ -64,7 +63,7 @@ public function testSetMemberIdWithNullArgument() */ public function testSetMemberIdWithEmptyStringArgument() { - $this->setExpectedException('\Bitrix24\Bitrix24Exception'); + $this->setExpectedException('\Bitrix24\Exceptions\Bitrix24Exception'); $obBitrix24 = new Bitrix24(false, new NullLogger()); $result = $obBitrix24->setMemberId(''); $this->assertTrue($result); @@ -74,7 +73,7 @@ public function testSetMemberIdWithEmptyStringArgument() */ public function testSetRetriesToConnectCountWithNull() { - $this->setExpectedException('\Bitrix24\Bitrix24Exception'); + $this->setExpectedException('\Bitrix24\Exceptions\Bitrix24Exception'); $obBitrix24 = new Bitrix24(false, new NullLogger()); $result = $obBitrix24->setRetriesToConnectCount(null); } @@ -101,7 +100,7 @@ public function testSetRetriesToConnectCountWithValidArgs() */ public function testSetRetriesToConnectTimeoutWithNull() { - $this->setExpectedException('\Bitrix24\Bitrix24Exception'); + $this->setExpectedException('\Bitrix24\Exceptions\Bitrix24Exception'); $obBitrix24 = new Bitrix24(false, new NullLogger()); $result = $obBitrix24->setRetriesToConnectTimeout(null); }