Skip to content

Commit

Permalink
Merge pull request #133 from mesilov/task#132
Browse files Browse the repository at this point in the history
Task#132
  • Loading branch information
mesilov authored Mar 19, 2019
2 parents 924995d + 6de93a6 commit 4d97533
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 27 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
# bitrix24-php-sdk change log
## 0.6.1 (20.03.2019)
* add `offset` parameter to entity `CRM\Status\Status` in method `getList`
* add `offset` parameter to entity `User\User` in method `getList`
* add method `messageAdd` to entity `Bitrix24\Bitrix24`
* add method `setEnabledSslVerify` to entity `Classes\Im\Im`
* add entity `Bitrix24\Bizproc\Provider`
* add entity `Bitrix24\CRM\Lead\ProductRows` class to work with products
* fix error in method `crm.company.update`
* fix error in method `Bitrix24::getNewAccessToken`
* fix error in method `Bizproc\Robot::add`
* fix log level in method `Bitrix24::handleBitrix24APILevelErrors`

## 0.6.0 (18.02.2018)
* add support for `FaceTracker` entity
* add presets for request timing information
Expand Down
83 changes: 56 additions & 27 deletions src/bitrix24.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

/**
* Class Bitrix24
* @author Mesilov Maxim <[email protected]>
*
* @author Mesilov Maxim <[email protected]>
* @copyright 2013 - 2016 Mesilov Maxim
*/
class Bitrix24 implements iBitrix24
Expand Down Expand Up @@ -146,13 +147,13 @@ class Bitrix24 implements iBitrix24
* @var bool ssl verify for checking CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST
*/
protected $sslVerify = true;


/**
* 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
* @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
*
Expand Down Expand Up @@ -279,6 +280,7 @@ public function getNewAccessToken()
$requestResult = $this->executeRequest($url);
// handling bitrix24 api-level errors
$this->handleBitrix24APILevelErrors($requestResult, 'refresh access token');

return $requestResult;
}

Expand Down Expand Up @@ -307,6 +309,7 @@ public function setApplicationId($applicationId)
throw new Bitrix24Exception('application id is empty');
}
$this->applicationId = $applicationId;

return true;
}

Expand Down Expand Up @@ -335,6 +338,7 @@ public function setApplicationSecret($applicationSecret)
throw new Bitrix24Exception('application secret is empty');
}
$this->applicationSecret = $applicationSecret;

return true;
}

Expand Down Expand Up @@ -363,6 +367,7 @@ public function setRefreshToken($refreshToken)
throw new Bitrix24Exception('refresh token is empty');
}
$this->refreshToken = $refreshToken;

return true;
}

Expand All @@ -389,6 +394,7 @@ 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');
Expand Down Expand Up @@ -420,6 +426,7 @@ public function setRedirectUri($redirectUri)
throw new Bitrix24Exception('redirect URI is empty');
}
$this->redirectUri = $redirectUri;

return true;
}// end of SetApplicationId

Expand Down Expand Up @@ -448,30 +455,31 @@ public function setDomain($domain)
throw new Bitrix24Exception('domain is empty');
}
$this->domain = $domain;

return true;
}

/**
* disable of checking CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST
*/
public function setDisabledSslVerify ()
public function setDisabledSslVerify()
{
$this->sslVerify = false;
}

/**
* enable of checking CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST
*/
public function setEnabledSslVerify ()
public function setEnabledSslVerify()
{
$this->sslVerify = true;
}

/**
* Execute a request API to Bitrix24 using cURL
*
* @param string $url
* @param array $additionalParameters
* @param array $additionalParameters
*
* @throws Bitrix24Exception
* @throws Bitrix24PortalDeletedException
Expand All @@ -490,7 +498,7 @@ protected function executeRequest($url, array $additionalParameters = array())
CURLE_READ_ERROR,
CURLE_OPERATION_TIMEOUTED,
CURLE_HTTP_POST_ERROR,
CURLE_SSL_CONNECT_ERROR
CURLE_SSL_CONNECT_ERROR,
);

$curlOptions = array(
Expand All @@ -503,11 +511,10 @@ protected function executeRequest($url, array $additionalParameters = array())
CURLOPT_USERAGENT => strtolower(__CLASS__ . '-PHP-SDK/v' . self::VERSION),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($additionalParameters),
CURLOPT_URL => $url
CURLOPT_URL => $url,
);

if (!$this->sslVerify)
{

if (!$this->sslVerify) {
$curlOptions[CURLOPT_SSL_VERIFYPEER] = 0;
$curlOptions[CURLOPT_SSL_VERIFYHOST] = 0;
}
Expand Down Expand Up @@ -583,6 +590,7 @@ protected function executeRequest($url, array $additionalParameters = array())
$this->log->error($errorMsg, $this->getErrorContext());
throw new Bitrix24Exception($errorMsg);
}

return $jsonResult;
}

Expand All @@ -607,7 +615,7 @@ protected function getErrorContext()
// network
'RAW_REQUEST' => $this->getRawRequest(),
'CURL_REQUEST_INFO' => $this->getRequestInfo(),
'RAW_RESPONSE' => $this->getRawResponse()
'RAW_RESPONSE' => $this->getRawResponse(),
);
}

Expand Down Expand Up @@ -638,6 +646,7 @@ public function setMemberId($memberId)
throw new Bitrix24Exception('memberId is null');
}
$this->memberId = $memberId;

return true;
}

Expand Down Expand Up @@ -666,6 +675,7 @@ public function setAccessToken($accessToken)
throw new Bitrix24Exception('access token is empty');
}
$this->accessToken = $accessToken;

return true;
}

Expand Down Expand Up @@ -713,7 +723,9 @@ public function getRetriesToConnectTimeout()

/**
* set retries to connect timeout in microseconds
*
* @param int $microseconds
*
* @return bool
* @throws Bitrix24Exception
*/
Expand All @@ -724,14 +736,15 @@ public function setRetriesToConnectTimeout($microseconds = 1000000)
throw new Bitrix24Exception('retries to connect count must be an integer');
}
$this->retriesToConnectTimeout = $microseconds;

return true;
}

/**
* Handling bitrix24 api-level errors
*
* @param $arRequestResult
* @param $methodName
* @param $arRequestResult
* @param $methodName
* @param array $additionalParameters
*
* @return null
Expand All @@ -757,29 +770,37 @@ protected function handleBitrix24APILevelErrors(
(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':
$this->log->error($errorMsg, $this->getErrorContext());
throw new Bitrix24WrongClientException($errorMsg);
case 'ERROR_METHOD_NOT_FOUND':
$this->log->error($errorMsg, $this->getErrorContext());
throw new Bitrix24MethodNotFoundException($errorMsg);
case 'INVALID_TOKEN':
case 'INVALID_GRANT':
$this->log->error($errorMsg, $this->getErrorContext());
throw new Bitrix24TokenIsInvalidException($errorMsg);
case 'EXPIRED_TOKEN':
$this->log->notice($errorMsg, $this->getErrorContext());
throw new Bitrix24TokenIsExpiredException($errorMsg);
case 'PAYMENT_REQUIRED':
$this->log->error($errorMsg, $this->getErrorContext());
throw new Bitrix24PaymentRequiredException($errorMsg);
case 'NO_AUTH_FOUND':
$this->log->error($errorMsg, $this->getErrorContext());
throw new Bitrix24PortalRenamedException($errorMsg);
case 'INSUFFICIENT_SCOPE':
$this->log->error($errorMsg, $this->getErrorContext());
throw new Bitrix24InsufficientScope($errorMsg);
default:
$this->log->error($errorMsg, $this->getErrorContext());
throw new Bitrix24ApiException($errorMsg);
}
}

return null;
}

Expand Down Expand Up @@ -830,6 +851,7 @@ public function getFirstAccessToken($code)
$requestResult = $this->executeRequest($url);
// handling bitrix24 api-level errors
$this->handleBitrix24APILevelErrors($requestResult, 'get first access token');

return $requestResult;
}

Expand Down Expand Up @@ -873,14 +895,15 @@ public function isAccessTokenExpire()
$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
* @param bool $isFull
*
* @return array
*
Expand Down Expand Up @@ -913,6 +936,7 @@ public function getAvailableMethods(array $applicationScope = array(), $isFull =
$scope = '&scope=' . implode(',', array_map('urlencode', array_unique($applicationScope)));
}
$url = 'https://' . $domain . '/rest/methods.json?auth=' . $accessToken . $showAll . $scope;

return $this->executeRequest($url);
}

Expand Down Expand Up @@ -945,6 +969,7 @@ public function getScope($isFull = false)
$showAll = '&full=true';
}
$url = 'https://' . $domain . '/rest/scope.json?auth=' . $accessToken . $showAll;

return $this->executeRequest($url);
}

Expand All @@ -960,6 +985,7 @@ public function getRetriesToConnectCount()

/**
* set CURL request count retries
*
* @param $retriesCnt
*
* @return boolean
Expand All @@ -973,14 +999,15 @@ public function setRetriesToConnectCount($retriesCnt = 1)
throw new Bitrix24Exception('retries to connect count must be an integer');
}
$this->retriesToConnectCount = (int)$retriesCnt;

return true;
}

/**
* Add call to batch. If [[$callback]] parameter is set, it will receive call result as first parameter.
*
* @param string $method
* @param array $parameters
* @param string $method
* @param array $parameters
* @param callable|null $callback
*
* @return string Unique call ID.
Expand All @@ -993,6 +1020,7 @@ public function addBatchCall($method, array $parameters = array(), callable $cal
'parameters' => $parameters,
'callback' => $callback,
);

return $id;
}

Expand All @@ -1009,7 +1037,7 @@ public function hasBatchCalls()
/**
* Process batch calls.
*
* @param int $halt Halt batch on error
* @param int $halt Halt batch on error
* @param int $delay Delay between batch calls (in msec)
*
* @throws Bitrix24Exception
Expand All @@ -1025,7 +1053,7 @@ public function processBatchCalls($halt = 0, $delay = self::BATCH_DELAY)
$batchQueryCounter++;
$slice = array_splice($this->_batch, 0, self::MAX_BATCH_CALLS);
$this->log->info('bitrix24PhpSdk.processBatchCalls.callItem', array(
'batch_query_number' => $batchQueryCounter
'batch_query_number' => $batchQueryCounter,
));

$commands = array();
Expand Down Expand Up @@ -1065,7 +1093,7 @@ public function processBatchCalls($halt = 0, $delay = self::BATCH_DELAY)
* Execute Bitrix24 REST API method
*
* @param string $methodName
* @param array $additionalParameters
* @param array $additionalParameters
*
* @return mixed
* @throws \Bitrix24\Exceptions\Bitrix24WrongClientException
Expand Down Expand Up @@ -1104,7 +1132,7 @@ public function call($methodName, array $additionalParameters = array())
* Execute Bitrix24 REST API method
*
* @param string $methodName
* @param array $additionalParameters
* @param array $additionalParameters
*
* @throws Bitrix24Exception
* @throws Bitrix24ApiException
Expand Down Expand Up @@ -1146,7 +1174,7 @@ protected function _call($methodName, array $additionalParameters = array())
$this->log->info('call bitrix24 method', array(
'BITRIX24_DOMAIN' => $this->domain,
'METHOD_NAME' => $methodName,
'METHOD_PARAMETERS' => $additionalParameters
'METHOD_PARAMETERS' => $additionalParameters,
));
$requestResult = $this->executeRequest($url, $additionalParameters);
// check errors and throw exception if errors exists
Expand Down Expand Up @@ -1192,6 +1220,7 @@ protected function _call($methodName, array $additionalParameters = array())
throw new Bitrix24SecurityException('security signature in api-response not found');
}
}

return $requestResult;
}
}

0 comments on commit 4d97533

Please sign in to comment.