44 lines
1.1 KiB
PHP
44 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace Bluesquare\Connect\Traits;
|
|
|
|
trait HasConnectSync
|
|
{
|
|
abstract function fill(array $attributes);
|
|
abstract function save();
|
|
abstract function delete();
|
|
|
|
public static $connectResource;
|
|
|
|
public static $connectColumnId = 'connect_resource_id';
|
|
|
|
public static function findConnectResource($id)
|
|
{
|
|
return self::query()->where(self::$connectColumnId, $id)->first();
|
|
}
|
|
|
|
public static function onConnectResourceCreated($id, $data)
|
|
{
|
|
$record = self::findConnectResource($id) ?? new self;
|
|
$record->fill($data); // TODO
|
|
$record->{self::$connectColumnId} = $id;
|
|
return $record->save();
|
|
}
|
|
|
|
public static function onConnectResourceUpdated($id, $data)
|
|
{
|
|
return self::onConnectResourceCreated($id, $data);
|
|
}
|
|
|
|
public static function onConnectResourceDeleted($id, $data = null)
|
|
{
|
|
$record = self::findConnectResource($id);
|
|
return $record ? $record->forceDelete() : false;
|
|
}
|
|
|
|
public static function onConnectResourceDoesNotExist($record)
|
|
{
|
|
return $record->forceDelete();
|
|
}
|
|
}
|