Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
baseapiobject.php
1<?
2
4
9use COption;
10
11if(!defined("BITRIX_CLOUD_ADV_URL"))
12{
13 define("BITRIX_CLOUD_ADV_URL", 'https://cloud-adv.bitrix.info');
14}
15
16if(!defined("SEO_BITRIX_API_URL"))
17{
18 define("SEO_BITRIX_API_URL", BITRIX_CLOUD_ADV_URL."/rest/");
19}
20
21abstract class BaseApiObject
22{
23 private const API_URL = SEO_BITRIX_API_URL;
24 protected $result;
25 protected $accessToken;
26 protected $clientId;
27 protected $clientSecret;
28
29 private function checkResult(): void
30 {
31 $errorCode = $this->result['error'] ?? $this->result['code']?? false;
32
33 if(!$errorCode)
34 {
35 return;
36 }
37
38 switch ($errorCode)
39 {
40 case 'verification_needed':
41 case 'ACCESS_DENIED':
42 COption::RemoveOption('sender', ApiRequest::ACCESS_CODE);
43 throw new AccessDeniedException();
44 break;
45 }
46 }
47
51 public function getAccessToken()
52 {
53 return $this->accessToken;
54 }
55
62 {
63 $this->accessToken = $accessToken;
64
65 return $this;
66 }
67
71 public function getClientId()
72 {
73 return $this->clientId;
74 }
75
81 public function setClientId($clientId)
82 {
83 $this->clientId = $clientId;
84
85 return $this;
86 }
87
91 public function getClientSecret()
92 {
94 }
95
102 {
103 $this->clientSecret = $clientSecret;
104
105 return $this;
106 }
107
108 protected function sendRequest($data = [])
109 {
110 $scope = static::getScope().$data['methodName']??'';
111
112 $httpResult = $this
113 ->query($scope, $data['parameters']??[])
114 ->getResult();
115
116 $httpResult = $httpResult ?
117 YandexJson::decode($httpResult) : [];
118
119 $this->result = isset($httpResult['result']) ?
120 YandexJson::decode($httpResult['result']) :
121 $httpResult
122 ;
123
124 $this->checkResult();
125 }
126 protected function registerOnCloudAdv()
127 {
128 $authAdapter = Service::getInstance()->getAuthAdapter(Service::TYPE_YANDEX);
129
130 $http = new HttpClient();
131 $http->setRedirect(false);
132 $http->get($authAdapter->getAuthUrl());
133 }
134
135 protected function query($scope, $param = NULL)
136 {
137 if ($param === NULL)
138 {
139 $param = array();
140 }
141
142 $http = new HttpClient();
143 $http->setRedirect(false);
144
145 $postData = array(
146 "access_code" => $this->accessToken,
147 "client_id" => $this->clientId,
148 "client_secret" => $this->clientSecret,
149 "data" => json_encode($param, JSON_UNESCAPED_UNICODE)
150 );
151
152 if (!empty($param))
153 {
154 $postData = array_merge($postData, $param);
155 }
156
157 $http->post(self::API_URL.$scope, $postData, false);
158
159 return $http;
160 }
161
162 abstract function getScope():string;
163}
static decode($data)
Definition json.php:53