1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spinen\QuickBooks; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
9
|
|
|
use QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken; |
10
|
|
|
use QuickBooksOnline\API\Exception\SdkException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Token |
14
|
|
|
* |
15
|
|
|
* @package Spinen\QuickBooks |
16
|
|
|
* |
17
|
|
|
* @property boolean $hasValidAccessToken Is the access token valid |
18
|
|
|
* @property boolean $hasValidRefreshToken Is the refresh token valid |
19
|
|
|
* @property Carbon $access_token_expires_at Timestamp that the access token expires |
20
|
|
|
* @property Carbon $refresh_token_expires_at Timestamp that the refresh token expires |
21
|
|
|
* @property integer $user_id Id of the related User |
22
|
|
|
* @property string $access_token The access token |
23
|
|
|
* @property string $realm_id Realm Id from the OAuth token |
24
|
|
|
* @property string $refresh_token The refresh token |
25
|
|
|
* @property Model $owner |
26
|
|
|
*/ |
27
|
|
|
class Token extends Model |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The table associated with the model. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $table = 'quickbooks_tokens'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The attributes that should be mutated to dates. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $dates = [ |
42
|
|
|
'access_token_expires_at', |
43
|
|
|
'refresh_token_expires_at', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The attributes that are mass assignable. |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $fillable = [ |
52
|
|
|
'access_token', |
53
|
|
|
'access_token_expires_at', |
54
|
|
|
'realm_id', |
55
|
|
|
'refresh_token', |
56
|
|
|
'refresh_token_expires_at', |
57
|
|
|
'user_id', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check if access token is valid |
62
|
|
|
* |
63
|
|
|
* A token is good for 1 hour, so if it expires greater than 1 hour from now, it is still valid |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
4 |
|
public function getHasValidAccessTokenAttribute() |
68
|
|
|
{ |
69
|
4 |
|
return $this->access_token_expires_at && Carbon::now() |
70
|
4 |
|
->lt($this->access_token_expires_at); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if refresh token is valid |
75
|
|
|
* |
76
|
|
|
* A token is good for 101 days, so if it expires greater than 101 days from now, it is still valid |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
4 |
|
public function getHasValidRefreshTokenAttribute() |
81
|
|
|
{ |
82
|
4 |
|
return $this->refresh_token_expires_at && Carbon::now() |
83
|
4 |
|
->lt($this->refresh_token_expires_at); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Parse OauthToken. |
88
|
|
|
* |
89
|
|
|
* Process the OAuth token & store it in the persistent storage |
90
|
|
|
* |
91
|
|
|
* @param OAuth2AccessToken $oauth_token |
92
|
|
|
* |
93
|
|
|
* @return Token |
94
|
|
|
* @throws SdkException |
95
|
|
|
*/ |
96
|
1 |
|
public function parseOauthToken(OAuth2AccessToken $oauth_token) |
97
|
|
|
{ |
98
|
|
|
// TODO: Deal with exception |
99
|
1 |
|
$this->access_token = $oauth_token->getAccessToken(); |
100
|
1 |
|
$this->access_token_expires_at = Carbon::parse($oauth_token->getAccessTokenExpiresAt()); |
101
|
1 |
|
$this->realm_id = $oauth_token->getRealmID(); |
102
|
1 |
|
$this->refresh_token = $oauth_token->getRefreshToken(); |
103
|
1 |
|
$this->refresh_token_expires_at = Carbon::parse($oauth_token->getRefreshTokenExpiresAt()); |
104
|
|
|
|
105
|
1 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Remove the token |
110
|
|
|
* |
111
|
|
|
* When a token is deleted, we still need a token for the client for the user. |
112
|
|
|
* |
113
|
|
|
* @return Token |
114
|
|
|
* @throws Exception |
115
|
|
|
*/ |
116
|
|
|
public function remove() |
117
|
|
|
{ |
118
|
|
|
$owner = $this->owner; |
119
|
|
|
|
120
|
|
|
$this->delete(); |
121
|
|
|
|
122
|
|
|
return $owner->quickBooksToken() |
123
|
|
|
->make(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Belongs to relation. |
128
|
|
|
* |
129
|
|
|
* @return BelongsTo |
130
|
|
|
*/ |
131
|
|
|
public function owner(): BelongsTo |
132
|
|
|
{ |
133
|
|
|
$config = config('quickbooks.eloquent') ?? config('quickbooks.user'); |
134
|
|
|
|
135
|
|
|
return $this->belongsTo($config['model'], $config['keys']['foreign'], $config['keys']['owner']); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|