Permalink
Browse files

✨ Watch Google resources

  • Loading branch information...
lorisleiva committed Oct 2, 2018
1 parent 3e8b4aa commit 3e75ff9458d3f25ce37d9d2e9ab3d6816017801e
@@ -15,6 +15,6 @@ class PeriodicSynchronizations implements ShouldQueue
public function handle()
{
Synchronization::get()->each->ping();
Synchronization::whereNull('resource_id')->get()->each->ping();
}
}
@@ -0,0 +1,20 @@
<?php
namespace App\Jobs;
use App\Jobs\WatchGoogleResource;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class WatchGoogleCalendars extends WatchGoogleResource implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function getGoogleRequest($service, $channel)
{
return $service->calendarList->watch($channel);
}
}
@@ -0,0 +1,22 @@
<?php
namespace App\Jobs;
use App\Jobs\WatchGoogleResource;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class WatchGoogleEvents extends WatchGoogleResource implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function getGoogleRequest($service, $channel)
{
return $service->events->watch(
$this->synchronizable->google_id, $channel
);
}
}
@@ -0,0 +1,39 @@
<?php
namespace App\Jobs;
use Illuminate\Support\Carbon;
abstract class WatchGoogleResource
{
protected $synchronizable;
public function __construct($synchronizable)
{
$this->synchronizable = $synchronizable;
}
public function handle()
{
$synchronization = $this->synchronizable->synchronization;
try {
$response = $this->getGoogleRequest(
$this->synchronizable->getGoogleService('Calendar'),
$synchronization->asGoogleChannel()
);
$synchronization->update([
'resource_id' => $response->getResourceId(),
'expired_at' => Carbon::createFromTimestampMs($response->getExpiration())
]);
} catch (\Google_Service_Exception $e) {
// If we reach an error at this point, it is likely that
// push notifications are not allowed for this resource.
// Instead we will sync it manually at regular interval.
}
}
abstract public function getGoogleRequest($service, $channel);
}
View
@@ -23,6 +23,16 @@ public function ping()
return $this->synchronizable->synchronize();
}
public function asGoogleChannel()
{
return tap(new \Google_Service_Calendar_Channel(), function ($channel) {
$channel->setId($this->id);
$channel->setResourceId($this->resource_id);
$channel->setType('web_hook');
$channel->setAddress(config('services.google.webhook_uri'));
});
}
public function synchronizable()
{
return $this->morphTo();

0 comments on commit 3e75ff9

Please sign in to comment.