3030
3131/**
3232 * Class Bitrix24
33- * @author Mesilov Maxim <[email protected] > 33+ *
34+ * @author Mesilov Maxim <[email protected] > 3435 * @copyright 2013 - 2016 Mesilov Maxim
3536 */
3637class Bitrix24 implements iBitrix24
@@ -146,13 +147,13 @@ class Bitrix24 implements iBitrix24
146147 * @var bool ssl verify for checking CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST
147148 */
148149 protected $ sslVerify = true ;
149-
150-
150+
151+
151152 /**
152153 * Create a object to work with Bitrix24 REST API service
153154 *
154- * @param bool $isSaveRawResponse - if true raw response from bitrix24 will be available from method getRawResponse, this is debug mode
155- * @param null|LoggerInterface $obLogger - instance of \Monolog\Logger
155+ * @param bool $isSaveRawResponse - if true raw response from bitrix24 will be available from method getRawResponse, this is debug mode
156+ * @param null|LoggerInterface $obLogger - instance of \Monolog\Logger
156157 *
157158 * @throws Bitrix24Exception
158159 *
@@ -279,6 +280,7 @@ public function getNewAccessToken()
279280 $ requestResult = $ this ->executeRequest ($ url );
280281 // handling bitrix24 api-level errors
281282 $ this ->handleBitrix24APILevelErrors ($ requestResult , 'refresh access token ' );
283+
282284 return $ requestResult ;
283285 }
284286
@@ -307,6 +309,7 @@ public function setApplicationId($applicationId)
307309 throw new Bitrix24Exception ('application id is empty ' );
308310 }
309311 $ this ->applicationId = $ applicationId ;
312+
310313 return true ;
311314 }
312315
@@ -335,6 +338,7 @@ public function setApplicationSecret($applicationSecret)
335338 throw new Bitrix24Exception ('application secret is empty ' );
336339 }
337340 $ this ->applicationSecret = $ applicationSecret ;
341+
338342 return true ;
339343 }
340344
@@ -363,6 +367,7 @@ public function setRefreshToken($refreshToken)
363367 throw new Bitrix24Exception ('refresh token is empty ' );
364368 }
365369 $ this ->refreshToken = $ refreshToken ;
370+
366371 return true ;
367372 }
368373
@@ -389,6 +394,7 @@ public function setApplicationScope(array $applicationScope)
389394 {
390395 if (is_array ($ applicationScope ) && count ($ applicationScope ) > 0 ) {
391396 $ this ->applicationScope = $ applicationScope ;
397+
392398 return true ;
393399 } else {
394400 throw new Bitrix24Exception ('application scope not set ' );
@@ -420,6 +426,7 @@ public function setRedirectUri($redirectUri)
420426 throw new Bitrix24Exception ('redirect URI is empty ' );
421427 }
422428 $ this ->redirectUri = $ redirectUri ;
429+
423430 return true ;
424431 }// end of SetApplicationId
425432
@@ -448,30 +455,31 @@ public function setDomain($domain)
448455 throw new Bitrix24Exception ('domain is empty ' );
449456 }
450457 $ this ->domain = $ domain ;
458+
451459 return true ;
452460 }
453461
454462 /**
455463 * disable of checking CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST
456464 */
457- public function setDisabledSslVerify ()
465+ public function setDisabledSslVerify ()
458466 {
459467 $ this ->sslVerify = false ;
460468 }
461-
469+
462470 /**
463471 * enable of checking CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST
464472 */
465- public function setEnabledSslVerify ()
473+ public function setEnabledSslVerify ()
466474 {
467475 $ this ->sslVerify = true ;
468476 }
469-
477+
470478 /**
471479 * Execute a request API to Bitrix24 using cURL
472480 *
473481 * @param string $url
474- * @param array $additionalParameters
482+ * @param array $additionalParameters
475483 *
476484 * @throws Bitrix24Exception
477485 * @throws Bitrix24PortalDeletedException
@@ -490,7 +498,7 @@ protected function executeRequest($url, array $additionalParameters = array())
490498 CURLE_READ_ERROR ,
491499 CURLE_OPERATION_TIMEOUTED ,
492500 CURLE_HTTP_POST_ERROR ,
493- CURLE_SSL_CONNECT_ERROR
501+ CURLE_SSL_CONNECT_ERROR ,
494502 );
495503
496504 $ curlOptions = array (
@@ -503,11 +511,10 @@ protected function executeRequest($url, array $additionalParameters = array())
503511 CURLOPT_USERAGENT => strtolower (__CLASS__ . '-PHP-SDK/v ' . self ::VERSION ),
504512 CURLOPT_POST => true ,
505513 CURLOPT_POSTFIELDS => http_build_query ($ additionalParameters ),
506- CURLOPT_URL => $ url
514+ CURLOPT_URL => $ url,
507515 );
508-
509- if (!$ this ->sslVerify )
510- {
516+
517+ if (!$ this ->sslVerify ) {
511518 $ curlOptions [CURLOPT_SSL_VERIFYPEER ] = 0 ;
512519 $ curlOptions [CURLOPT_SSL_VERIFYHOST ] = 0 ;
513520 }
@@ -583,6 +590,7 @@ protected function executeRequest($url, array $additionalParameters = array())
583590 $ this ->log ->error ($ errorMsg , $ this ->getErrorContext ());
584591 throw new Bitrix24Exception ($ errorMsg );
585592 }
593+
586594 return $ jsonResult ;
587595 }
588596
@@ -607,7 +615,7 @@ protected function getErrorContext()
607615 // network
608616 'RAW_REQUEST ' => $ this ->getRawRequest (),
609617 'CURL_REQUEST_INFO ' => $ this ->getRequestInfo (),
610- 'RAW_RESPONSE ' => $ this ->getRawResponse ()
618+ 'RAW_RESPONSE ' => $ this ->getRawResponse (),
611619 );
612620 }
613621
@@ -638,6 +646,7 @@ public function setMemberId($memberId)
638646 throw new Bitrix24Exception ('memberId is null ' );
639647 }
640648 $ this ->memberId = $ memberId ;
649+
641650 return true ;
642651 }
643652
@@ -666,6 +675,7 @@ public function setAccessToken($accessToken)
666675 throw new Bitrix24Exception ('access token is empty ' );
667676 }
668677 $ this ->accessToken = $ accessToken ;
678+
669679 return true ;
670680 }
671681
@@ -713,7 +723,9 @@ public function getRetriesToConnectTimeout()
713723
714724 /**
715725 * set retries to connect timeout in microseconds
726+ *
716727 * @param int $microseconds
728+ *
717729 * @return bool
718730 * @throws Bitrix24Exception
719731 */
@@ -724,14 +736,15 @@ public function setRetriesToConnectTimeout($microseconds = 1000000)
724736 throw new Bitrix24Exception ('retries to connect count must be an integer ' );
725737 }
726738 $ this ->retriesToConnectTimeout = $ microseconds ;
739+
727740 return true ;
728741 }
729742
730743 /**
731744 * Handling bitrix24 api-level errors
732745 *
733- * @param $arRequestResult
734- * @param $methodName
746+ * @param $arRequestResult
747+ * @param $methodName
735748 * @param array $additionalParameters
736749 *
737750 * @return null
@@ -757,29 +770,37 @@ protected function handleBitrix24APILevelErrors(
757770 (array_key_exists ('error_description ' , $ arRequestResult ) ? $ arRequestResult ['error_description ' ] : '' ),
758771 $ methodName ,
759772 $ this ->getDomain ());
760- $ this ->log ->error ($ errorMsg , $ this ->getErrorContext ());
761773 // throw specific API-level exceptions
762774 switch (strtoupper (trim ($ arRequestResult ['error ' ]))) {
763775 case 'WRONG_CLIENT ' :
764776 case 'ERROR_OAUTH ' :
777+ $ this ->log ->error ($ errorMsg , $ this ->getErrorContext ());
765778 throw new Bitrix24WrongClientException ($ errorMsg );
766779 case 'ERROR_METHOD_NOT_FOUND ' :
780+ $ this ->log ->error ($ errorMsg , $ this ->getErrorContext ());
767781 throw new Bitrix24MethodNotFoundException ($ errorMsg );
768782 case 'INVALID_TOKEN ' :
769783 case 'INVALID_GRANT ' :
784+ $ this ->log ->error ($ errorMsg , $ this ->getErrorContext ());
770785 throw new Bitrix24TokenIsInvalidException ($ errorMsg );
771786 case 'EXPIRED_TOKEN ' :
787+ $ this ->log ->notice ($ errorMsg , $ this ->getErrorContext ());
772788 throw new Bitrix24TokenIsExpiredException ($ errorMsg );
773789 case 'PAYMENT_REQUIRED ' :
790+ $ this ->log ->error ($ errorMsg , $ this ->getErrorContext ());
774791 throw new Bitrix24PaymentRequiredException ($ errorMsg );
775792 case 'NO_AUTH_FOUND ' :
793+ $ this ->log ->error ($ errorMsg , $ this ->getErrorContext ());
776794 throw new Bitrix24PortalRenamedException ($ errorMsg );
777795 case 'INSUFFICIENT_SCOPE ' :
796+ $ this ->log ->error ($ errorMsg , $ this ->getErrorContext ());
778797 throw new Bitrix24InsufficientScope ($ errorMsg );
779798 default :
799+ $ this ->log ->error ($ errorMsg , $ this ->getErrorContext ());
780800 throw new Bitrix24ApiException ($ errorMsg );
781801 }
782802 }
803+
783804 return null ;
784805 }
785806
@@ -830,6 +851,7 @@ public function getFirstAccessToken($code)
830851 $ requestResult = $ this ->executeRequest ($ url );
831852 // handling bitrix24 api-level errors
832853 $ this ->handleBitrix24APILevelErrors ($ requestResult , 'get first access token ' );
854+
833855 return $ requestResult ;
834856 }
835857
@@ -873,14 +895,15 @@ public function isAccessTokenExpire()
873895 $ this ->handleBitrix24APILevelErrors ($ requestResult , 'app.info ' );
874896 }
875897 }
898+
876899 return $ isTokenExpire ;
877900 }// end of isTokenExpire
878901
879902 /**
880903 * Get list of all methods available for current application
881904 *
882905 * @param array | null $applicationScope
883- * @param bool $isFull
906+ * @param bool $isFull
884907 *
885908 * @return array
886909 *
@@ -913,6 +936,7 @@ public function getAvailableMethods(array $applicationScope = array(), $isFull =
913936 $ scope = '&scope= ' . implode (', ' , array_map ('urlencode ' , array_unique ($ applicationScope )));
914937 }
915938 $ url = 'https:// ' . $ domain . '/rest/methods.json?auth= ' . $ accessToken . $ showAll . $ scope ;
939+
916940 return $ this ->executeRequest ($ url );
917941 }
918942
@@ -945,6 +969,7 @@ public function getScope($isFull = false)
945969 $ showAll = '&full=true ' ;
946970 }
947971 $ url = 'https:// ' . $ domain . '/rest/scope.json?auth= ' . $ accessToken . $ showAll ;
972+
948973 return $ this ->executeRequest ($ url );
949974 }
950975
@@ -960,6 +985,7 @@ public function getRetriesToConnectCount()
960985
961986 /**
962987 * set CURL request count retries
988+ *
963989 * @param $retriesCnt
964990 *
965991 * @return boolean
@@ -973,14 +999,15 @@ public function setRetriesToConnectCount($retriesCnt = 1)
973999 throw new Bitrix24Exception ('retries to connect count must be an integer ' );
9741000 }
9751001 $ this ->retriesToConnectCount = (int )$ retriesCnt ;
1002+
9761003 return true ;
9771004 }
9781005
9791006 /**
9801007 * Add call to batch. If [[$callback]] parameter is set, it will receive call result as first parameter.
9811008 *
982- * @param string $method
983- * @param array $parameters
1009+ * @param string $method
1010+ * @param array $parameters
9841011 * @param callable|null $callback
9851012 *
9861013 * @return string Unique call ID.
@@ -993,6 +1020,7 @@ public function addBatchCall($method, array $parameters = array(), callable $cal
9931020 'parameters ' => $ parameters ,
9941021 'callback ' => $ callback ,
9951022 );
1023+
9961024 return $ id ;
9971025 }
9981026
@@ -1009,7 +1037,7 @@ public function hasBatchCalls()
10091037 /**
10101038 * Process batch calls.
10111039 *
1012- * @param int $halt Halt batch on error
1040+ * @param int $halt Halt batch on error
10131041 * @param int $delay Delay between batch calls (in msec)
10141042 *
10151043 * @throws Bitrix24Exception
@@ -1025,7 +1053,7 @@ public function processBatchCalls($halt = 0, $delay = self::BATCH_DELAY)
10251053 $ batchQueryCounter ++;
10261054 $ slice = array_splice ($ this ->_batch , 0 , self ::MAX_BATCH_CALLS );
10271055 $ this ->log ->info ('bitrix24PhpSdk.processBatchCalls.callItem ' , array (
1028- 'batch_query_number ' => $ batchQueryCounter
1056+ 'batch_query_number ' => $ batchQueryCounter,
10291057 ));
10301058
10311059 $ commands = array ();
@@ -1065,7 +1093,7 @@ public function processBatchCalls($halt = 0, $delay = self::BATCH_DELAY)
10651093 * Execute Bitrix24 REST API method
10661094 *
10671095 * @param string $methodName
1068- * @param array $additionalParameters
1096+ * @param array $additionalParameters
10691097 *
10701098 * @return mixed
10711099 * @throws \Bitrix24\Exceptions\Bitrix24WrongClientException
@@ -1104,7 +1132,7 @@ public function call($methodName, array $additionalParameters = array())
11041132 * Execute Bitrix24 REST API method
11051133 *
11061134 * @param string $methodName
1107- * @param array $additionalParameters
1135+ * @param array $additionalParameters
11081136 *
11091137 * @throws Bitrix24Exception
11101138 * @throws Bitrix24ApiException
@@ -1146,7 +1174,7 @@ protected function _call($methodName, array $additionalParameters = array())
11461174 $ this ->log ->info ('call bitrix24 method ' , array (
11471175 'BITRIX24_DOMAIN ' => $ this ->domain ,
11481176 'METHOD_NAME ' => $ methodName ,
1149- 'METHOD_PARAMETERS ' => $ additionalParameters
1177+ 'METHOD_PARAMETERS ' => $ additionalParameters,
11501178 ));
11511179 $ requestResult = $ this ->executeRequest ($ url , $ additionalParameters );
11521180 // check errors and throw exception if errors exists
@@ -1192,6 +1220,7 @@ protected function _call($methodName, array $additionalParameters = array())
11921220 throw new Bitrix24SecurityException ('security signature in api-response not found ' );
11931221 }
11941222 }
1223+
11951224 return $ requestResult ;
11961225 }
11971226}
0 commit comments