osb/app/Models/ProviderOauth.php

57 lines
1.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
2022-08-18 13:29:42 +00:00
use App\Traits\SiteID;
class ProviderOauth extends Model
{
2022-08-18 13:29:42 +00:00
use SiteID;
protected $table = 'provider_oauth';
protected $fillable = ['name','active'];
/* RELATIONS */
2022-08-18 13:29:42 +00:00
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);
}
2022-08-18 13:29:42 +00:00
/* 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());
}
}