Completed
Branch master (bda8d5)
by Дмитрий
02:19
created

AccessToken::__construct()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8.125

Importance

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