41 $this->httpClient->query(
43 $this->prepareCreateUrl(),
47 if ($this->isRequestSuccess())
49 $resultData = $this->prepareResult($this->httpClient->getResult(), $section);
50 $this->updateSectionColor($resultData[
'syncSection']);
51 $result->setData($resultData);
55 $response = Json::decode($this->httpClient->getResult());
56 if (!empty($response[
'error']))
58 $error = $response[
'error'];
59 switch ($error[
'code'])
64 $this->handleUnauthorize($this->connection);
65 $result->addError(
new Error($error[
'message'], $error[
'code']));
68 if (!empty($error[
'code']))
70 $result->addError(
new Error($error[
'message'], $error[
'code']));
74 $result->addError(
new Error(
'Uncknown Google API error', 400));
80 $result->addError(
new Error(
'do not create section'));
86 AddMessage2Log($e->getMessage(),
'calendar', 2,
true);
87 $result->addError(
new Error(
'failed to create an section in google'));
91 AddMessage2Log($e->getMessage(),
'calendar', 2,
true);
92 $result->addError(
new Error(
'failed to convert section'));
113 $this->httpClient->query(
119 if ($this->isRequestSuccess())
121 $resultData = $this->prepareResult($this->httpClient->getResult(), $section);
122 $this->updateSectionColor($resultData[
'syncSection']);
123 $result->setData($resultData);
127 $response = Json::decode($this->httpClient->getResult());
128 if (!empty($response[
'error']))
130 $error = $response[
'error'];
131 switch ($error[
'code'])
134 $this->handleUnauthorize($this->connection);
135 $result->addError(
new Error($error[
'message'], $error[
'code']));
138 if (!empty($error[
'code']))
140 $result->addError(
new Error($error[
'message'], $error[
'code']));
144 $result->addError(
new Error(
'Uncknown Google API error', 400));
148 $result->addError(
new Error(
'do not update section'));
153 AddMessage2Log($e->getMessage(),
'calendar', 2,
true);
154 $result->addError(
new Error(
'failed to update an section in google'));
173 $this->httpClient->query(
175 $this->prepareCalendarUrl($context->getSectionConnection()->getVendorSectionId())
177 if (!$this->isRequestDeleteSuccess())
179 $response = Json::decode($this->httpClient->getResult());
180 if (!empty($response[
'error']))
182 $error = $response[
'error'];
183 switch ($error[
'code'])
186 $this->handleUnauthorize($this->connection);
187 $result->addError(
new Error($error[
'message'], $error[
'code']));
190 if (!empty($error[
'code']))
192 $result->addError(
new Error($error[
'message'], $error[
'code']));
196 $result->addError(
new Error(
'Uncknown Google API error', 400));
202 $result->addError(
new Error(
'failed to delete an section in google'));
210 private function prepareCreateUrl(): string
215 private function prepareCalendarUrl(
string $vendorSectionId): string
217 return Server::mapUri(
218 $this->connection->getServer()->getFullPath()
219 . self::CALENDAR_PATH,
221 '%CALENDAR_ID%' => Server::getEncodePath($vendorSectionId)
232 private function prepareResult(
string $result, Core\Section\Section $section): array
234 $externalSection = Json::decode($result);
243 ->setSection($section)
244 ->setVendorName(Core\Section\Section::LOCAL_EXTERNAL_TYPE)
246 ->setSectionConnection($sectionConnection)
251 'id' => $externalSection[
'id'],
252 'version' => $externalSection[
'etag'],
253 'syncSection' => $syncSection,
262 private function encode(array $section)
264 return Json::encode($section, JSON_UNESCAPED_SLASHES);
278 private function updateSectionColor(Sync\Entities\
SyncSection $syncSection): void
280 $this->httpClient->put(
281 $this->createCalendarListUpdateUrl($syncSection->getSectionConnection()->getVendorSectionId()),
282 $this->prepareUpdateColorParams($syncSection)
286 private function createCalendarListUpdateUrl(?
string $getVendorSectionId): string
288 return Server::mapUri(
289 $this->connection->getServer()->getFullPath()
290 . self::CALENDAR_LIST_PATH
291 .
'?' . preg_replace(
'/(%3D)/',
'=', http_build_query([
'colorRgbFormat' =>
"True"])),
293 '%CALENDAR_ID%' => Server::getEncodePath($getVendorSectionId),
301 private function prepareUpdateColorParams(Sync\Entities\SyncSection $syncSection)
305 if ($color = $syncSection->getSection()->getColor())
307 $parameters[
'backgroundColor'] = $color;
308 $parameters[
'foregroundColor'] =
'#ffffff';
311 $parameters[
'selected'] =
'true';
313 return Json::encode($parameters, JSON_UNESCAPED_SLASHES);