AccessToken::__construct()   B
last analyzed

Complexity

Conditions 9
Paths 22

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9.3752

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 9
eloc 17
c 4
b 0
f 0
nc 22
nop 1
dl 0
loc 35
ccs 15
cts 18
cp 0.8333
crap 9.3752
rs 8.0555
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
declare(strict_types=1);
7
8
namespace SocialConnect\OAuth2;
9
10
use SocialConnect\Provider\AccessTokenInterface;
11
use SocialConnect\Provider\Exception\InvalidAccessToken;
12
13
class AccessToken implements AccessTokenInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $token;
19
20
    /**
21
     * @var string|null
22
     */
23
    protected $refreshToken;
24
25
    /**
26
     * @var int|null
27
     */
28
    protected $expires;
29
30
    /**
31
     * @var string|null
32
     */
33
    protected $uid;
34
35
    /**
36
     * @param array $token
37
     * @throws InvalidAccessToken
38
     */
39 102
    public function __construct(array $token)
40
    {
41 102
        if (!isset($token['access_token'])) {
42 1
            throw new InvalidAccessToken(
43 1
                'API returned data without access_token field'
44
            );
45
        }
46
47 101
        $this->token = $token['access_token'];
48
49
        // Show preference to 'expires_in' since it is defined in RFC6749 Section 5.1.
50
        // Defer to 'expires' if it is provided instead.
51 101
        if (isset($token['expires_in'])) {
52 1
            if (!is_numeric($token['expires_in'])) {
53
                throw new InvalidAccessToken('expires_in value must be an integer');
54
            }
55
56 1
            $this->expires = $token['expires_in'] != 0 ? time() + $token['expires_in'] : 0;
57 100
        } elseif (!empty($token['expires'])) {
58
            // Some providers supply the seconds until expiration rather than
59
            // the exact timestamp. Take a best guess at which we received.
60 3
            $expires = $token['expires'];
61 3
            if (!$this->isExpirationTimestamp($expires)) {
62
                $expires += time();
63
            }
64
65 3
            $this->expires = $expires;
66
        }
67
68 101
        if (isset($token['user_id'])) {
69 25
            $this->uid = (string) $token['user_id'];
70
        }
71
72 101
        if (isset($token['refresh_token'])) {
73
            $this->refreshToken = $token['refresh_token'];
74
        }
75 101
    }
76
77
    /**
78
     * Check if a value is an expiration timestamp or second value.
79
     *
80
     * @param integer $value
81
     * @return bool
82
     */
83 3
    protected function isExpirationTimestamp($value)
84
    {
85
        // If the given value is larger than the original OAuth 2 draft date,
86
        // assume that it is meant to be a (possible expired) timestamp.
87 3
        $oauth2InceptionDate = 1349067600; // 2012-10-01
88 3
        return ($value > $oauth2InceptionDate);
89
    }
90
91
    /**
92
     * @return string
93
     */
94 99
    public function getToken()
95
    {
96 99
        return $this->token;
97
    }
98
99
    /**
100
     * @param string $uid
101
     */
102 3
    public function setUserId(string $uid)
103
    {
104 3
        $this->uid = $uid;
105 3
    }
106
107
    /**
108
     * @return string|null
109
     */
110 31
    public function getUserId()
111
    {
112 31
        return $this->uid;
113
    }
114
115
    /**
116
     * @return int|null
117
     */
118 4
    public function getExpires()
119
    {
120 4
        return $this->expires;
121
    }
122
123
    /**
124
     * @return string|null
125
     */
126
    public function getRefreshToken(): ?string
127
    {
128
        return $this->refreshToken;
129
    }
130
}
131