You've already forked laravel-connect
wip
This commit is contained in:
23
src/Traits/HasConnectData.php
Normal file
23
src/Traits/HasConnectData.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\Connect\Traits;
|
||||
|
||||
trait HasConnectData
|
||||
{
|
||||
public static $connectIdentifier = 'connect_id';
|
||||
|
||||
protected $connectFillable = [];
|
||||
|
||||
public function fillConnectData(array $data)
|
||||
{
|
||||
foreach ($this->connectFillable as $origin => $target) {
|
||||
if (is_string($origin)) {
|
||||
$this->$target = $data[$origin] ?? null;
|
||||
} else {
|
||||
$this->$target = $data[$target] ?? null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract public function fill(array $attributes);
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
<?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;
|
||||
$attributes = $record->getConnectFillableAttributes();
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (in_array($key, $attributes))
|
||||
$record->$key = $value;
|
||||
}
|
||||
|
||||
$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();
|
||||
}
|
||||
|
||||
public function getConnectFillableAttributes()
|
||||
{
|
||||
return $this->fillable;
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,10 @@ namespace Bluesquare\Connect\Traits;
|
||||
|
||||
trait HasConnectTokens
|
||||
{
|
||||
//
|
||||
public function fillConnectTokens(array $data)
|
||||
{
|
||||
$this->connect_access_token = $data['access_token'];
|
||||
$this->connect_refresh_token = $data['refresh_token'];
|
||||
$this->connect_expires_at = $data['expires_at'];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user