| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
|
| 5 |
namespace Yoast\WP\SEO\AI\Authorization\Infrastructure; |
| 6 |
|
| 7 |
use RuntimeException; |
| 8 |
use Yoast\WP\SEO\Helpers\User_Helper; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Refresh_Token_Repository |
| 12 |
* Handles the storage and retrieval of refresh tokens for users. |
| 13 |
*/ |
| 14 |
class Refresh_Token_User_Meta_Repository implements Refresh_Token_User_Meta_Repository_Interface { |
| 15 |
|
| 16 |
/** |
| 17 |
* The user helper. |
| 18 |
* |
| 19 |
* @var User_Helper |
| 20 |
*/ |
| 21 |
private $user_helper; |
| 22 |
|
| 23 |
/** |
| 24 |
* Refresh_Token_Repository constructor. |
| 25 |
* |
| 26 |
* @param User_Helper $user_helper The user helper. |
| 27 |
*/ |
| 28 |
public function __construct( User_Helper $user_helper ) { |
| 29 |
$this->user_helper = $user_helper; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get the token for a user. |
| 34 |
* |
| 35 |
* @param int $user_id The user ID. |
| 36 |
* |
| 37 |
* @return string The token data. |
| 38 |
* |
| 39 |
* @throws RuntimeException If the token is not found or invalid. |
| 40 |
*/ |
| 41 |
public function get_token( int $user_id ): string { |
| 42 |
$refresh_jwt = $this->user_helper->get_meta( $user_id, self::META_KEY, true ); |
| 43 |
if ( ! \is_string( $refresh_jwt ) || $refresh_jwt === '' ) { |
| 44 |
throw new RuntimeException( 'Unable to retrieve the refresh token.' ); |
| 45 |
} |
| 46 |
|
| 47 |
return $refresh_jwt; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Store the token for a user. |
| 52 |
* |
| 53 |
* @param int $user_id The user ID. |
| 54 |
* @param string $value The token value. |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function store_token( int $user_id, string $value ): void { |
| 59 |
$this->user_helper->update_meta( |
| 60 |
$user_id, |
| 61 |
self::META_KEY, |
| 62 |
$value, |
| 63 |
); } |
| 64 |
|
| 65 |
/** |
| 66 |
* Delete the token for a user. |
| 67 |
* |
| 68 |
* @param int $user_id The user ID. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public function delete_token( int $user_id ): void { |
| 73 |
$this->user_helper->delete_meta( $user_id, self::META_KEY ); |
| 74 |
} |
| 75 |
} |
| 76 |
|