OauthToken   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 268
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 32
eloc 59
c 1
b 0
f 0
dl 0
loc 268
rs 9.84

20 Methods

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