Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Token/Token.php

raw symbols SCIP
1 <?php
2 namespace Aws\Token;
3
4 use Aws\Identity\BearerTokenIdentity;
5
6 /**
7 * Basic implementation of the AWS Token interface that allows callers to
8 * pass in an AWS token in the constructor.
9 */
10 class Token extends BearerTokenIdentity implements TokenInterface, \Serializable
11 {
12 protected $token;
13 protected $expires;
14 protected ?TokenSource $source;
15
16 /**
17 * Constructs a new basic token object, with the specified AWS
18 * token
19 *
20 * @param string $token Security token to use
21 * @param int $expires UNIX timestamp for when the token expires
22 */
23 public function __construct(
24 $token,
25 $expires = null,
26 ?TokenSource $source = null
27 ){
28 $this->token = $token;
29 $this->expires = $expires;
30 $this->source = $source;
31 }
32
33 /**
34 * Sets the state of a token object
35 *
36 * @param array $state array containing 'token' and 'expires'
37 */
38 public static function __set_state(array $state)
39 {
40 return new self(
41 $state['token'],
42 $state['expires']
43 );
44 }
45
46 /**
47 * @return string
48 */
49 public function getToken()
50 {
51 return $this->token;
52 }
53
54 /**
55 * @return int
56 */
57 public function getExpiration()
58 {
59 return $this->expires;
60 }
61
62 /**
63 * @return string|null
64 */
65 public function getSource(): ?string
66 {
67 return $this->source?->value;
68 }
69
70 /**
71 * @return bool
72 */
73 public function isExpired()
74 {
75 return $this->expires !== null && time() >= $this->expires;
76 }
77
78 /**
79 * @return array
80 */
81 public function toArray()
82 {
83 return [
84 'token' => $this->token,
85 'expires' => $this->expires,
86 'source' => $this->source?->value
87 ];
88 }
89
90 /**
91 * @return string
92 */
93 public function serialize()
94 {
95 return json_encode($this->__serialize());
96 }
97
98 /**
99 * Sets the state of the object from serialized json data
100 */
101 public function unserialize($serialized)
102 {
103 $data = json_decode($serialized, true);
104
105 $this->__unserialize($data);
106 }
107
108 /**
109 * @return array
110 */
111 public function __serialize()
112 {
113 return $this->toArray();
114 }
115
116 /**
117 * Sets the state of this object from an array
118 */
119 public function __unserialize($data)
120 {
121 $this->token = $data['token'];
122 $this->expires = $data['expires'];
123 $this->source = isset($data['source'])
124 ? TokenSource::from($data['source'])
125 : null;
126 }
127 }