Skip to content

Commit

Permalink
♻️ Use Synchronizable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Sep 17, 2018
1 parent e8ffdb5 commit 9e0fa4b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
11 changes: 5 additions & 6 deletions app/Calendar.php
Expand Up @@ -2,13 +2,16 @@


namespace App; namespace App;


use App\Concerns\Synchronizable;
use App\Event; use App\Event;
use App\GoogleAccount; use App\GoogleAccount;
use App\Jobs\SynchronizeGoogleEvents; use App\Jobs\SynchronizeGoogleEvents;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;


class Calendar extends Model class Calendar extends Model
{ {
use Synchronizable;

protected $fillable = [ protected $fillable = [
'google_id', 'name', 'color', 'timezone', 'google_id', 'name', 'color', 'timezone',
]; ];
Expand All @@ -23,12 +26,8 @@ public function events()
return $this->hasMany(Event::class); return $this->hasMany(Event::class);
} }


public static function boot() public function synchronize()
{ {
parent::boot(); SynchronizeGoogleEvents::dispatch($this);

static::created(function ($calendar) {
SynchronizeGoogleEvents::dispatch($calendar);
});
} }
} }
28 changes: 28 additions & 0 deletions app/Concerns/Synchronizable.php
@@ -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();
}
11 changes: 5 additions & 6 deletions app/GoogleAccount.php
Expand Up @@ -3,13 +3,16 @@
namespace App; namespace App;


use App\Calendar; use App\Calendar;
use App\Concerns\Synchronizable;
use App\Jobs\SynchronizeGoogleCalendars; use App\Jobs\SynchronizeGoogleCalendars;
use App\Services\Google; use App\Services\Google;
use App\User; use App\User;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;


class GoogleAccount extends Model class GoogleAccount extends Model
{ {
use Synchronizable;

protected $fillable = [ protected $fillable = [
'google_id', 'name', 'token', 'google_id', 'name', 'token',
]; ];
Expand All @@ -28,12 +31,8 @@ public function calendars()
return $this->hasMany(Calendar::class); return $this->hasMany(Calendar::class);
} }


public static function boot() public function synchronize()
{ {
parent::boot(); SynchronizeGoogleCalendars::dispatch($this);

static::created(function ($googleAccount) {
SynchronizeGoogleCalendars::dispatch($googleAccount);
});
} }
} }

0 comments on commit 9e0fa4b

Please sign in to comment.