1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
pushmanager.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sync\Office365;
4
5use Bitrix\Calendar\Core\Base\BaseException;
6use Bitrix\Calendar\Core\Base\Date;
7use Bitrix\Calendar\Sync\Connection\Connection;
8use Bitrix\Calendar\Sync\Connection\SectionConnection;
9use Bitrix\Calendar\Sync\Exceptions\ApiException;
10use Bitrix\Calendar\Sync\Exceptions\AuthException;
11use Bitrix\Calendar\Sync\Exceptions\ConflictException;
12use Bitrix\Calendar\Sync\Exceptions\NotFoundException;
13use Bitrix\Calendar\Sync\Exceptions\RemoteAccountException;
14use Bitrix\Calendar\Sync\Internals\HasContextTrait;
15use Bitrix\Calendar\Sync\Managers\PushManagerInterface;
16use Bitrix\Calendar\Sync\Push\Push;
17use Bitrix\Calendar\Sync\Util\Result;
18use Bitrix\Main\ArgumentException;
19use Bitrix\Main\ArgumentNullException;
20use Bitrix\Main\Error;
21use Bitrix\Main\LoaderException;
22use Bitrix\Main\ObjectException;
23use Bitrix\Main\Type\DateTime;
24use Exception;
25
27{
28 use HasContextTrait;
29
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)
Определения pushmanager.php:30
renewPush(Push $pushChannel)
Определения pushmanager.php:44
addSectionPush(SectionConnection $link)
Определения pushmanager.php:98
addConnectionPush(Connection $connection)
Определения pushmanager.php:196
deletePush(Push $pushChannel)
Определения pushmanager.php:145
getResourceId()
Определения push.php:96
getChannelId()
Определения push.php:78
setExpireDate(Date $expireDate)
Определения push.php:123
Определения error.php:15
addError(Error $error)
Определения result.php:50
Определения date.php:9
$data['IS_AVAILABLE']
Определения .description.php:13
$result
Определения get_property_values.php:14
$context
Определения csv_new_setup.php:223
$time
Определения payment.php:61