PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.8
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / values / oauth / oauth-token.php

oauth-token.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.8, at src/values/oauth/oauth-token.php

130 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Values\OAuth;
4
5 use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Property_Exception;
6 use YoastSEO_Vendor\League\OAuth2\Client\Token\AccessTokenInterface;
7
8 /**
9 * Class OAuth_Token
10 */
11 class OAuth_Token {
12
13 /**
14 * The access token.
15 *
16 * @var string
17 */
18 public $access_token;
19
20 /**
21 * The refresh token.
22 *
23 * @var string
24 */
25 public $refresh_token;
26
27 /**
28 * The expiration date.
29 *
30 * @var int
31 */
32 public $expires;
33
34 /**
35 * Whether or not the token has expired.
36 *
37 * @var bool
38 */
39 public $has_expired;
40
41 /**
42 * The timestamp at which the token was created.
43 *
44 * @var int
45 */
46 public $created_at;
47
48 /**
49 * OAuth_Token constructor.
50 *
51 * @param string $access_token The access token.
52 * @param string $refresh_token The refresh token.
53 * @param int $expires The date and time at which the token will expire.
54 * @param bool $has_expired Whether or not the token has expired.
55 * @param int $created_at The timestamp of when the token was created.
56 *
57 * @throws Empty_Property_Exception Exception thrown if a token property is empty.
58 */
59 public function __construct( $access_token, $refresh_token, $expires, $has_expired, $created_at ) {
60
61 if ( empty( $access_token ) ) {
62 throw new Empty_Property_Exception( 'access_token' );
63 }
64
65 $this->access_token = $access_token;
66
67 if ( empty( $access_token ) ) {
68 throw new Empty_Property_Exception( 'refresh_token' );
69 }
70
71 $this->refresh_token = $refresh_token;
72
73 if ( empty( $expires ) ) {
74 throw new Empty_Property_Exception( 'expires' );
75 }
76
77 $this->expires = $expires;
78
79 if ( \is_null( $has_expired ) ) {
80 throw new Empty_Property_Exception( 'has_expired' );
81 }
82
83 $this->has_expired = $has_expired;
84 $this->created_at = $created_at;
85 }
86
87 /**
88 * Creates a new instance based on the passed response.
89 *
90 * @param AccessTokenInterface $response The response object to create a new instance from.
91 *
92 * @return OAuth_Token The token object.
93 *
94 * @throws Empty_Property_Exception Exception thrown if a token property is empty.
95 */
96 public static function from_response( AccessTokenInterface $response ) {
97 return new self(
98 $response->getToken(),
99 $response->getRefreshToken(),
100 $response->getExpires(),
101 $response->hasExpired(),
102 \time()
103 );
104 }
105
106 /**
107 * Determines whether or not the token has expired.
108 *
109 * @return bool Whether or not the token has expired.
110 */
111 public function has_expired() {
112 return ( \time() >= $this->expires ) || $this->has_expired === true;
113 }
114
115 /**
116 * Converts the object to an array.
117 *
118 * @return array The converted object.
119 */
120 public function to_array() {
121 return [
122 'access_token' => $this->access_token,
123 'refresh_token' => $this->refresh_token,
124 'expires' => $this->expires,
125 'has_expired' => $this->has_expired(),
126 'created_at' => $this->created_at,
127 ];
128 }
129 }
130