You've already forked laravel-connect
feat: sync, webhook, tokens refresh, fillable, key config
This commit is contained in:
@@ -1,23 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\Connect\Traits;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
trait HasConnectData
|
||||
{
|
||||
public static $connectIdentifier = 'connect_id';
|
||||
|
||||
protected $connectFillable = [];
|
||||
public function getConnectIdentifier()
|
||||
{
|
||||
return $this->connectIdentifier ?? 'connect_id';
|
||||
}
|
||||
|
||||
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;
|
||||
$touched = [];
|
||||
|
||||
$fillable = $this->connectFillable ?? [];
|
||||
|
||||
foreach ($fillable as $origin => $targets) {
|
||||
$value = is_string($origin) ? $data[$origin] : $data[$targets];
|
||||
$targets = is_string($origin) && is_array($targets) ? $targets : [$targets];
|
||||
|
||||
foreach ($targets as $target) {
|
||||
$parts = explode('|', $target);
|
||||
$target = $parts[0];
|
||||
$currentValue = $value ?? ($parts[1] ?? null);
|
||||
$target_model = $this;
|
||||
$parts = explode('.', $target);
|
||||
|
||||
foreach ($parts as $i => $property) {
|
||||
if ($i < count($parts) - 1) {
|
||||
$target_model = $target_model->$property;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($target_model !== $this)
|
||||
$touched[] = $target_model;
|
||||
|
||||
$target_model->$property = $currentValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract public function fill(array $attributes);
|
||||
foreach ($touched as $model)
|
||||
$model->save();
|
||||
}
|
||||
}
|
||||
|
||||
41
src/Traits/HasConnectWebhook.php
Normal file
41
src/Traits/HasConnectWebhook.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Bluesquare\Connect\Traits;
|
||||
|
||||
trait HasConnectWebhook
|
||||
{
|
||||
public function onConnectCreate(array $data)
|
||||
{
|
||||
$this->onConnectUpdate($data);
|
||||
}
|
||||
|
||||
public function onConnectUpdate(array $data)
|
||||
{
|
||||
if (in_array(HasConnectData::class, class_uses(self::class))) {
|
||||
$this->fillConnectData($data);
|
||||
} else {
|
||||
$this->email = $data['email'];
|
||||
}
|
||||
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public function onConnectDelete(array $data)
|
||||
{
|
||||
$this->onConnectUpdate($data);
|
||||
|
||||
if (in_array(\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses(self::class))) {
|
||||
$this->delete();
|
||||
} elseif (array_key_exists('remember_token', $this->attributes)) {
|
||||
$this->remember_token = null;
|
||||
$this->save();
|
||||
}
|
||||
}
|
||||
|
||||
public function onConnectRestore(array $data)
|
||||
{
|
||||
$this->restore();
|
||||
|
||||
$this->onConnectUpdate($data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user