Token::parseOauthToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
ccs 7
cts 7
cp 1
crap 1
1
<?php
2
3
namespace Spinen\QuickBooks;
4
5
use App\User;
0 ignored issues
show
Bug introduced by
The type App\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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