Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function syncEvents(User $user, string $calendarId, ?string $syncToken = null)
- {
- try {
- $params = [
- 'maxResults' => 2500,
- 'singleEvents' => true,
- ];
- if ($syncToken) {
- $params['syncToken'] = $syncToken;
- } else {
- // If no sync token, use timeMin for full sync
- $params['timeMin'] = date(DATE_RFC3339, strtotime('-30 days'));
- }
- $pageToken = null;
- $newSyncToken = null;
- do {
- if ($pageToken) {
- $params['pageToken'] = $pageToken;
- }
- $events = $this->calendarService->events->listEvents($calendarId, $params);
- $pageToken = $events->getNextPageToken();
- $newSyncToken = $events->getNextSyncToken();
- foreach ($events->getItems() as $event) {
- // Process each event - update your local database
- $this->processEvent($user, $calendarId, $event);
- }
- } while ($pageToken);
- // Update the sync token in database
- if ($newSyncToken) {
- CalendarWebhook::where('user_id', $user->id)
- ->where('calendar_id', $calendarId)
- ->update(['sync_token' => $newSyncToken]);
- }
- return true;
- } catch (\Exception $e) {
- // Handle sync token expired
- if (strpos($e->getMessage(), 'Sync token') !== false) {
- // Clear sync token and do a full sync
- CalendarWebhook::where('user_id', $user->id)
- ->where('calendar_id', $calendarId)
- ->update(['sync_token' => null]);
- // Try again without sync token
- return $this->syncEvents($user, $calendarId, null);
- }
- Log::error('Failed to sync Google Calendar events: ' . $e->getMessage());
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement