2022-08-12 04:53:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-08-18 13:29:42 +00:00
|
|
|
use Intuit\Models\ProviderToken as ProviderTokenBase;
|
2022-08-12 04:53:06 +00:00
|
|
|
|
|
|
|
use App\Traits\SiteID;
|
|
|
|
|
2022-08-18 13:29:42 +00:00
|
|
|
class ProviderToken extends ProviderTokenBase
|
2022-08-12 04:53:06 +00:00
|
|
|
{
|
|
|
|
use SiteID;
|
|
|
|
|
|
|
|
protected $dates = [
|
|
|
|
'access_token_expires_at',
|
|
|
|
'refresh_token_expires_at',
|
|
|
|
];
|
2022-08-13 01:48:16 +00:00
|
|
|
|
2023-05-12 10:09:51 +00:00
|
|
|
protected $with = ['provider'];
|
|
|
|
|
2022-08-13 01:48:16 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
|
|
|
public function provider()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(ProviderOauth::class,'provider_oauth_id');
|
|
|
|
}
|
2023-05-12 10:09:51 +00:00
|
|
|
|
|
|
|
/* METHODS */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an API object to interact with this provider
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function API(): mixed
|
|
|
|
{
|
|
|
|
if (! $this->provider->api_class() || ! $this->access_token)
|
|
|
|
throw new \Exception(sprintf('No API details for [%s]',$this->provider->name));
|
|
|
|
|
|
|
|
return new ($this->provider->api_class())($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do we have API details for this provider
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasAPIdetails(): bool
|
|
|
|
{
|
|
|
|
return $this->provider->api_class() && $this->access_token && (! $this->hasAccessTokenExpired());
|
|
|
|
}
|
2022-08-12 04:53:06 +00:00
|
|
|
}
|