| 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 ( |
| 26 |
\Depicter::cache('base')->get( 'is_client_registered' ) && |
| 27 |
\Depicter::auth()->getClientKey() |
| 28 |
) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$params = [ |
| 33 |
'form_params' => [ |
| 34 |
'version' => DEPICTER_VERSION, |
| 35 |
'version_initial' => \Depicter::options()->get('version_initial'), |
| 36 |
'info' => \Depicter::options()->get('info') |
| 37 |
] |
| 38 |
]; |
| 39 |
|
| 40 |
try{ |
| 41 |
$response = \Depicter::remote()->post( 'v1/client/register', $params ); |
| 42 |
|
| 43 |
$payload = JSON::decode( $response->getBody(), true ); |
| 44 |
|
| 45 |
if ( ! empty( $payload['client_key'] ) ) { |
| 46 |
\Depicter::options()->set( 'client_key', $payload['client_key'] ); |
| 47 |
} elseif ( ! empty( $payload['errors'] ) ) { |
| 48 |
\Depicter::options()->set('register_error_message', $payload['errors'] ); |
| 49 |
} |
| 50 |
|
| 51 |
\Depicter::cache('base')->set( 'is_client_registered', true, DAY_IN_SECONDS ); |
| 52 |
|
| 53 |
} catch ( GuzzleException|\Exception $e ) { |
| 54 |
\Depicter::options()->set('register_error_message', $e->getMessage() ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Send user feedback |
| 60 |
* |
| 61 |
* @param array $bodyParams |
| 62 |
* |
| 63 |
* @return bool |
| 64 |
* @throws GuzzleException |
| 65 |
*/ |
| 66 |
public function reportIssue( $bodyParams = [] ) { |
| 67 |
$response = \Depicter::remote()->post( 'v1/report/issue', [ |
| 68 |
'form_params' => $bodyParams |
| 69 |
]); |
| 70 |
return $response->getStatusCode() == 200; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Send user error reports |
| 75 |
* |
| 76 |
* @param array $bodyParams |
| 77 |
* |
| 78 |
* @return bool |
| 79 |
* @throws GuzzleException |
| 80 |
*/ |
| 81 |
public function reportError( $bodyParams = [] ) { |
| 82 |
$response = \Depicter::remote()->post( 'v1/report/error', [ |
| 83 |
'form_params' => $bodyParams |
| 84 |
]); |
| 85 |
return $response->getStatusCode() == 200; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* send subscriber |
| 90 |
* @param array $bodyParams |
| 91 |
* |
| 92 |
* @return bool |
| 93 |
* @throws GuzzleException |
| 94 |
*/ |
| 95 |
public function subscribe( $bodyParams = [] ) { |
| 96 |
$response = \Depicter::remote()->post( 'v1/subscriber/store', [ |
| 97 |
'form_params' => $bodyParams |
| 98 |
]); |
| 99 |
return $response->getStatusCode() == 200; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Validate user activation status |
| 105 |
*/ |
| 106 |
public function validateActivation() { |
| 107 |
try { |
| 108 |
$response = \Depicter::remote()->post( 'v1/client/validate/activation' ); |
| 109 |
$info = JSON::decode( $response->getBody(), true); |
| 110 |
|
| 111 |
if( is_null( $info['success'] ) ){ |
| 112 |
\Depicter::options()->set('activation_error_message', '' ); |
| 113 |
|
| 114 |
} elseif ( ! empty( $info['data'] ) ) { |
| 115 |
\Depicter::options()->set('subscription_status', $info['data']['status'] ); |
| 116 |
\Depicter::options()->set('subscription_expires_at', $info['data']['expires_at'] ); |
| 117 |
\Depicter::options()->set('user_tier', $info['data']['user_tier'] ); |
| 118 |
\Depicter::options()->set('activation_error_message', '' ); |
| 119 |
|
| 120 |
if ( $info['data']['status'] == 'active' ) { |
| 121 |
return true; |
| 122 |
} |
| 123 |
} elseif( $info['success'] == 1 ){ |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
if( ! empty( $info['log'] ) ) { |
| 128 |
\Depicter::options()->set('activation_log_message', $info['log'] ); |
| 129 |
} |
| 130 |
|
| 131 |
} catch ( GuzzleException $exception ) { |
| 132 |
\Depicter::options()->set('activation_error_message' , $exception->getMessage() ); |
| 133 |
\Depicter::options()->set('connection_error_message' , $exception->getMessage() ); |
| 134 |
} |
| 135 |
|
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|