Bitrix-D7 22.6
 
Загрузка...
Поиск...
Не найдено
manager.php
1<?php
2
4
5use Bitrix\Calendar\Core;
16
17abstract class Manager extends ServiceBase
18{
23 protected int $userId;
24
25 private static array $httpClients = [];
26
33 public function __construct(Connection $connection, int $userId)
34 {
35 parent::__construct($connection);
36 $this->userId = $userId;
37 if (!$this->initHttpClient())
38 {
39 $this->deactivateConnection();
40 }
41 }
42
51 private function initHttpClient(bool $force = false): bool
52 {
53 $success = true;
54 $userId = $this->userId;
55 if (!isset(self::$httpClients[$userId]) || $force)
56 {
57 if (!Loader::includeModule('socialservices'))
58 {
59 throw new SystemException('Module Socialservices not found');
60 }
61
62 $httpClient = new HttpClient();
63 if (\CSocServGoogleProxyOAuth::isProxyAuth())
64 {
65 $oAuth = new \CSocServGoogleProxyOAuth($userId);
66 }
67 else
68 {
69 $oAuth = new \CSocServGoogleOAuth($userId);
70 }
71
72 $oAuth->getEntityOAuth()->addScope(
73 [
74 'https://www.googleapis.com/auth/calendar',
75 'https://www.googleapis.com/auth/calendar.readonly',
76 ]
77 );
78
79 $oAuth->getEntityOAuth()->setUser($userId);
80
81 if ($oAuth->getEntityOAuth()->GetAccessToken())
82 {
83 $httpClient->setHeader('Authorization', 'Bearer ' . $oAuth->getEntityOAuth()->getToken());
84 $httpClient->setHeader('Content-Type', 'application/json');
85 $httpClient->setHeader('Referer', Helper::getDomain());
86 unset($oAuth);
87 }
88 else
89 {
90 $success = false;
91 }
92
93 self::$httpClients[$userId] = new HttpQuery($httpClient, $userId);
94 }
95
96 $this->httpClient = self::$httpClients[$userId];
97
98 return $success;
99 }
100
101 private function deactivateConnection()
102 {
103 $this->connection
104 ->setStatus('[401] Unauthorized')
105 ->setLastSyncTime(new Core\Base\Date())
106 ;
107
109 $mapperFactory = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
110 $mapperFactory->getConnection()->update($this->connection);
111
112 Util::addPullEvent('refresh_sync_status', $this->connection->getOwner()->getId(), [
113 'syncInfo' => [
114 'google' => [
115 'status' => false,
116 'type' => $this->connection->getAccountType(),
117 'connected' => true,
118 'id' => $this->connection->getId(),
119 'syncOffset' => 0
120 ],
121 ],
122 'requestUid' => Util::getRequestUid(),
123 ]);
124 }
125
129 protected function isRequestSuccess(): bool
130 {
131 return $this->httpClient->getStatus() === 200;
132 }
133
134 protected function isRequestDeleteSuccess(): bool
135 {
136 $acceptedCodes = [200, 201, 204, 404];
137
138 return in_array($this->httpClient->getStatus(), $acceptedCodes);
139 }
140
148 protected function handleUnauthorize(Connection $connection)
149 {
150 $this->deactivateConnection();
151 }
152
160 protected function request($params)
161 {
162 // TODO: implement it
163 }
164}
__construct(Connection $connection, int $userId)
Definition: manager.php:33
handleUnauthorize(Connection $connection)
Definition: manager.php:148
static addPullEvent(string $command, int $userId, array $params=[])
Definition: util.php:371
static getRequestUid()
Definition: util.php:518