date_helper = $date_helper; $this->user_helper = $user_helper; } /** * Store the verification code for a user. * * @param int $user_id The user ID. * @param string $code The code verifier. * @param int $created_at The time the code was created. * * @return void */ public function store_code_verifier( int $user_id, string $code, int $created_at ): void { $this->user_helper->update_meta( $user_id, 'yoast_wpseo_ai_generator_code_verifier_for_blog_' . \get_current_blog_id(), [ 'code' => $code, 'created_at' => $created_at, ], ); } /** * Get the verification code for a user. * * @param int $user_id The user ID. * * @throws RuntimeException If the code verifier is not found or has expired. * @return Code_Verifier The verification code or null if not found. */ public function get_code_verifier( int $user_id ): ?Code_Verifier { $data = $this->user_helper->get_meta( $user_id, 'yoast_wpseo_ai_generator_code_verifier_for_blog_' . \get_current_blog_id(), true ); if ( ! \is_array( $data ) || ! isset( $data['code'] ) || $data['code'] === '' ) { throw new RuntimeException( 'Unable to retrieve the verification code.' ); } if ( ! isset( $data['created_at'] ) || $data['created_at'] < ( $this->date_helper->current_time() - self::CODE_VERIFIER_VALIDITY ) ) { $this->delete_code_verifier( $user_id ); throw new RuntimeException( 'Code verifier has expired.' ); } return new Code_Verifier( $data['code'], $data['created_at'] ); } /** * Delete the verification code for a user. * * @param int $user_id The user ID. * * @return void */ public function delete_code_verifier( int $user_id ): void { $this->user_helper->delete_meta( $user_id, 'yoast_wpseo_ai_generator_code_verifier_for_blog_' . \get_current_blog_id() ); } }