Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pushmanager.php
1<?php
2
4
19
21{
30 public function addSectionPush(SectionConnection $link): Result
31 {
32 $result = new Result();
33 if ($this->isVirtualCalendar($link))
34 {
35 return $result->addError(new Error('This type of calendar doesnt support push.', 415));
36 }
37
38 $calendarId = $link->getVendorSectionId();
39 $params = $this->makeChannelParams($link->getId(), Dictionary::PUSH_CHANNEL_TYPES['sectionConnection']);
40
41 // TODO: Remake it: move this logic to parent::request().
42 // Or, better, in separate class.
43 $this->httpClient->query(
45 $this->connection->getVendor()->getServer()->getFullPath()
46 . '/calendars/' . urlencode($calendarId) . '/events/watch',
47 Web\Json::encode($params, JSON_UNESCAPED_SLASHES)
48 );
49
50 if ($this->httpClient->getStatus() === 200)
51 {
52 $data = Web\Json::decode($this->httpClient->getResult());
53 $result->setData([
54 'CHANNEL_ID' => $data['id'],
55 'RESOURCE_ID' => $data['resourceId'],
56 'EXPIRES' => new DateTime($data['expiration'] / 1000, 'U'),
57 ]);
58 }
59 else if ($this->httpClient->getStatus() === 401)
60 {
61 $this->handleUnauthorize($this->connection);
62
63 $result->addError(new Error('Unauthorized', $this->httpClient->getStatus()));
64 }
65 else
66 {
67 $result->addError(new Error('Error of create channel', $this->httpClient->getStatus()));
68 }
69
70 return $result;
71 }
72
73 public function renewPush(Push $pushChannel): Result
74 {
75 return (new Result())->addError(new Error('Service doesnt support this method', 405));
76 }
77
84 public function deletePush(Push $pushChannel): Result
85 {
86 $result = new Result();
87 // TODO: Remake it: move this logic to parent::request().
88 // Or, better, in separate class.
89 $this->httpClient->query(
91 $this->connection->getVendor()->getServer()->getFullPath() . '/channels/stop',
92 Web\Json::encode([
93 'id' => $pushChannel->getChannelId(), // TODO: need to understand - what id is waiting
94 'resourceId' => $pushChannel->getResourceId()
95 ], JSON_UNESCAPED_SLASHES)
96 );
97
98 if ($this->httpClient->getStatus() !== 200)
99 {
100 $result->addError(new Error('Error of stopping push channel.', $this->httpClient->getStatus()));
101 }
102
103 return $result;
104 }
105
114 public function addConnectionPush(Connection $connection): Result
115 {
116 $result = new Result();
117 $channelInfo = $this->makeChannelParams($connection->getName(), GoogleApiSync::CONNECTION_CHANNEL_TYPE);
118 // TODO: Remake it: move this logic to parent::request().
119 // Or, better, in separate class.
120 $this->httpClient->query(
122 $this->connection->getVendor()->getServer()->getFullPath() . '/users/me/calendarList/watch',
123 Web\Json::encode($channelInfo, JSON_UNESCAPED_SLASHES)
124 );
125
126 if ($this->isRequestSuccess())
127 {
128 $data = Web\Json::decode($this->httpClient->getResult());
129 $result->setData([
130 'CHANNEL_ID' => $data['id'],
131 'RESOURCE_ID' => $data['resourceId'],
132 'EXPIRES' => new DateTime($data['expiration'] / 1000, 'U'),
133 ]);
134 }
135 else if ($this->httpClient->getStatus() === 401)
136 {
137 $this->handleUnauthorize($this->connection);
138
139 $result->addError(new Error('Unauthorized', $this->httpClient->getStatus()));
140 }
141 else
142 {
143 $result->addError(new Error('Error of create channel', $this->httpClient->getStatus()));
144 }
145
146 return $result;
147 }
148
155 private function makeChannelParams($uniqId, string $type): array
156 {
157 if (defined('BX24_HOST_NAME') && BX24_HOST_NAME)
158 {
159 $externalUrl = GoogleApiSync::EXTERNAL_LINK . BX24_HOST_NAME;
160 }
161 else
162 {
163 $request = Context::getCurrent()->getRequest();
164 if (defined('SITE_SERVER_NAME') && SITE_SERVER_NAME)
165 {
166 $host = SITE_SERVER_NAME;
167 }
168 else
169 {
170 $host = Option::get('main', 'server_name', $request->getHttpHost());
171 }
172
173 $externalUrl = 'https://' . $host . '/bitrix/tools/calendar/push.php';
174 }
175
176 return [
177 'id' => $type . '_' . $this->userId.'_'.md5($uniqId. time()),
178 'type' => 'web_hook',
179 'address' => $externalUrl,
180 'expiration' => (time() + GoogleApiSync::CHANNEL_EXPIRATION) * 1000,
181 ];
182 }
183
189 private function isVirtualCalendar(SectionConnection $link): bool
190 {
191 return (strpos($link->getVendorSectionId(), 'holiday.calendar.google.com'))
192 || (strpos($link->getVendorSectionId(), 'group.v.calendar.google.com'))
193 || (strpos($link->getVendorSectionId(), '@virtual'))
194 || (strpos($link->getSection()->getExternalType(), '_readonly'))
195 || (strpos($link->getSection()->getExternalType(), '_freebusy'))
196 ;
197 }
198}
addSectionPush(SectionConnection $link)
addConnectionPush(Connection $connection)
static getCurrent()
Definition context.php:241
addError(Error $error)
Definition result.php:50