40 $this->httpClient->query(
41 HttpClient::HTTP_POST,
42 $this->prepareCreateUrl(),
46 if ($this->isRequestSuccess())
48 $resultData = $this->prepareResult($this->httpClient->getResult(), $section);
49 $this->updateSectionColor($resultData[
'syncSection']);
54 $response = Json::decode($this->httpClient->getResult());
58 switch ($error[
'code'])
63 $this->handleUnauthorize($this->connection);
64 $result->addError(
new Error($error[
'message'], $error[
'code']));
67 if (!empty($error[
'code']))
69 $result->addError(
new Error($error[
'message'], $error[
'code']));
73 $result->addError(
new Error(
'Uncknown Google API error', 400));
85 $result->addError(
new Error(
'failed to create an section in google'));
89 $result->addError(
new Error(
'failed to convert section'));
110 $this->httpClient->query(
111 HttpClient::HTTP_PUT,
112 $this->prepareCalendarUrl(
$context->getSectionConnection()->getVendorSectionId()),
116 if ($this->isRequestSuccess())
118 $resultData = $this->prepareResult($this->httpClient->getResult(), $section);
119 $this->updateSectionColor($resultData[
'syncSection']);
124 $response = Json::decode($this->httpClient->getResult());
128 switch ($error[
'code'])
131 $this->handleUnauthorize($this->connection);
132 $result->addError(
new Error($error[
'message'], $error[
'code']));
135 if (!empty($error[
'code']))
137 $result->addError(
new Error($error[
'message'], $error[
'code']));
141 $result->addError(
new Error(
'Uncknown Google API error', 400));
150 $result->addError(
new Error(
'failed to update an section in google'));
169 $this->httpClient->query(
170 HttpClient::HTTP_DELETE,
171 $this->prepareCalendarUrl(
$context->getSectionConnection()->getVendorSectionId())
173 if (!$this->isRequestDeleteSuccess())
175 $response = Json::decode($this->httpClient->getResult());
179 switch ($error[
'code'])
182 $this->handleUnauthorize($this->connection);
183 $result->addError(
new Error($error[
'message'], $error[
'code']));
186 if (!empty($error[
'code']))
188 $result->addError(
new Error($error[
'message'], $error[
'code']));
192 $result->addError(
new Error(
'Uncknown Google API error', 400));
198 $result->addError(
new Error(
'failed to delete an section in google'));
206 private function prepareCreateUrl(): string
208 return $this->connection->getServer()->getFullPath() . self::CREATE_PATH;
211 private function prepareCalendarUrl(
string $vendorSectionId): string
213 return Server::mapUri(
214 $this->connection->getServer()->getFullPath()
215 . self::CALENDAR_PATH,
217 '%CALENDAR_ID%' => Server::getEncodePath($vendorSectionId)
228 private function prepareResult(
string $result,
Core\Section\Section $section):
array
230 $externalSection = Json::decode(
$result);
239 ->setSection($section)
240 ->setVendorName(
Core\Section\Section::LOCAL_EXTERNAL_TYPE)
242 ->setSectionConnection($sectionConnection)
247 'id' => $externalSection[
'id'],
248 'version' => $externalSection[
'etag'],
249 'syncSection' => $syncSection,
258 private function encode(
array $section)
260 return Json::encode($section, JSON_UNESCAPED_SLASHES);
276 if (empty($syncSection->getSectionConnection()?->getVendorSectionId()))
281 $this->httpClient->put(
282 $this->createCalendarListUpdateUrl($syncSection->getSectionConnection()?->getVendorSectionId()),
283 $this->prepareUpdateColorParams($syncSection)
287 private function createCalendarListUpdateUrl(?
string $getVendorSectionId): string
289 return Server::mapUri(
290 $this->connection->getServer()->getFullPath()
291 . self::CALENDAR_LIST_PATH
292 .
'?' . preg_replace(
'/(%3D)/',
'=', http_build_query([
'colorRgbFormat' =>
"True"])),
294 '%CALENDAR_ID%' => Server::getEncodePath($getVendorSectionId),
302 private function prepareUpdateColorParams(Sync\Entities\SyncSection $syncSection)
306 if ($color = $syncSection->getSection()->getColor())
308 $parameters[
'backgroundColor'] = $color;
309 $parameters[
'foregroundColor'] =
'#ffffff';
312 $parameters[
'selected'] =
'true';
314 return Json::encode($parameters, JSON_UNESCAPED_SLASHES);