Skip to content
Permalink
Browse files

♻️ Refactor Google authentication from Synchronizables

  • Loading branch information...
lorisleiva committed Oct 1, 2018
1 parent 02ca77e commit 9959fd34e5932b0a89c3c0defd4f0e424163430b
@@ -2,6 +2,7 @@
namespace App\Concerns;
use App\Services\Google;
use App\Synchronization;
trait Synchronizable
@@ -23,6 +24,13 @@ public function synchronization()
{
return $this->morphOne(Synchronization::class, 'synchronizable');
}
public function getGoogleService($service)
{
return app(Google::class)
->connectWithSynchronizable($this)
->service($service);
}
abstract public function synchronize();
}
@@ -3,7 +3,6 @@
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;
@@ -14,13 +13,6 @@ class SynchronizeGoogleCalendars extends SynchronizeGoogleResource implements Sh
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function getGoogleService()
{
return app(Google::class)
->connectUsing($this->synchronizable->token)
->service('Calendar');
}
public function getGoogleRequest($service, $options)
{
return $service->calendarList->listCalendarList($options);
@@ -3,7 +3,6 @@
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;
@@ -15,13 +14,6 @@ class SynchronizeGoogleEvents extends SynchronizeGoogleResource implements Shoul
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function getGoogleService()
{
return app(Google::class)
->connectUsing($this->synchronizable->googleAccount->token)
->service('Calendar');
}
public function getGoogleRequest($service, $options)
{
return $service->events->listEvents(
@@ -17,7 +17,7 @@ public function handle()
{
$pageToken = null;
$syncToken = $this->synchronization->token;
$service = $this->getGoogleService();
$service = $this->synchronizable->getGoogleService('Calendar');
do {
$tokens = compact('pageToken', 'syncToken');
@@ -46,7 +46,6 @@ public function handle()
]);
}
abstract public function getGoogleService();
abstract public function getGoogleRequest($service, $options);
abstract public function syncItem($item);
abstract public function dropAllSyncedItems();
@@ -2,6 +2,9 @@
namespace App\Services;
use App\Calendar;
use App\GoogleAccount;
class Google
{
protected $client;
@@ -26,6 +29,27 @@ public function connectUsing($token)
return $this;
}
public function connectWithSynchronizable($synchronizable)
{
$token = $this->getTokenFromSynchronizable($synchronizable);
return $this->connectUsing($token);
}
protected function getTokenFromSynchronizable($synchronizable)
{
switch (true) {
case $synchronizable instanceof GoogleAccount:
return $synchronizable->token;
case $synchronizable instanceof Calendar:
return $synchronizable->googleAccount->token;
default:
throw new \Exception("Invalid Synchronizable");
}
}
public function revokeToken($token = null)
{
$token = $token ?? $this->client->getAccessToken();

0 comments on commit 9959fd3

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.