osb/app/Models/ProviderOauth.php

57 lines
1.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\SiteID;
class ProviderOauth extends Model
{
use SiteID;
protected $table = 'provider_oauth';
protected $fillable = ['name','active'];
/* RELATIONS */
public function accounts()
{
return $this->belongsToMany(Account::class,'account_provider')
->where('account_provider.site_id',$this->site_id)
->withPivot('ref','synctoken','created_at','updated_at');
}
public function tokens()
{
return $this->hasMany(ProviderToken::class);
}
public function users()
{
return $this->hasMany(UserOauth::class);
}
/* METHODS */
public function api_class(): ?string
{
return config('services.provider.'.strtolower($this->name).'.api');
}
public function API(ProviderToken $o,bool $tryprod=FALSE): mixed
{
return ($this->api_class() && $o->access_token) ? new ($this->api_class())($o,$tryprod) : NULL;
}
/**
* Do we have API details for this supplier
*
* @return bool
*/
public function hasAPIdetails(): bool
{
return $this->api_class() && $this->access_token && (! $this->hasAccessTokenExpired());
}
}