Skip to content
Permalink
Browse files

✨ Create and use jobs that import Google's data

  • Loading branch information...
lorisleiva committed Aug 17, 2018
1 parent bd25acb commit 7cac0f3a9778df23d4f2aa6021f4dc89a9d5a91d
Showing with 173 additions and 0 deletions.
  1. +10 βˆ’0 app/Calendar.php
  2. +10 βˆ’0 app/GoogleAccount.php
  3. +55 βˆ’0 app/Jobs/SynchronizeGoogleCalendars.php
  4. +72 βˆ’0 app/Jobs/SynchronizeGoogleEvents.php
  5. +26 βˆ’0 app/Jobs/SynchronizeGoogleResource.php
@@ -4,6 +4,7 @@
use App\Event;
use App\GoogleAccount;
use App\Jobs\SynchronizeGoogleEvents;
use Illuminate\Database\Eloquent\Model;
class Calendar extends Model
@@ -21,4 +22,13 @@ public function events()
{
return $this->hasMany(Event::class);
}
public static function boot()
{
parent::boot();
static::created(function ($calendar) {
SynchronizeGoogleEvents::dispatch($calendar);
});
}
}
@@ -3,6 +3,7 @@
namespace App;
use App\Calendar;
use App\Jobs\SynchronizeGoogleCalendars;
use App\Services\Google;
use App\User;
use Illuminate\Database\Eloquent\Model;
@@ -26,4 +27,13 @@ public function calendars()
{
return $this->hasMany(Calendar::class);
}
public static function boot()
{
parent::boot();
static::created(function ($googleAccount) {
SynchronizeGoogleCalendars::dispatch($googleAccount);
});
}
}
@@ -0,0 +1,55 @@
<?php
namespace App\Jobs;
use App\Jobs\SynchronizeGoogleResource;
use App\Services\Google;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SynchronizeGoogleCalendars extends SynchronizeGoogleResource implements ShouldQueue
{
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)
->service('Calendar');
}
public function getGoogleRequest($service, $options)
{
return $service->calendarList->listCalendarList($options);
}
public function syncItem($googleCalendar)
{
if ($googleCalendar->deleted) {
return $this->googleAccount->calendars()
->where('google_id', $googleCalendar->id)
->get()->each->delete();
}
$this->googleAccount->calendars()->updateOrCreate(
[
'google_id' => $googleCalendar->id,
],
[
'name' => $googleCalendar->summary,
'color' => $googleCalendar->backgroundColor,
'timezone' => $googleCalendar->timeZone,
]
);
}
}
@@ -0,0 +1,72 @@
<?php
namespace App\Jobs;
use App\Jobs\SynchronizeGoogleResource;
use App\Services\Google;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
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)
->service('Calendar');
}
public function getGoogleRequest($service, $options)
{
return $service->events->listEvents(
$this->calendar->google_id, $options
);
}
public function syncItem($googleEvent)
{
if ($googleEvent->status === 'cancelled') {
return $this->calendar->events()
->where('google_id', $googleEvent->id)
->delete();
}
$this->calendar->events()->updateOrCreate(
[
'google_id' => $googleEvent->id,
],
[
'name' => $googleEvent->summary,
'description' => $googleEvent->description,
'allday' => $this->isAllDayEvent($googleEvent),
'started_at' => $this->parseDatetime($googleEvent->start),
'ended_at' => $this->parseDatetime($googleEvent->end),
]
);
}
protected function isAllDayEvent($googleEvent)
{
return ! $googleEvent->start->dateTime && ! $googleEvent->end->dateTime;
}
protected function parseDatetime($googleDatetime)
{
$rawDatetime = $googleDatetime->dateTime ?: $googleDatetime->date;
return Carbon::parse($rawDatetime)->setTimezone('UTC');
}
}
@@ -0,0 +1,26 @@
<?php
namespace App\Jobs;
abstract class SynchronizeGoogleResource
{
public function handle()
{
$pageToken = null;
$service = $this->getGoogleService();
do {
$list = $this->getGoogleRequest($service, compact('pageToken'));
foreach ($list->getItems() as $item) {
$this->syncItem($item);
}
$pageToken = $list->getNextPageToken();
} while ($pageToken);
}
abstract public function getGoogleService();
abstract public function getGoogleRequest($service, $options);
abstract public function syncItem($item);
}

0 comments on commit 7cac0f3

Please sign in to comment.
You can’t perform that action at this time.