AccessToken::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2017 Instagram, 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
 * Instagram.
9
 *
10
 * As with any software that integrates with the Instagram platform, your use
11
 * of this software is subject to the Instagram Developer Principles and
12
 * Policies [http://developers.Instagram.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
 */
24
namespace Maztech\Authentication;
25
26
/**
27
 * Class AccessToken
28
 *
29
 * @package Instagram
30
 */
31
class AccessToken
32
{
33
    /**
34
     * The access token value.
35
     *
36
     * @var string
37
     */
38
    protected $value = '';
39
40
    /**
41
     * Date when token expires.
42
     *
43
     * @var \DateTime|null
44
     */
45
    protected $expiresAt;
46
47
    /**
48
     * The user id value.
49
     *
50
     * @var int|null
51
     */
52
    protected $user_id;
53
54
    /**
55
     * Create a new access token entity.
56
     *
57
     * @param string $accessToken
58
     * @param int    $expiresAt
59
     */
60
    public function __construct($accessToken, $expiresAt = 0, $userId = null)
61
    {
62
        $this->value = $accessToken;
63
        if ($expiresAt) {
64
            $this->setExpiresAtFromTimeStamp($expiresAt);
65
        }
66
        $this->user_id = $userId;
67
    }
68
69
    /**
70
     * Generate an app secret proof to sign a request to Graph.
71
     *
72
     * @param string $appSecret The app secret.
73
     *
74
     * @return string
75
     */
76
    public function getAppSecretProof($appSecret)
77
    {
78
        return hash_hmac('sha256', $this->value, $appSecret);
79
    }
80
81
    /**
82
     * Getter for expiresAt.
83
     *
84
     * @return \DateTime|null
85
     */
86
    public function getExpiresAt()
87
    {
88
        return $this->expiresAt;
89
    }
90
91
    /**
92
     * Determines whether or not this is an app access token.
93
     *
94
     * @return bool
95
     */
96
    public function isAppAccessToken()
97
    {
98
        return strpos($this->value, '|') !== false;
99
    }
100
101
    /**
102
     * Determines whether or not this is a long-lived token.
103
     *
104
     * @return bool
105
     */
106
    public function isLongLived()
107
    {
108
        if ($this->expiresAt) {
109
            return $this->expiresAt->getTimestamp() > time() + (60 * 60 * 2);
110
        }
111
112
        if ($this->isAppAccessToken()) {
113
            return true;
114
        }
115
116
        return false;
117
    }
118
119
    /**
120
     * Checks the expiration of the access token.
121
     *
122
     * @return boolean|null
123
     */
124
    public function isExpired()
125
    {
126
        if ($this->getExpiresAt() instanceof \DateTime) {
127
            return $this->getExpiresAt()->getTimestamp() < time();
128
        }
129
130
        if ($this->isAppAccessToken()) {
131
            return false;
132
        }
133
134
        return null;
135
    }
136
137
    /**
138
     * Returns the access token as a string.
139
     *
140
     * @return string
141
     */
142
    public function getValue()
143
    {
144
        return $this->value;
145
    }
146
147
    /**
148
     * Returns the access token as a string.
149
     *
150
     * @return string
151
     */
152
    public function __toString()
153
    {
154
        return $this->getValue();
155
    }
156
157
    /**
158
     * Setter for expires_at.
159
     *
160
     * @param int $timeStamp
161
     */
162
    protected function setExpiresAtFromTimeStamp($timeStamp)
163
    {
164
        $dt = new \DateTime();
165
        $dt->setTimestamp($timeStamp);
166
        $this->expiresAt = $dt;
167
    }
168
169
    /**
170
     * Returns the user id as a integer.
171
     *
172
     * @return int
173
     */
174
    public function getUserId()
175
    {
176
        return $this->user_id;
177
    }
178
}
179