AccessToken::isValidWithOffset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Mailxpert\APIBundle\Model;
4
5
use Mailxpert\Authentication\AccessToken as SDKAccessToken;
6
7
class AccessToken implements AccessTokenInterface
8
{
9
    const VALID_OFFSET = 300;
10
11
    protected $id;
12
13
    /**
14
     * @var string
15
     */
16
    protected $accessToken;
17
18
    /**
19
     * @var string
20
     */
21
    protected $refreshToken;
22
23
    /**
24
     * @var int
25
     */
26
    protected $expireAt;
27
28
    /**
29
     * @var int
30
     */
31
32
    protected $refreshTokenExpireAt;
33
34
    /**
35
     * @var string
36
     */
37
    protected $scope;
38
39
    /**
40
     * @return string
41
     */
42
    public function __toString()
43
    {
44
        return (string) $this->getAccessToken();
45
    }
46
47
    /**
48
     * @return mixed
49
     */
50
    public function getId()
51
    {
52
        return $this->id;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getAccessToken()
59
    {
60
        return $this->accessToken;
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getRefreshToken()
67
    {
68
        return $this->refreshToken;
69
    }
70
71
    /**
72
     * @return int
73
     */
74
    public function getExpireAt()
75
    {
76
        return $this->expireAt;
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function isValid()
83
    {
84
        return (bool) (time() < $this->getExpireAt());
85
    }
86
87
    public function isValidWithOffset($offset = self::VALID_OFFSET)
88
    {
89
        return time() < ($this->getExpireAt() - $offset);
90
    }
91
92
    /**
93
     * @return int
94
     */
95
    public function getRefreshTokenExpireAt()
96
    {
97
        return $this->refreshTokenExpireAt;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getScope()
104
    {
105
        return $this->scope;
106
    }
107
108
    /**
109
     * @param string $accessToken
110
     *
111
     * @return AccessToken
112
     */
113
    public function setAccessToken($accessToken)
114
    {
115
        $this->accessToken = $accessToken;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param string $refreshToken
122
     *
123
     * @return AccessToken
124
     */
125
    public function setRefreshToken($refreshToken)
126
    {
127
        $this->refreshToken = $refreshToken;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @param int $expireAt
134
     *
135
     * @return AccessToken
136
     */
137
    public function setExpireAt($expireAt)
138
    {
139
        $this->expireAt = $expireAt;
140
141
        return $this;
142
    }
143
144
    /**
145
     * @param int $refreshTokenExpireAt
146
     *
147
     * @return AccessToken
148
     */
149
    public function setRefreshTokenExpireAt($refreshTokenExpireAt)
150
    {
151
        $this->refreshTokenExpireAt = $refreshTokenExpireAt;
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param string|null $scope
158
     *
159
     * @return AccessToken
160
     */
161
    public function setScope($scope = null)
162
    {
163
        $this->scope = $scope;
164
165
        return $this;
166
    }
167
168
    /**
169
     * @param string $scope
170
     *
171
     * @return bool
172
     */
173
    public function hasScope($scope)
174
    {
175
        $scopes = explode(',', $this->getScope());
176
177
        return in_array($scope, $scopes);
178
    }
179
180
    /**
181
     * @return SDKAccessToken
182
     */
183
    public function getSDKAccessToken()
184
    {
185
        return new SDKAccessToken(
186
            $this->getAccessToken(),
187
            $this->getRefreshToken(),
188
            $this->getExpireAt(),
189
            $this->getScope(),
190
            $this->getRefreshTokenExpireAt()
191
        );
192
    }
193
194
195
}
196