1: <?php
2: namespace Worldline\Acquiring\Sdk\Authentication;
3:
4: /**
5: * Class DefaultOAuth2TokenCache
6: *
7: * @package Worldline\Acquiring\Sdk\Authentication
8: */
9: class DefaultOAuth2TokenCache implements OAuth2TokenCache
10: {
11: const OAUTH2_ACCESS_TOKEN_PREFIX = 'acquiring.api.oauth2.access-token';
12: const EXPIRATION_TIMESTAMP_PREFIX = 'acquiring.api.oauth2.expiration-timestamp';
13:
14: /** @var array<string, string> */
15: private $array;
16:
17: /**
18: * @param array<string, string>$array an array in which the cached OAuth2 access token and expiration timestamp will be stored.
19: */
20: public function __construct(&$array = array())
21: {
22: $this->array = &$array;
23: }
24:
25: /**
26: * @param string $tokenType The token type that was derived from the URI path.
27: * @return string|null The currently cached OAuth2 access token, or null if not set or expired.
28: */
29: public function getOAuth2AccessToken($tokenType)
30: {
31: $accessTokenKey = self::OAUTH2_ACCESS_TOKEN_PREFIX . $tokenType;
32: $timestampKey = self::EXPIRATION_TIMESTAMP_PREFIX . $tokenType;
33: if (isset($this->array[$accessTokenKey]) && isset($this->array[$timestampKey])) {
34: $expirationTimestamp = intval($this->array[$timestampKey]);
35: if ($expirationTimestamp > time()) {
36: return $this->array[$accessTokenKey];
37: }
38: }
39: return null;
40: }
41:
42: /**
43: * @param string $tokenType The token type that was derived from the URI path.
44: * @param string $oauth2AccessToken The OAuth2 access token to store.
45: * @param int $expirationTimestamp The timestamp the OAuth2 access token expires, as a number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
46: */
47: public function storeOAuth2AccessToken($tokenType, $oauth2AccessToken, $expirationTimestamp)
48: {
49: $this->array[self::OAUTH2_ACCESS_TOKEN_PREFIX . $tokenType] = $oauth2AccessToken;
50: $this->array[self::EXPIRATION_TIMESTAMP_PREFIX . $tokenType] = strval($expirationTimestamp);
51: }
52:
53: }
54: