Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.44% |
34 / 36 |
|
92.31% |
24 / 26 |
CRAP | |
0.00% |
0 / 1 |
| AbstractHttpClient | |
94.44% |
34 / 36 |
|
92.31% |
24 / 26 |
32.18 | |
0.00% |
0 / 1 |
| setHandler | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getHandler | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasHandler | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setClientId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getClientId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasClientId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setClientSecret | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getClientSecret | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasClientSecret | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setScope | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getScope | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasScope | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setAccountId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getAccountId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasAccountId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setUsername | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getUsername | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasUsername | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setToken | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setTokenExpires | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getTokenExpires | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasTokenExpires | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isTokenExpired | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| verifyToken | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
6 | |||
| requestToken | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <dev@noladev.com> |
| 8 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Mail\Api; |
| 16 | |
| 17 | use Pop\Http; |
| 18 | |
| 19 | /** |
| 20 | * Abstract HTTP class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Mail |
| 24 | * @author Nick Sagona, III <dev@noladev.com> |
| 25 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 5.0.0 |
| 28 | */ |
| 29 | abstract class AbstractHttpClient extends AbstractHttp implements HttpClientInterface |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Client ID |
| 34 | * @var ?string |
| 35 | */ |
| 36 | protected ?string $clientId = null; |
| 37 | |
| 38 | /** |
| 39 | * Client Secret |
| 40 | * @var ?string |
| 41 | */ |
| 42 | protected ?string $clientSecret = null; |
| 43 | |
| 44 | /** |
| 45 | * Scope |
| 46 | * @var ?string |
| 47 | */ |
| 48 | protected ?string $scope = null; |
| 49 | |
| 50 | /** |
| 51 | * Account ID |
| 52 | * @var ?string |
| 53 | */ |
| 54 | protected ?string $accountId = null; |
| 55 | |
| 56 | /** |
| 57 | * Account username |
| 58 | * @var ?string |
| 59 | */ |
| 60 | protected ?string $username = null; |
| 61 | |
| 62 | /** |
| 63 | * Token |
| 64 | * @var ?string |
| 65 | */ |
| 66 | protected ?string $token = null; |
| 67 | |
| 68 | /** |
| 69 | * Token expires |
| 70 | * @var ?string |
| 71 | */ |
| 72 | protected ?string $tokenExpires = null; |
| 73 | |
| 74 | /** |
| 75 | * Token request URI |
| 76 | * @var ?string |
| 77 | */ |
| 78 | protected ?string $tokenRequestUri = null; |
| 79 | |
| 80 | /** |
| 81 | * Base URL |
| 82 | * @var ?string |
| 83 | */ |
| 84 | protected ?string $baseUri = null; |
| 85 | |
| 86 | /** |
| 87 | * Request handler, injectable so callers (tests) can intercept the |
| 88 | * internal token-request client requestToken() builds itself - that |
| 89 | * client is never exposed via getClient()/setClient() |
| 90 | * @var ?Http\Client\Handler\HandlerInterface |
| 91 | */ |
| 92 | protected ?Http\Client\Handler\HandlerInterface $handler = null; |
| 93 | |
| 94 | /** |
| 95 | * Set request handler |
| 96 | * |
| 97 | * @param Http\Client\Handler\HandlerInterface $handler |
| 98 | * @return AbstractHttpClient |
| 99 | */ |
| 100 | public function setHandler(Http\Client\Handler\HandlerInterface $handler): AbstractHttpClient |
| 101 | { |
| 102 | $this->handler = $handler; |
| 103 | return $this; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get request handler |
| 108 | * |
| 109 | * @return ?Http\Client\Handler\HandlerInterface |
| 110 | */ |
| 111 | public function getHandler(): ?Http\Client\Handler\HandlerInterface |
| 112 | { |
| 113 | return $this->handler; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Has request handler |
| 118 | * |
| 119 | * @return bool |
| 120 | */ |
| 121 | public function hasHandler(): bool |
| 122 | { |
| 123 | return ($this->handler !== null); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Set client ID |
| 128 | * |
| 129 | * @param string $clientId |
| 130 | * @return AbstractHttpClient |
| 131 | */ |
| 132 | public function setClientId(string $clientId): AbstractHttpClient |
| 133 | { |
| 134 | $this->clientId = $clientId; |
| 135 | return $this; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get client ID |
| 140 | * |
| 141 | * @return ?string |
| 142 | */ |
| 143 | public function getClientId(): ?string |
| 144 | { |
| 145 | return $this->clientId; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Has client ID |
| 150 | * |
| 151 | * @return bool |
| 152 | */ |
| 153 | public function hasClientId(): bool |
| 154 | { |
| 155 | return ($this->clientId !== null); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Set client secret |
| 160 | * |
| 161 | * @param string $clientSecret |
| 162 | * @return AbstractHttpClient |
| 163 | */ |
| 164 | public function setClientSecret(string $clientSecret): AbstractHttpClient |
| 165 | { |
| 166 | $this->clientSecret = $clientSecret; |
| 167 | return $this; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get client ID |
| 172 | * |
| 173 | * @return ?string |
| 174 | */ |
| 175 | public function getClientSecret(): ?string |
| 176 | { |
| 177 | return $this->clientSecret; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Has client ID |
| 182 | * |
| 183 | * @return bool |
| 184 | */ |
| 185 | public function hasClientSecret(): bool |
| 186 | { |
| 187 | return ($this->clientSecret !== null); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * Set scope |
| 193 | * @param string $scope |
| 194 | * @return AbstractHttpClient |
| 195 | */ |
| 196 | public function setScope(string $scope): AbstractHttpClient |
| 197 | { |
| 198 | $this->scope = $scope; |
| 199 | return $this; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Get scope ID |
| 204 | * |
| 205 | * @return ?string |
| 206 | */ |
| 207 | public function getScope(): ?string |
| 208 | { |
| 209 | return $this->scope; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Has scope ID |
| 214 | * |
| 215 | * @return bool |
| 216 | */ |
| 217 | public function hasScope(): bool |
| 218 | { |
| 219 | return ($this->scope !== null); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Set account ID |
| 224 | * |
| 225 | * @param string $accountId |
| 226 | * @return AbstractHttpClient |
| 227 | */ |
| 228 | public function setAccountId(string $accountId): AbstractHttpClient |
| 229 | { |
| 230 | $this->accountId = $accountId; |
| 231 | return $this; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Get account ID |
| 236 | * |
| 237 | * @return ?string |
| 238 | */ |
| 239 | public function getAccountId(): ?string |
| 240 | { |
| 241 | return $this->accountId; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Has account ID |
| 246 | * |
| 247 | * @return bool |
| 248 | */ |
| 249 | public function hasAccountId(): bool |
| 250 | { |
| 251 | return ($this->accountId !== null); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Set account username |
| 256 | * |
| 257 | * @param string $username |
| 258 | * @return AbstractHttpClient |
| 259 | */ |
| 260 | public function setUsername(string $username): AbstractHttpClient |
| 261 | { |
| 262 | $this->username = $username; |
| 263 | return $this; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Get account username |
| 268 | * |
| 269 | * @return ?string |
| 270 | */ |
| 271 | public function getUsername(): ?string |
| 272 | { |
| 273 | return $this->username; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Has account username |
| 278 | * |
| 279 | * @return bool |
| 280 | */ |
| 281 | public function hasUsername(): bool |
| 282 | { |
| 283 | return ($this->username !== null); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Set token |
| 288 | * |
| 289 | * @param string $token |
| 290 | * @return AbstractHttpClient |
| 291 | */ |
| 292 | public function setToken(string $token): AbstractHttpClient |
| 293 | { |
| 294 | $this->token = $token; |
| 295 | return $this; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Get token |
| 300 | * |
| 301 | * @return ?string |
| 302 | */ |
| 303 | public function getToken(): ?string |
| 304 | { |
| 305 | return $this->token; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Has token |
| 310 | * |
| 311 | * @return bool |
| 312 | */ |
| 313 | public function hasToken(): bool |
| 314 | { |
| 315 | return ($this->token !== null); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Set token expires |
| 320 | * |
| 321 | * @param string $tokenExpires |
| 322 | * @return AbstractHttpClient |
| 323 | */ |
| 324 | public function setTokenExpires(string $tokenExpires): AbstractHttpClient |
| 325 | { |
| 326 | $this->tokenExpires = $tokenExpires; |
| 327 | return $this; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Get token expires |
| 332 | * |
| 333 | * @return ?string |
| 334 | */ |
| 335 | public function getTokenExpires(): ?string |
| 336 | { |
| 337 | return $this->tokenExpires; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Has token expires |
| 342 | * |
| 343 | * @return bool |
| 344 | */ |
| 345 | public function hasTokenExpires(): bool |
| 346 | { |
| 347 | return ($this->tokenExpires !== null); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Check if token is expired |
| 352 | * |
| 353 | * @return boolean |
| 354 | */ |
| 355 | public function isTokenExpired(): bool |
| 356 | { |
| 357 | return (((int)$this->tokenExpires > 0) && ($this->tokenExpires <= time())); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Verify token and refresh is expired |
| 362 | * |
| 363 | * @return boolean |
| 364 | */ |
| 365 | public function verifyToken(): bool |
| 366 | { |
| 367 | if (!isset($this->token) || !isset($this->tokenExpires) || ($this->isTokenExpired())) { |
| 368 | $this->requestToken(); |
| 369 | } |
| 370 | |
| 371 | return (isset($this->token) && isset($this->tokenExpires) && (!$this->isTokenExpired())); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Request token |
| 376 | * |
| 377 | * @throws Exception|Http\Exception|Http\Client\Exception|Http\Client\Handler\Exception |
| 378 | * @return AbstractHttpClient |
| 379 | */ |
| 380 | abstract public function requestToken(): AbstractHttpClient; |
| 381 | |
| 382 | } |