Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
service.php
1<?
2
3namespace Bitrix\Seo\WebHook;
4
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 $response = $engine->getInterface()->getTransport()->call($methodName, $parameters);
306 return (isset($response['result']['RESULT']) && $response['result']['RESULT']);
307 }
308
315 public function checkSecurityCode($securityCode)
316 {
317 return ($this->data && $this->data['SECURITY_CODE'] === $securityCode);
318 }
319
325 public function getErrorCollection()
326 {
327 return $this->errorCollection;
328 }
329
330 protected function sendEvent()
331 {
332 $event = new Event('seo', 'OnWebHook', array(
333 'PAYLOAD' => $this->payload,
334 ));
335 EventManager::getInstance()->send($event);
336 foreach ($event->getResults() as $result)
337 {
338 $parameters = $result->getParameters();
339 if (!empty($parameters['ERROR_COLLECTION']))
340 {
342 $resultErrorCollection = $parameters['ERROR_COLLECTION'];
343 $this->errorCollection->add($resultErrorCollection->toArray());
344 }
345 }
346 }
347}
static registerForm($formId)
Definition service.php:238
static getData($type, $externalId)
Definition service.php:285
checkSecurityCode($securityCode)
Definition service.php:315
__construct($type, $externalId)
Definition service.php:59
static queryHookRegister($methodName, array $parameters)
Definition service.php:297
static unregisterForm($formId)
Definition service.php:248
static answerData(array $data=array())
Definition service.php:108
static create($type, $externalId)
Definition service.php:48
handle(Payload\Batch $payload)
Definition service.php:187
static answerError($code=null, $text=null)
Definition service.php:82