Jake Vanderwerf
5 days ago 0dfe1d8afafc59c4a5559c498342668d5a58d6ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace JVBase\integrations;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class OAuthCredentials
{
    public string $access_token;
    public int $expires_at;
    public ?string $refresh_token;
    public ?string $revoke;
    public array $scopes;
 
    public function __construct(array $data)
    {
        $this->access_token = $data['access_token'] ?? '';
        $this->expires_at = (int)($data['expires_at'] ?? 0);
        $this->refresh_token = $data['refresh_token'] ?? null;
        $this->scopes = $data['scopes'] ?? [];
    }
 
    public function isValid(): bool
    {
        return !empty($this->access_token) && $this->expires_at > 0;
    }
 
    public function isExpired(int $buffer = 60): bool
    {
        return $this->expires_at <= (time() + $buffer);
    }
 
    public function hasScopes(array $required): bool
    {
        return empty(array_diff($required, $this->scopes));
    }
 
    public function toArray(): array
    {
        return [
            'access_token'  => $this->access_token,
            'expires_at'    => $this->expires_at,
            'refresh_token' => $this->refresh_token,
//          'scopes'        => $this->scopes,
        ];
    }
}