pre version 2
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Services\GoogleOAuthService;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class GoogleOAuthConnection extends Model
|
||||
{
|
||||
protected $table = 'google_oauth_connections';
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'service',
|
||||
'email',
|
||||
'access_token',
|
||||
'refresh_token',
|
||||
'expires_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'expires_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function isExpired(): bool
|
||||
{
|
||||
return $this->expires_at && $this->expires_at->isPast();
|
||||
}
|
||||
|
||||
public function refresh(): void
|
||||
{
|
||||
if (!$this->refresh_token) {
|
||||
throw new \RuntimeException('Nessun refresh token disponibile per ' . $this->email);
|
||||
}
|
||||
app(GoogleOAuthService::class)->refreshToken($this);
|
||||
}
|
||||
|
||||
public function getValidAccessToken(): string
|
||||
{
|
||||
if ($this->isExpired() && $this->refresh_token) {
|
||||
$this->refresh();
|
||||
}
|
||||
return $this->access_token;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user