Connect AdSense over OAuth, sync entities and daily reports locally, and show placement performance in the admin panel without calling the Management API from public pages.
81 lines
2.0 KiB
PHP
81 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
final class AdsenseConnection extends Model
|
|
{
|
|
public const STATUS_PENDING = 'pending';
|
|
|
|
public const STATUS_CONNECTED = 'connected';
|
|
|
|
public const STATUS_RECONNECT_REQUIRED = 'reconnect_required';
|
|
|
|
public const STATUS_ERROR = 'error';
|
|
|
|
public const STATUS_DISCONNECTED = 'disconnected';
|
|
|
|
protected $fillable = [
|
|
'account_resource_name',
|
|
'account_display_name',
|
|
'encrypted_refresh_token',
|
|
'status',
|
|
'connected_by_user_id',
|
|
'connected_at',
|
|
'last_sync_attempt_at',
|
|
'last_successful_sync_at',
|
|
'last_error',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'encrypted_refresh_token',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'encrypted_refresh_token' => 'encrypted',
|
|
'connected_at' => 'datetime',
|
|
'last_sync_attempt_at' => 'datetime',
|
|
'last_successful_sync_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function connectedBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'connected_by_user_id');
|
|
}
|
|
|
|
public static function current(): ?self
|
|
{
|
|
return self::query()->latest('id')->first();
|
|
}
|
|
|
|
public function hasRefreshToken(): bool
|
|
{
|
|
return filled($this->encrypted_refresh_token);
|
|
}
|
|
|
|
public function isConnected(): bool
|
|
{
|
|
return $this->status === self::STATUS_CONNECTED && $this->hasRefreshToken();
|
|
}
|
|
|
|
public function needsReconnect(): bool
|
|
{
|
|
return $this->status === self::STATUS_RECONNECT_REQUIRED || (
|
|
in_array($this->status, [self::STATUS_PENDING, self::STATUS_CONNECTED, self::STATUS_ERROR], true)
|
|
&& ! $this->hasRefreshToken()
|
|
);
|
|
}
|
|
|
|
public function isDisconnected(): bool
|
|
{
|
|
return $this->status === self::STATUS_DISCONNECTED || ! $this->hasRefreshToken();
|
|
}
|
|
}
|