| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\SEMrush; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Config\SEMrush_Client; |
| 6 |
use Yoast\WP\SEO\Exceptions\OAuth\Authentication_Failed_Exception; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class SEMrush_Login_Action |
| 10 |
*/ |
| 11 |
class SEMrush_Login_Action { |
| 12 |
|
| 13 |
/** |
| 14 |
* The SEMrush_Client instance. |
| 15 |
* |
| 16 |
* @var SEMrush_Client |
| 17 |
*/ |
| 18 |
protected $client; |
| 19 |
|
| 20 |
/** |
| 21 |
* SEMrush_Login_Action constructor. |
| 22 |
* |
| 23 |
* @param SEMrush_Client $client The API client. |
| 24 |
*/ |
| 25 |
public function __construct( SEMrush_Client $client ) { |
| 26 |
$this->client = $client; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Authenticates with SEMrush to request the necessary tokens. |
| 31 |
* |
| 32 |
* @param string $code The authentication code to use to request a token with. |
| 33 |
* |
| 34 |
* @return object The response object. |
| 35 |
*/ |
| 36 |
public function authenticate( $code ) { |
| 37 |
// Code has already been validated at this point. No need to do that again. |
| 38 |
try { |
| 39 |
$tokens = $this->client->request_tokens( $code ); |
| 40 |
|
| 41 |
return (object) [ |
| 42 |
'tokens' => $tokens->to_array(), |
| 43 |
'status' => 200, |
| 44 |
]; |
| 45 |
} catch ( Authentication_Failed_Exception $e ) { |
| 46 |
return $e->get_response(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Performs the login request, if necessary. |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function login() { |
| 56 |
if ( $this->client->has_valid_tokens() ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
// Prompt with login screen. |
| 61 |
} |
| 62 |
} |
| 63 |
|