Passed
Push — master ( 6c42be...776013 )
by Igor
02:03
created

OauthToken::getHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This class represents Payever oAuth OauthToken
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Authorization
8
 * @package   Payever\Core
9
 * @author    payever GmbH <[email protected]>
10
 * @copyright 2017-2018 payever GmbH
11
 * @license   MIT <https://opensource.org/licenses/MIT>
12
 * @link      https://getpayever.com/developer/api-documentation/ Documentation
13
 */
14
15
namespace Payever\ExternalIntegration\Core\Authorization;
16
17
use Payever\ExternalIntegration\Core\Base\OauthTokenInterface;
18
use Payever\ExternalIntegration\Core\Helper\StringHelper;
19
20
/**
21
 * This class represents Payever oAuth OauthToken
22
 *
23
 * PHP version 5.4
24
 *
25
 * @category  Authorization
26
 * @package   Payever\Core
27
 * @author    Andrey Puhovsky <[email protected]>
28
 * @copyright 2017-2018 payever GmbH
29
 * @license   MIT <https://opensource.org/licenses/MIT>
30
 * @link      https://getpayever.com/developer/api-documentation/ Documentation
31
 */
32
class OauthToken implements OauthTokenInterface
33
{
34
    /**
35
     * How long refresh_token should be considered valid (in seconds)
36
     */
37
    const REFRESH_TOKEN_LIFETIME = 864000;
38
39
    /**
40
     * How long access_token should be considered valid (in seconds)
41
     *
42
     * This will save us from errors caused by heavy network latency and other delay factors
43
     */
44
    const ACCESS_TOKEN_LIFETIME = 3540;
45
46
    /** @var string $accessToken */
47
    protected $accessToken;
48
49
    /** @var string $refreshToken */
50
    protected $refreshToken;
51
52
    /** @var int $createdAt */
53
    protected $createdAt;
54
55
    /** @var int $updatedAt */
56
    protected $updatedAt;
57
58
    /** @var string $scope */
59
    protected $scope;
60
61
    /**
62
     * OauthToken constructor with optional autoload
63
     *
64
     * @param array|null $params OauthToken params
65
     *
66
     * @throws \Exception
67
     */
68
    public function __construct($params = null)
69
    {
70
        $this->createdAt = time();
71
72
        if ($params) {
73
            $this->load($params);
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     *
80
     * @throws \Exception
81
     */
82
    public function load($params)
83
    {
84
        if (is_string($params)) {
85
            $params = json_decode($params);
86
87
            if (json_last_error() !== JSON_ERROR_NONE) {
88
                throw new \Exception(json_last_error_msg(), json_last_error());
89
            }
90
        }
91
92
        foreach ($params as $key => $value) {
93
            $function = StringHelper::camelize('set_' . $key);
94
95
            if (method_exists($this, $function)) {
96
                $this->{$function}($value);
97
            }
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getHash()
107
    {
108
        return md5(json_encode($this->getParams()));
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function isExpired()
115
    {
116
        if ($this->getAccessToken() && $this->getExpiresIn() <= 0) {
117
            return true;
118
        }
119
120
        return false;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function isRefreshable()
127
    {
128
        if ($this->getUpdatedAt()) {
129
            return (time() - $this->getUpdatedAt() < self::REFRESH_TOKEN_LIFETIME);
130
        }
131
132
        return false;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function getParams()
139
    {
140
        return array(
141
            'scope'         => $this->scope,
142
            'access_token'  => $this->accessToken,
143
            'refresh_token' => $this->refreshToken,
144
            'created_at'    => $this->createdAt,
145
            'updated_at'    => $this->updatedAt,
146
        );
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getAuthorizationString()
153
    {
154
        return sprintf("Authorization: Bearer %s", $this->getAccessToken());
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function getExpiresIn()
161
    {
162
        if ($this->getUpdatedAt()) {
163
            return max(0, ($this->getUpdatedAt() - time()) + self::ACCESS_TOKEN_LIFETIME);
164
        }
165
166
        return 0;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function getUpdatedAt()
173
    {
174
        return $this->updatedAt;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function getCreatedAt()
181
    {
182
        return $this->createdAt;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function getRefreshToken()
189
    {
190
        return $this->refreshToken;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function getAccessToken()
197
    {
198
        return $this->accessToken;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function getScope()
205
    {
206
        return $this->scope;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public static function getScopes()
213
    {
214
        return array(
215
            static::SCOPE_PAYMENT_ACTIONS,
216
            static::SCOPE_CREATE_PAYMENT,
217
            static::SCOPE_PAYMENT_INFO,
218
        );
219
    }
220
221
    /**
222
     * @inheritdoc
223
     */
224
    public static function getGrandTypes()
225
    {
226
        return array(
227
            static::GRAND_TYPE_REFRESH_TOKEN,
228
            static::GRAND_TYPE_OBTAIN_TOKEN,
229
        );
230
    }
231
232
    /**
233
     * Sets Updated At
234
     *
235
     * @param int|null $updatedAt
236
     *
237
     * @return static
238
     */
239
    public function setUpdatedAt($updatedAt = null)
240
    {
241
        $this->updatedAt = $updatedAt ?: time();
242
243
        return $this;
244
    }
245
246
    /**
247
     * Sets Created At
248
     *
249
     * @param int|null $createdAt
250
     *
251
     * @return static
252
     */
253
    public function setCreatedAt($createdAt = null)
254
    {
255
        $this->createdAt = $createdAt ?: time();
256
257
        return $this;
258
    }
259
260
    /**
261
     * Sets Refresh OauthToken
262
     *
263
     * @param string $refreshToken
264
     *
265
     * @return static
266
     */
267
    public function setRefreshToken($refreshToken)
268
    {
269
        $this->refreshToken = $refreshToken;
270
271
        return $this;
272
    }
273
274
    /**
275
     * Sets Access OauthToken
276
     *
277
     * @param string $accessToken
278
     *
279
     * @return static
280
     */
281
    public function setAccessToken($accessToken)
282
    {
283
        $this->accessToken = $accessToken;
284
285
        return $this;
286
    }
287
288
    /**
289
     * Sets Scope
290
     *
291
     * @param string $scope
292
     *
293
     * @return static
294
     */
295
    public function setScope($scope)
296
    {
297
        $this->scope = $scope;
298
299
        return $this;
300
    }
301
}
302