mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 15:07:49 +00:00
55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Plugin extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected $casts = [
|
|
'data_payload' => 'json',
|
|
'data_payload_updated_at' => 'datetime',
|
|
'is_native' => 'boolean',
|
|
];
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($model) {
|
|
if (empty($model->uuid)) {
|
|
$model->uuid = Str::uuid();
|
|
}
|
|
});
|
|
}
|
|
|
|
public function isDataStale(): bool
|
|
{
|
|
if (! $this->data_payload_updated_at || ! $this->data_stale_minutes) {
|
|
return true;
|
|
}
|
|
|
|
return $this->data_payload_updated_at->addMinutes($this->data_stale_minutes)->isPast();
|
|
}
|
|
|
|
public function updateDataPayload(): void
|
|
{
|
|
if ($this->data_strategy === 'polling' && $this->polling_url) {
|
|
$response = Http::withHeaders(['User-Agent' => 'usetrmnl/byos_laravel', 'Accept' => 'application/json'])
|
|
->get($this->polling_url)
|
|
->json();
|
|
|
|
$this->update([
|
|
'data_payload' => $response,
|
|
'data_payload_updated_at' => now(),
|
|
]);
|
|
}
|
|
}
|
|
}
|