| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace Give\Vendors\LiquidWeb\LicensingApiClient\Http; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use Give\Vendors\LiquidWeb\LicensingApiClient\Exceptions\MissingAuthenticationException; |
| 7 |
use Give\Vendors\LiquidWeb\LicensingApiClient\Value\AuthToken; |
| 8 |
|
| 9 |
/** |
| 10 |
* Combines auth policy and configured token state for request execution. |
| 11 |
*/ |
| 12 |
final class AuthState |
| 13 |
{ |
| 14 |
private AuthContext $authContext; |
| 15 |
|
| 16 |
private ?AuthToken $token; |
| 17 |
|
| 18 |
/** |
| 19 |
* @throws InvalidArgumentException |
| 20 |
*/ |
| 21 |
public function __construct(AuthContext $authContext, ?AuthToken $token = null) { |
| 22 |
if ($authContext->mode() === AuthContext::MODE_EXPLICIT && $token === null) { |
| 23 |
throw new InvalidArgumentException('Explicit auth mode requires a token.'); |
| 24 |
} |
| 25 |
|
| 26 |
$this->authContext = $authContext; |
| 27 |
$this->token = $token; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @throws MissingAuthenticationException |
| 32 |
*/ |
| 33 |
public function optionalToken(): ?AuthToken { |
| 34 |
if ($this->authContext->mode() === AuthContext::MODE_NONE) { |
| 35 |
return null; |
| 36 |
} |
| 37 |
|
| 38 |
if ($this->authContext->mode() === AuthContext::MODE_CONFIGURED && $this->token === null) { |
| 39 |
throw new MissingAuthenticationException( |
| 40 |
'This request requires authentication, but no token is available.' |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
return $this->token; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @throws MissingAuthenticationException |
| 49 |
*/ |
| 50 |
public function requiredToken(): AuthToken { |
| 51 |
$token = $this->optionalToken(); |
| 52 |
|
| 53 |
if ($token === null) { |
| 54 |
throw new MissingAuthenticationException( |
| 55 |
'This request requires authentication, but no token is available.' |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
return $token; |
| 60 |
} |
| 61 |
|
| 62 |
public function withoutAuth(): self { |
| 63 |
return $this->withAuthContext(new AuthContext(AuthContext::MODE_NONE), $this->token); |
| 64 |
} |
| 65 |
|
| 66 |
public function withConfiguredToken(): self { |
| 67 |
return $this->withAuthContext(new AuthContext(AuthContext::MODE_CONFIGURED), $this->token); |
| 68 |
} |
| 69 |
|
| 70 |
public function withToken(string $token): self { |
| 71 |
return $this->withAuthContext( |
| 72 |
new AuthContext(AuthContext::MODE_EXPLICIT), |
| 73 |
new AuthToken($token) |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
public function authContext(): AuthContext { |
| 78 |
return $this->authContext; |
| 79 |
} |
| 80 |
|
| 81 |
public function token(): ?AuthToken { |
| 82 |
return $this->token; |
| 83 |
} |
| 84 |
|
| 85 |
private function withAuthContext(AuthContext $authContext, ?AuthToken $token): self { |
| 86 |
if ( |
| 87 |
$this->authContext->equals($authContext) |
| 88 |
&& ( |
| 89 |
($this->token === null && $token === null) |
| 90 |
|| ($this->token !== null && $token !== null && $this->token->equals($token)) |
| 91 |
) |
| 92 |
) { |
| 93 |
return $this; |
| 94 |
} |
| 95 |
|
| 96 |
return new self($authContext, $token); |
| 97 |
} |
| 98 |
} |
| 99 |
|