Permalink
Browse files

Store and use syncTokens

  • Loading branch information...
lorisleiva committed Sep 17, 2018
1 parent 7c0fe4b commit 35ea0dac6ec70fae23a84a1a2ea263029fcf4512
@@ -14,17 +14,10 @@ class SynchronizeGoogleCalendars extends SynchronizeGoogleResource implements Sh
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $googleAccount;
public function __construct($googleAccount)
{
$this->googleAccount = $googleAccount;
}
public function getGoogleService()
{
return app(Google::class)
->connectUsing($this->googleAccount->token)
->connectUsing($this->synchronizable->token)
->service('Calendar');
}
@@ -36,12 +29,12 @@ public function getGoogleRequest($service, $options)
public function syncItem($googleCalendar)
{
if ($googleCalendar->deleted) {
return $this->googleAccount->calendars()
return $this->synchronizable->calendars()
->where('google_id', $googleCalendar->id)
->get()->each->delete();
}
$this->googleAccount->calendars()->updateOrCreate(
$this->synchronizable->calendars()->updateOrCreate(
[
'google_id' => $googleCalendar->id,
],
@@ -52,4 +45,10 @@ public function syncItem($googleCalendar)
]
);
}
public function dropAllSyncedItems()
{
// Here we use `each->delete()` to make sure model listeners are called.
$this->synchronizable->calendars->each->delete();
}
}
@@ -14,37 +14,30 @@
class SynchronizeGoogleEvents extends SynchronizeGoogleResource implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $calendar;
public function __construct($calendar)
{
$this->calendar = $calendar;
}
public function getGoogleService()
{
return app(Google::class)
->connectUsing($this->calendar->googleAccount->token)
->connectUsing($this->synchronizable->googleAccount->token)
->service('Calendar');
}
public function getGoogleRequest($service, $options)
{
return $service->events->listEvents(
$this->calendar->google_id, $options
$this->synchronizable->google_id, $options
);
}
public function syncItem($googleEvent)
{
if ($googleEvent->status === 'cancelled') {
return $this->calendar->events()
return $this->synchronizable->events()
->where('google_id', $googleEvent->id)
->delete();
}
$this->calendar->events()->updateOrCreate(
$this->synchronizable->events()->updateOrCreate(
[
'google_id' => $googleEvent->id,
],
@@ -58,6 +51,11 @@ public function syncItem($googleEvent)
);
}
public function dropAllSyncedItems()
{
$this->synchronizable->events()->delete();
}
protected function isAllDayEvent($googleEvent)
{
return ! $googleEvent->start->dateTime && ! $googleEvent->end->dateTime;
@@ -4,23 +4,50 @@
abstract class SynchronizeGoogleResource
{
protected $synchronizable;
protected $synchronization;
public function __construct($synchronizable)
{
$this->synchronizable = $synchronizable;
$this->synchronization = $synchronizable->synchronization;
}
public function handle()
{
$pageToken = null;
$syncToken = $this->synchronization->token;
$service = $this->getGoogleService();
do {
$list = $this->getGoogleRequest($service, compact('pageToken'));
$tokens = compact('pageToken', 'syncToken');
try {
$list = $this->getGoogleRequest($service, $tokens);
} catch (\Google_Service_Exception $e) {
if ($e->getCode() === 410) {
$this->synchronization->update(['token' => null]);
$this->dropAllSyncedItems();
return $this->handle();
}
throw $e;
}
foreach ($list->getItems() as $item) {
$this->syncItem($item);
}
$pageToken = $list->getNextPageToken();
} while ($pageToken);
$this->synchronization->update([
'token' => $list->getNextSyncToken(),
'last_synchronized_at' => now(),
]);
}
abstract public function getGoogleService();
abstract public function getGoogleRequest($service, $options);
abstract public function syncItem($item);
abstract public function dropAllSyncedItems();
}

0 comments on commit 35ea0da

Please sign in to comment.