Permalink
Browse files

♻️ Use Synchronizable trait

  • Loading branch information...
lorisleiva committed Sep 17, 2018
1 parent e8ffdb5 commit 9e0fa4b4965695049e23944dc6041542c3935d07
Showing with 38 additions and 12 deletions.
  1. +5 −6 app/Calendar.php
  2. +28 −0 app/Concerns/Synchronizable.php
  3. +5 −6 app/GoogleAccount.php
View
@@ -2,13 +2,16 @@
namespace App;
use App\Concerns\Synchronizable;
use App\Event;
use App\GoogleAccount;
use App\Jobs\SynchronizeGoogleEvents;
use Illuminate\Database\Eloquent\Model;
class Calendar extends Model
{
use Synchronizable;
protected $fillable = [
'google_id', 'name', 'color', 'timezone',
];
@@ -23,12 +26,8 @@ public function events()
return $this->hasMany(Event::class);
}
public static function boot()
public function synchronize()
{
parent::boot();
static::created(function ($calendar) {
SynchronizeGoogleEvents::dispatch($calendar);
});
SynchronizeGoogleEvents::dispatch($this);
}
}
@@ -0,0 +1,28 @@
<?php
namespace App\Concerns;
use App\Synchronization;
trait Synchronizable
{
public static function bootSynchronizable()
{
// Start a new synchronization once created.
static::created(function ($synchronizable) {
$synchronizable->synchronization()->create();
});
// Stop and delete associated synchronization.
static::deleting(function ($synchronizable) {
optional($synchronizable->synchronization)->delete();
});
}
public function synchronization()
{
return $this->morphOne(Synchronization::class, 'synchronizable');
}
abstract public function synchronize();
}
View
@@ -3,13 +3,16 @@
namespace App;
use App\Calendar;
use App\Concerns\Synchronizable;
use App\Jobs\SynchronizeGoogleCalendars;
use App\Services\Google;
use App\User;
use Illuminate\Database\Eloquent\Model;
class GoogleAccount extends Model
{
use Synchronizable;
protected $fillable = [
'google_id', 'name', 'token',
];
@@ -28,12 +31,8 @@ public function calendars()
return $this->hasMany(Calendar::class);
}
public static function boot()
public function synchronize()
{
parent::boot();
static::created(function ($googleAccount) {
SynchronizeGoogleCalendars::dispatch($googleAccount);
});
SynchronizeGoogleCalendars::dispatch($this);
}
}

0 comments on commit 9e0fa4b

Please sign in to comment.