Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pushmanager.php
1<?php
2
4
14use Bitrix\Calendar\Sync\Internals\HasContextTrait;
24use Exception;
25
27{
28 use HasContextTrait;
29
30 public function __construct(Office365Context $context)
31 {
32 $this->context = $context;
33 parent::__construct($context->getConnection());
34 }
35
44 public function renewPush(Push $pushChannel): Result
45 {
46 $result = new Result();
47 try
48 {
49 if ($data = $this->context->getVendorSyncService()->resubscribe($pushChannel->getResourceId()))
50 {
51 if (empty($data['expirationDateTime']))
52 {
53 $time = time() + 70 * 60 * 60;
54 $data['expirationDateTime'] = date('c', $time);
55 }
56
57 $pushChannel->setExpireDate(new Date($this->convertToDateTime($data['expirationDateTime'])));
58 $result->setData([
59 'CHANNEL_ID' => $pushChannel->getChannelId(),
60 'RESOURCE_ID' => $data['id'],
61 'EXPIRES' => $this->convertToDateTime($data['expirationDateTime']),
62 ]);
63 }
64 else
65 {
66 $result->addError(new Error('Error of renew push channel'));
67 }
68 }
69 catch (ApiException $e)
70 {
71 $result->addError(new Error('Error of MS Graph API', $e->getCode(), $e->getMessage()));
72 }
73
74 return $result;
75 }
76
84 private function convertToDateTime(string $time): DateTime
85 {
86 $phpDateTime = new \DateTime($time);
87
88 return DateTime::createFromPhp($phpDateTime);
89 }
90
98 public function addSectionPush(SectionConnection $link): Result
99 {
100 $result = new Result();
101 try
102 {
103 $data = $this->context->getVendorSyncService()->subscribeSection($link);
104
105 if ($data && !empty($data['channelId']))
106 {
107 $result->setData([
108 'CHANNEL_ID' => $data['channelId'],
109 'RESOURCE_ID' => $data['id'],
110 'EXPIRES' => $this->convertToDateTime($data['expirationDateTime'] ?? date('c')),
111 ]);
112 }
113 else
114 {
115 $result->addError(new Error('Error of create subscription.'));
116 }
117 }
118 catch (ApiException $e)
119 {
120 $result->addError(new Error('Error of Push subscribing. Vendor returned error.', $e->getCode()));
121 }
122 catch (AuthException $e)
123 {
124 $result->addError(new Error('No authentication data', $e->getCode()));
125 }
126
127 return $result;
128 }
129
145 public function deletePush(Push $pushChannel): Result
146 {
147 $result = new Result();
148
149 try
150 {
151 if ($data = $this->context->getVendorSyncService()->unsubscribe($pushChannel->getResourceId()))
152 {
153 $result->setData($data);
154 }
155
156 return $result;
157 }
158 catch (RemoteAccountException|AuthException $exception)
159 {
160 return $result;
161 }
162 }
163
179 public function clearAllSubscriptions(): void
180 {
181 $apiResponse = $this->context->getApiClient()->get('subscriptions');
182 if (!empty($apiResponse['value']))
183 {
184 foreach ($apiResponse['value'] as $subscription)
185 {
186 $this->context->getApiClient()->delete('subscriptions/' . $subscription['id']);
187 }
188 }
189 }
190
197 {
198 return (new Result())->addError(new Error('Connection push unavailable for this service', 503));
199 }
200}
__construct(Office365Context $context)
addSectionPush(SectionConnection $link)
setExpireDate(Date $expireDate)
Definition push.php:123
addError(Error $error)
Definition result.php:50
static createFromPhp(\DateTime $datetime)
Definition datetime.php:232