| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\MyYoast_Client\Domain; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use SensitiveParameter; |
| 7 |
|
| 8 |
/** |
| 9 |
* Immutable value object representing the result of Dynamic Client Registration. |
| 10 |
* |
| 11 |
* Stores the client_id, registration access token, and management endpoint URI |
| 12 |
* received from the authorization server during DCR (RFC 7591). |
| 13 |
*/ |
| 14 |
class Registered_Client { |
| 15 |
|
| 16 |
/** |
| 17 |
* The registered OAuth client ID. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
private $client_id; |
| 22 |
|
| 23 |
/** |
| 24 |
* The registration access token for RFC 7592 management operations. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $registration_access_token; |
| 29 |
|
| 30 |
/** |
| 31 |
* The management endpoint URL for this client registration. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $registration_client_uri; |
| 36 |
|
| 37 |
/** |
| 38 |
* Additional metadata from the registration response. |
| 39 |
* |
| 40 |
* @var array<string, string|array<string>> |
| 41 |
*/ |
| 42 |
private $metadata; |
| 43 |
|
| 44 |
/** |
| 45 |
* Registered_Client constructor. |
| 46 |
* |
| 47 |
* @param string $client_id The registered client ID. |
| 48 |
* @param string $registration_access_token The registration access token. |
| 49 |
* @param string $registration_client_uri The management endpoint URL. |
| 50 |
* @param array<string, string|array<string>> $metadata Additional metadata from the registration response. |
| 51 |
* |
| 52 |
* @throws InvalidArgumentException If client_id is empty. |
| 53 |
*/ |
| 54 |
public function __construct( |
| 55 |
string $client_id, |
| 56 |
// phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPNativeAttributeFound -- No-op on PHP < 8.2; redacts parameter from stack traces on PHP 8.2+. |
| 57 |
#[SensitiveParameter] |
| 58 |
string $registration_access_token, |
| 59 |
string $registration_client_uri, |
| 60 |
array $metadata = [] |
| 61 |
) { |
| 62 |
if ( $client_id === '' ) { |
| 63 |
throw new InvalidArgumentException( 'Registered_Client requires a non-empty client_id.' ); |
| 64 |
} |
| 65 |
|
| 66 |
$this->client_id = $client_id; |
| 67 |
$this->registration_access_token = $registration_access_token; |
| 68 |
$this->registration_client_uri = $registration_client_uri; |
| 69 |
$this->metadata = $metadata; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the registered client ID. |
| 74 |
* |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function get_client_id(): string { |
| 78 |
return $this->client_id; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns the registration access token. |
| 83 |
* |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
public function get_registration_access_token(): string { |
| 87 |
return $this->registration_access_token; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Returns the management endpoint URL. |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
public function get_registration_client_uri(): string { |
| 96 |
return $this->registration_client_uri; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns additional metadata from the registration response. |
| 101 |
* |
| 102 |
* @return array<string, string|array<string>> |
| 103 |
*/ |
| 104 |
public function get_metadata(): array { |
| 105 |
return $this->metadata; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Converts the DTO to an associative array for storage. |
| 110 |
* |
| 111 |
* @return array<string, string|array<string>> |
| 112 |
*/ |
| 113 |
public function to_array(): array { |
| 114 |
return [ |
| 115 |
'client_id' => $this->client_id, |
| 116 |
'registration_access_token' => $this->registration_access_token, |
| 117 |
'registration_client_uri' => $this->registration_client_uri, |
| 118 |
'metadata' => $this->metadata, |
| 119 |
]; |
| 120 |
} |
| 121 |
} |
| 122 |
|