1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
service.php
См. документацию.
1<?
2
3namespace Bitrix\Seo\WebHook;
4
5use Bitrix\Main\ArgumentException;
6use Bitrix\Main\Context;
7use Bitrix\Main\Error;
8use Bitrix\Main\ErrorCollection;
9use Bitrix\Main\Event;
10use Bitrix\Main\EventManager;
11use Bitrix\Main\Web\Json;
12use Bitrix\Seo\Engine\Bitrix as EngineBitrix;
13
19{
20 const ANSWER_ERROR_SYSTEM = '001';
21 const ANSWER_ERROR_NO_CODE = '002';
26
29
30 protected $type;
31
32 protected $externalId;
33
35 protected $payload;
36
37 protected $errors = array();
38
39 protected $data = null;
40
48 public static function create($type, $externalId)
49 {
50 return new static($type, $externalId);
51 }
52
59 public function __construct($type, $externalId)
60 {
61 $this->errorCollection = new ErrorCollection();
62
63 $this->type = $type;
64 $this->externalId = $externalId;
65
66 $this->data = self::getData($this->type, $this->externalId);
67 }
68
69 protected static function answer(array $answer)
70 {
72 global $APPLICATION;
73 $APPLICATION->restartBuffer();
74 header('Content-Type:application/json; charset=UTF-8');
75
76 echo Json::encode($answer);
77
78 \CMain::finalActions();
79 exit;
80 }
81
82 protected static function answerError($code = null, $text = null)
83 {
84 if (!$code)
85 {
86 $code = self::ANSWER_ERROR_SYSTEM;
87 }
88
89 if (!$text)
90 {
91 $errorMessages = array(
92 self::ANSWER_ERROR_SYSTEM => 'Error.',
93 self::ANSWER_ERROR_NO_CODE => 'Parameter `code` not found.',
94 self::ANSWER_ERROR_NO_EXT_ID => 'Parameter `externalId` not found.',
95 self::ANSWER_ERROR_NO_SEC_CODE => 'Parameter `sec` not found.',
96 self::ANSWER_ERROR_WRONG_SEC_CODE => 'Wrong `sec` parameter.',
97 );
98
99 $text = $errorMessages[$code];
100 }
101
102 self::answer(array(
103 'error' => array('code' => $code, 'text' => $text),
104 'data' => array()
105 ));
106 }
107
108 protected static function answerData(array $data = array())
109 {
110 self::answer(array(
111 'error' => false,
112 'data' => $data
113 ));
114 }
115
121 public static function listen()
122 {
123 $request = Context::getCurrent()->getRequest();
124 $type = $request->get('code');
125 if (!$type)
126 {
127 self::answerError(self::ANSWER_ERROR_NO_CODE);
128 return;
129 }
130
131 $securityCode = $request->get('sec');
132 if (!$securityCode)
133 {
134 self::answerError(self::ANSWER_ERROR_NO_SEC_CODE);
135 return;
136 }
137 $externalId = $request->get('externalId');
138 if (!$externalId)
139 {
140 self::answerError(self::ANSWER_ERROR_NO_EXT_ID);
141 return;
142 }
143
144 try
145 {
146 $payload = Json::decode($request->get('payload'));
147 $payload = (new Payload\Batch())->setArray($payload);
148 }
149 catch (ArgumentException $e)
150 {
151 self::answerError(self::ANSWER_ERROR_NO_PAYLOAD);
152 return;
153 }
154 $instance = self::create($type, $externalId);
155 if (!$instance->checkSecurityCode($securityCode))
156 {
157 self::answerError(self::ANSWER_ERROR_WRONG_SEC_CODE);
158 }
159
160 try
161 {
162 $instance->handle($payload);
163 }
164 catch (\Exception $e)
165 {
166 self::answerError($e->getCode(), $e->getMessage());
167 return;
168 }
169
170
171
172 foreach ($instance->getErrorCollection()->toArray() as $error)
173 {
175 self::answerError($error->getCode(), $error->getMessage());
176 }
177
178 self::answerData();
179 }
180
187 public function handle(Payload\Batch $payload)
188 {
189 $this->payload = $payload;
190 $this->sendEvent();
191
192 return $this;
193 }
194
201 public function register(array $parameters = [])
202 {
203 if (!$this->data)
204 {
205 $addParameters = [
206 'TYPE' => $this->type,
207 'EXTERNAL_ID' => $this->externalId,
208 ];
209 if (!empty($parameters['SECURITY_CODE']))
210 {
211 $addParameters['SECURITY_CODE'] = $parameters['SECURITY_CODE'];
212 }
213 $addResult = Internals\WebHookTable::add($addParameters);
214 if (!$addResult->isSuccess())
215 {
216 return false;
217 }
218
219 $this->data = self::getData($this->type, $this->externalId);
220 }
221
222 $result = self::queryHookRegister(
223 'seo.client.webhook.register',
224 array(
225 'CODE' => $this->data['TYPE'],
226 'EXTERNAL_ID' => $this->data['EXTERNAL_ID'],
227 'SECURITY_CODE' => $this->data['SECURITY_CODE'],
228 'CONFIRMATION_CODE' => isset($parameters['CONFIRMATION_CODE']) ?
229 $parameters['CONFIRMATION_CODE']
230 :
231 null,
232 )
233 );
234
235 return $result;
236 }
237
238 public static function registerForm($formId)
239 {
240 return self::queryHookRegister(
241 'seo.client.form.register',
242 [
243 'FORM_ID' => $formId,
244 ]
245 );
246 }
247
248 public static function unregisterForm($formId)
249 {
250 return self::queryHookRegister(
251 'seo.client.form.unregister',
252 [
253 'FORM_ID' => $formId,
254 ]
255 );
256 }
257
263 public function remove()
264 {
265 $result = self::queryHookRegister(
266 'seo.client.webhook.remove',
267 array(
268 'CODE' => $this->type,
269 'EXTERNAL_ID' => $this->externalId
270 )
271 );
272
273 if ($result)
274 {
275 if ($this->data)
276 {
277 $deleteResult = Internals\WebHookTable::delete($this->data['ID']);
278 $result = $deleteResult->isSuccess();
279 }
280 }
281
282 return $result;
283 }
284
285 protected static function getData($type, $externalId)
286 {
287 $list = Internals\WebHookTable::getList(array(
288 'filter' => array(
289 '=TYPE' => $type,
290 '=EXTERNAL_ID' => $externalId,
291 )
292 ));
293
294 return $list->fetch();
295 }
296
297 protected static function queryHookRegister($methodName, array $parameters)
298 {
299 $engine = new EngineBitrix();
300 if (!$engine->isRegistered())
301 {
302 return false;
303 }
304
305 if (!$engine->getInterface())
306 {
307 return false;
308 }
309
310 $response = $engine->getInterface()->getTransport()->call($methodName, $parameters);
311
312 return (isset($response['result']['RESULT']) && $response['result']['RESULT']);
313 }
314
321 public function checkSecurityCode($securityCode)
322 {
323 return ($this->data && $this->data['SECURITY_CODE'] === $securityCode);
324 }
325
331 public function getErrorCollection()
332 {
333 return $this->errorCollection;
334 }
335
336 protected function sendEvent()
337 {
338 $event = new Event('seo', 'OnWebHook', array(
339 'PAYLOAD' => $this->payload,
340 ));
342 foreach ($event->getResults() as $result)
343 {
344 $parameters = $result->getParameters();
345 if (!empty($parameters['ERROR_COLLECTION']))
346 {
348 $resultErrorCollection = $parameters['ERROR_COLLECTION'];
349 $this->errorCollection->add($resultErrorCollection->toArray());
350 }
351 }
352 }
353}
$type
Определения options.php:106
global $APPLICATION
Определения include.php:80
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
Определения event.php:5
static getInstance()
Определения eventmanager.php:31
const ANSWER_ERROR_NO_PAYLOAD
Определения service.php:23
static registerForm($formId)
Определения service.php:238
const ANSWER_ERROR_WRONG_SEC_CODE
Определения service.php:25
const ANSWER_ERROR_NO_SEC_CODE
Определения service.php:24
const ANSWER_ERROR_SYSTEM
Определения service.php:20
static getData($type, $externalId)
Определения service.php:285
checkSecurityCode($securityCode)
Определения service.php:321
__construct($type, $externalId)
Определения service.php:59
$errorCollection
Определения service.php:28
static queryHookRegister($methodName, array $parameters)
Определения service.php:297
const ANSWER_ERROR_NO_EXT_ID
Определения service.php:22
getErrorCollection()
Определения service.php:331
static unregisterForm($formId)
Определения service.php:248
static answerData(array $data=array())
Определения service.php:108
const ANSWER_ERROR_NO_CODE
Определения service.php:21
static create($type, $externalId)
Определения service.php:48
handle(Payload\Batch $payload)
Определения service.php:187
static answerError($code=null, $text=null)
Определения service.php:82
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
$event
Определения prolog_after.php:141
$instance
Определения ps_b24_final.php:14
$text
Определения template_pdf.php:79
$response
Определения result.php:21
$engine
Определения options.php:121
$error
Определения subscription_card_product.php:20