| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\Wincher; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Config\Wincher_Client; |
| 6 |
use Yoast\WP\SEO\Exceptions\OAuth\Authentication_Failed_Exception; |
| 7 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Wincher_Login_Action |
| 11 |
*/ |
| 12 |
class Wincher_Login_Action { |
| 13 |
|
| 14 |
/** |
| 15 |
* The Wincher_Client instance. |
| 16 |
* |
| 17 |
* @var Wincher_Client |
| 18 |
*/ |
| 19 |
protected $client; |
| 20 |
|
| 21 |
/** |
| 22 |
* The Options_Helper instance. |
| 23 |
* |
| 24 |
* @var Options_Helper |
| 25 |
*/ |
| 26 |
protected $options_helper; |
| 27 |
|
| 28 |
/** |
| 29 |
* Wincher_Login_Action constructor. |
| 30 |
* |
| 31 |
* @param Wincher_Client $client The API client. |
| 32 |
* @param Options_Helper $options_helper The options helper. |
| 33 |
*/ |
| 34 |
public function __construct( Wincher_Client $client, Options_Helper $options_helper ) { |
| 35 |
$this->client = $client; |
| 36 |
$this->options_helper = $options_helper; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns the authorization URL. |
| 41 |
* |
| 42 |
* @return object The response object. |
| 43 |
*/ |
| 44 |
public function get_authorization_url() { |
| 45 |
return (object) [ |
| 46 |
'status' => 200, |
| 47 |
'url' => $this->client->get_authorization_url(), |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Authenticates with Wincher to request the necessary tokens. |
| 53 |
* |
| 54 |
* @param string $code The authentication code to use to request a token with. |
| 55 |
* @param string $website_id The website id associated with the code. |
| 56 |
* |
| 57 |
* @return object The response object. |
| 58 |
*/ |
| 59 |
public function authenticate( $code, $website_id ) { |
| 60 |
// Code has already been validated at this point. No need to do that again. |
| 61 |
try { |
| 62 |
$tokens = $this->client->request_tokens( $code ); |
| 63 |
$this->options_helper->set( 'wincher_website_id', $website_id ); |
| 64 |
|
| 65 |
return (object) [ |
| 66 |
'tokens' => $tokens->to_array(), |
| 67 |
'status' => 200, |
| 68 |
]; |
| 69 |
} catch ( Authentication_Failed_Exception $e ) { |
| 70 |
return $e->get_response(); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|