Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
catalogwebhookhandler.php
1<?php
2
3namespace Bitrix\Seo\Catalog;
4
6
15
16
18{
20 private $request;
21
23 private $engine;
24
26 private $eventManager;
27
29 private $application;
30
31 public function __construct(
32 Request $request,
33 Engine $engine,
34 EventManager $eventManager,
35 Application $application
36 )
37 {
38 $this->request = $request;
39 $this->engine = $engine;
40 $this->eventManager = $eventManager;
41 $this->application = $application;
42 }
43
44 private function verifyRequest() : Result
45 {
46 $result = new Result();
47
48 $authToken = $this->request->getHeader('Authorization');
49 if (!$authToken)
50 {
51 $authToken = \Bitrix\Main\Context::getCurrent()->getServer()->get('REMOTE_USER');
52 }
53 if (!$authToken)
54 {
55 $result->addError(new Error('wrong request'));
56 return $result;
57 }
58
59 $authToken = substr($authToken, strlen('Bearer '));
60 $engineClientSecret = $this->engine->getClientSecret();
61 $authTokenSalt = mb_substr($authToken, 0, 8);
62 $isAllowedToken = $authToken === $authTokenSalt . md5($authTokenSalt . $engineClientSecret);
63 if (!$isAllowedToken)
64 {
65 $result->addError(new Error('Invalid client credentials'));
66 }
67
68 return $result;
69
70 }
71
72 private function run() : Json
73 {
74 $result = $this->verifyRequest();
75
76 if (!$result->isSuccess())
77 {
78 return $this->buildErrorResponse(
79 $result->getErrorCollection()->current()
80 );
81 }
82
83 $this->eventManager->send(
84 new Event('seo', 'OnCatalogWebhook', ['payload' => $this->request->toArray(),])
85 );
86
87 return $this->buildResponse();
88 }
89
90
91 public function handle(): void
92 {
93 $response = $this->run();
94 $this->application->end(200, $response);
95 }
96
97 private function buildErrorResponse(Error $error): Json
98 {
99 return new Json([
100 'error' => [
101 'message' => $error->getMessage(),
102 'code' => $error->getCode(),
103 ],
104 'data' => [],
105 ]);
106 }
107
108 private function buildResponse(): Json
109 {
110 return new Json([
111 'error' => false,
112 'data' => [
113 'status' => 'ok'
114 ],
115 ]);
116 }
117}
__construct(Request $request, Engine $engine, EventManager $eventManager, Application $application)