| 1 |
<?php |
| 2 |
namespace Depicter\Services; |
| 3 |
|
| 4 |
use Averta\WordPress\Utility\JSON; |
| 5 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 6 |
|
| 7 |
class ClientService |
| 8 |
{ |
| 9 |
/** |
| 10 |
* ClientService constructor. |
| 11 |
*/ |
| 12 |
public function __construct(){ |
| 13 |
if ( empty( \Depicter::options()->get( 'version_initial' ) ) ) { |
| 14 |
\Depicter::options()->set( 'version_initial', DEPICTER_VERSION ); |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Register client info |
| 20 |
* |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public function authorize() { |
| 24 |
|
| 25 |
if ( ! empty( \Depicter::cache('base')->get( 'is_client_registered' ) ) ) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
$params = [ |
| 30 |
'form_params' => [ |
| 31 |
'version' => DEPICTER_VERSION, |
| 32 |
'version_initial' => \Depicter::options()->get('version_initial'), |
| 33 |
'info' => \Depicter::options()->get('info') |
| 34 |
] |
| 35 |
]; |
| 36 |
|
| 37 |
try{ |
| 38 |
$endpoint = \Depicter::remote()->endpoint() . 'v1/client/register'; |
| 39 |
$response = \Depicter::remote()->post( $endpoint, $params ); |
| 40 |
|
| 41 |
if ( $response->getStatusCode() == 200 || $response->getStatusCode() == 204 ) { |
| 42 |
$payload = JSON::decode( $response->getBody(), true ); |
| 43 |
|
| 44 |
if ( ! empty( $payload['client_key'] ) ) { |
| 45 |
\Depicter::options()->set( 'client_key', $payload['client_key'] ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
} catch ( GuzzleException $e ) { |
| 50 |
try{ |
| 51 |
$this->reportError([ |
| 52 |
'issueType' => 'depicter-wp-api', |
| 53 |
'issueRelatesTo' => 'client-registration', |
| 54 |
'userDescription' => $e->getMessage(). ' | ' . $params['headers']['user-agent'] |
| 55 |
]); |
| 56 |
} catch( GuzzleException $e ){} |
| 57 |
} |
| 58 |
|
| 59 |
\Depicter::cache('base')->set( 'is_client_registered', true, DAY_IN_SECONDS ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Send user feedback |
| 64 |
* |
| 65 |
* @param array $bodyParams |
| 66 |
* |
| 67 |
* @return bool |
| 68 |
* @throws GuzzleException |
| 69 |
*/ |
| 70 |
public function reportIssue( $bodyParams = [] ) { |
| 71 |
$response = \Depicter::remote()->post( 'v1/report/issue', [ |
| 72 |
'form_params' => $bodyParams |
| 73 |
]); |
| 74 |
return $response->getStatusCode() == 200; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Send user error reports |
| 79 |
* |
| 80 |
* @param array $bodyParams |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
* @throws GuzzleException |
| 84 |
*/ |
| 85 |
public function reportError( $bodyParams = [] ) { |
| 86 |
$response = \Depicter::remote()->post( 'v1/report/error', [ |
| 87 |
'form_params' => $bodyParams |
| 88 |
]); |
| 89 |
return $response->getStatusCode() == 200; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* send subscriber |
| 94 |
* @param array $bodyParams |
| 95 |
* |
| 96 |
* @return bool |
| 97 |
* @throws GuzzleException |
| 98 |
*/ |
| 99 |
public function subscribe( $bodyParams = [] ) { |
| 100 |
$response = \Depicter::remote()->post( 'v1/subscriber/store', [ |
| 101 |
'form_params' => $bodyParams |
| 102 |
]); |
| 103 |
return $response->getStatusCode() == 200; |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
|