AccessToken::isExpired()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 */
23
namespace Facebook\Authentication;
24
25
/**
26
 * @package Facebook
27
 */
28
class AccessToken
29
{
30
    /**
31
     * The access token value.
32
     *
33
     * @var string
34
     */
35
    protected $value = '';
36
37
    /**
38
     * Date when token expires.
39
     *
40
     * @var null|\DateTime
41
     */
42
    protected $expiresAt;
43
44
    /**
45
     * Create a new access token entity.
46
     *
47
     * @param string $accessToken
48
     * @param int    $expiresAt
49
     */
50 46
    public function __construct($accessToken, $expiresAt = 0)
51
    {
52 46
        $this->value = $accessToken;
53 46
        if ($expiresAt) {
54 6
            $this->setExpiresAtFromTimeStamp($expiresAt);
55
        }
56 46
    }
57
58
    /**
59
     * Generate an app secret proof to sign a request to Graph.
60
     *
61
     * @param string $appSecret the app secret
62
     *
63
     * @return string
64
     */
65 32
    public function getAppSecretProof($appSecret)
66
    {
67 32
        return hash_hmac('sha256', $this->value, $appSecret);
68
    }
69
70
    /**
71
     * Getter for expiresAt.
72
     *
73
     * @return null|\DateTime
74
     */
75 3
    public function getExpiresAt()
76
    {
77 3
        return $this->expiresAt;
78
    }
79
80
    /**
81
     * Determines whether or not this is an app access token.
82
     *
83
     * @return bool
84
     */
85 3
    public function isAppAccessToken()
86
    {
87 3
        return strpos($this->value, '|') !== false;
88
    }
89
90
    /**
91
     * Determines whether or not this is a long-lived token.
92
     *
93
     * @return bool
94
     */
95 2
    public function isLongLived()
96
    {
97 2
        if ($this->expiresAt) {
98 2
            return $this->expiresAt->getTimestamp() > time() + (60 * 60 * 2);
99
        }
100
101
        return $this->isAppAccessToken();
102
    }
103
104
    /**
105
     * Checks the expiration of the access token.
106
     *
107
     * @return null|bool
108
     */
109 2
    public function isExpired()
110
    {
111 2
        if ($this->getExpiresAt() instanceof \DateTime) {
112 1
            return $this->getExpiresAt()->getTimestamp() < time();
113
        }
114
115 1
        if ($this->isAppAccessToken()) {
116 1
            return false;
117
        }
118
119
        return null;
120
    }
121
122
    /**
123
     * Returns the access token as a string.
124
     *
125
     * @return string
126
     */
127 13
    public function getValue()
128
    {
129 13
        return $this->value;
130
    }
131
132
    /**
133
     * Returns the access token as a string.
134
     *
135
     * @return string
136
     */
137 5
    public function __toString()
138
    {
139 5
        return $this->getValue();
140
    }
141
142
    /**
143
     * Setter for expires_at.
144
     *
145
     * @param int $timeStamp
146
     */
147 6
    protected function setExpiresAtFromTimeStamp($timeStamp)
148
    {
149 6
        $dt = new \DateTime();
150 6
        $dt->setTimestamp($timeStamp);
151 6
        $this->expiresAt = $dt;
152 6
    }
153
}
154