Files
laravel-quickbooks/src/Token.php
T
Joel Male 8ec2c3cc38 feat: add Laravel 11/12 and PHP 8.3+ support
- Widen illuminate/* deps to ^10.0|^11.0|^12.0, require PHP ^8.3
- Upgrade PHPUnit to ^10.5|^11.0, Mockery to ^1.6
- Rewrite phpunit.xml.dist for PHPUnit 10+ format (source/include
  replaces filter/whitelist, removed deprecated attributes)
- Replace $dates with $casts on Token model
- Implement DeferrableProvider interface instead of $defer property
  on ClientServiceProvider
- Use FQCN route definitions instead of string-based namespace()
- Fix migration getColumnType() check for Laravel 11+ native types
- Replace @test doc annotations with #[Test] attributes
- Fix dynamic property deprecation in ControllerTest
2026-02-18 10:29:34 +10:00

130 lines
3.4 KiB
PHP

<?php
namespace Wearepixel\QuickBooks;
use App\Models\User;
use Carbon\Carbon;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken;
use QuickBooksOnline\API\Exception\SdkException;
/**
* Class Token
*
*
* @property bool $hasValidAccessToken Is the access token valid
* @property bool $hasValidRefreshToken Is the refresh token valid
* @property Carbon $access_token_expires_at Timestamp that the access token expires
* @property Carbon $refresh_token_expires_at Timestamp that the refresh token expires
* @property int $user_id Id of the related User
* @property string $access_token The access token
* @property string $realm_id Realm Id from the OAuth token
* @property string $refresh_token The refresh token
* @property User $user
*/
class Token extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'quickbooks_tokens';
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'access_token_expires_at' => 'datetime',
'refresh_token_expires_at' => 'datetime',
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'access_token',
'access_token_expires_at',
'realm_id',
'refresh_token',
'refresh_token_expires_at',
'user_id',
];
/**
* Check if access token is valid
*
* A token is good for 1 hour, so if it expires greater than 1 hour from now, it is still valid
*/
public function getHasValidAccessTokenAttribute(): bool
{
return $this->access_token_expires_at && Carbon::now()->lt($this->access_token_expires_at);
}
/**
* Check if refresh token is valid
*
* A token is good for 101 days, so if it expires greater than 101 days from now, it is still valid
*/
public function getHasValidRefreshTokenAttribute(): bool
{
return $this->refresh_token_expires_at &&
Carbon::now()->lt($this->refresh_token_expires_at);
}
/**
* Parse OauthToken.
*
* Process the OAuth token & store it in the persistent storage
*
* @throws SdkException
*/
public function parseOauthToken(OAuth2AccessToken $oauth_token): Token
{
// TODO: Deal with exception
$this->access_token = $oauth_token->getAccessToken();
$this->access_token_expires_at = Carbon::parse($oauth_token->getAccessTokenExpiresAt());
$this->realm_id = $oauth_token->getRealmID();
$this->refresh_token = $oauth_token->getRefreshToken();
$this->refresh_token_expires_at = Carbon::parse($oauth_token->getRefreshTokenExpiresAt());
return $this;
}
/**
* Remove the token
*
* When a token is deleted, we still need a token for the client for the user.
*
* @throws Exception
*/
public function remove(): Token
{
$user = $this->user;
$this->delete();
return $user->quickBooksToken()->make();
}
/**
* Belongs to user.
*/
public function user(): BelongsTo
{
$config = config('quickbooks.user');
return $this->belongsTo(
$config['model'],
$config['keys']['foreign'],
$config['keys']['owner'],
);
}
}