AccessToken::set_expires()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Class AccessToken
4
 *
5
 * @created      09.07.2017
6
 * @author       Smiley <[email protected]>
7
 * @copyright    2017 Smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\OAuth\Core;
12
13
use chillerlan\Settings\SettingsContainerAbstract;
14
15
use function time;
16
17
/**
18
 * Access token implementation for any OAuth version.
19
 *
20
 * // Oauth1
21
 * @property string $accessTokenSecret
22
 *
23
 * // Oauth2
24
 * @property array  $scopes
25
 *
26
 * // Oauth1/2
27
 * @property string $accessToken
28
 * @property string $refreshToken
29
 * @property array  $extraParams
30
 * @property int    $expires
31
 * @property string $provider
32
 */
33
final class AccessToken extends SettingsContainerAbstract{
34
35
	/**
36
	 * Denotes an unknown end of lifetime.
37
	 */
38
	public const EOL_UNKNOWN = -9001;
39
40
	/**
41
	 * Denotes a token which never expires
42
	 */
43
	public const EOL_NEVER_EXPIRES = -9002;
44
45
	/**
46
	 * defines a maximum expiry period (1 year)
47
	 */
48
	public const EXPIRY_MAX = (86400 * 365);
49
50
	/**
51
	 * the access token secret (OAuth1)
52
	 */
53
	protected ?string $accessTokenSecret = null;
54
55
	/**
56
	 * the oauth access token
57
	 */
58
	protected ?string $accessToken = null;
59
60
	/**
61
	 * an optional refresh token (OAuth2)
62
	 */
63
	protected ?string $refreshToken = null;
64
65
	/**
66
	 * the token expiration date/time
67
	 * @todo: change to DateInterval?
68
	 */
69
	protected ?int $expires = self::EOL_UNKNOWN;
70
71
	/**
72
	 * Additional token parameters supplied by the provider
73
	 */
74
	protected array $extraParams = [];
75
76
	/**
77
	 * The scopes that are attached to this token (OAuth2)
78
	 *
79
	 * Please note that the scopes have to be stored manually after receiving the token
80
	 * as the initial auth URL request data is discarded before the callback comes in.
81
	 */
82
	protected array $scopes = [];
83
84
	/**
85
	 * the provider who issued this token
86
	 */
87
	protected ?string $provider = null;
88
89
	/**
90
	 * AccessToken constructor.
91
	 *
92
	 * @param iterable|null $properties
93
	 */
94
	public function __construct(iterable $properties = null){
95
		parent::__construct($properties);
96
97
		$this->setExpiry($this->expires);
98
	}
99
100
	/**
101
	 * @param int|null $expires
102
	 *
103
	 * @return void
104
	 */
105
	protected function set_expires(int $expires = null):void{
106
		$this->setExpiry($expires);
107
	}
108
109
	/**
110
	 * @param int|null $expires
111
	 *
112
	 * @return \chillerlan\OAuth\Core\AccessToken
113
	 */
114
	public function setExpiry(int $expires = null):AccessToken{
115
		$now = time();
116
117
		if($expires === 0 || $expires === $this::EOL_NEVER_EXPIRES){
118
			$this->expires = $this::EOL_NEVER_EXPIRES;
119
		}
120
		elseif($expires > $now){
121
			$this->expires = $expires;
122
		}
123
		elseif($expires > 0 && $expires < $this::EXPIRY_MAX){
124
			$this->expires = ($now + $expires);
125
		}
126
		else{
127
			$this->expires = $this::EOL_UNKNOWN;
128
		}
129
130
		return $this;
131
	}
132
133
	/**
134
	 * @return bool
135
	 */
136
	public function isExpired():bool{
137
		return $this->expires !== $this::EOL_NEVER_EXPIRES && $this->expires !== $this::EOL_UNKNOWN && time() > $this->expires;
138
	}
139
140
}
141