| 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( 'v2/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 |
* Get refresh token |
| 60 |
* |
| 61 |
* @param bool $force_check |
| 62 |
* @return string|bool |
| 63 |
*/ |
| 64 |
public function getRefreshToken( $force_check = false ) { |
| 65 |
|
| 66 |
$refreshToken = \Depicter::cache('base')->get( 'refresh_token', '' ); |
| 67 |
if ( !empty( $refreshToken ) && ! $force_check ) { |
| 68 |
return $refreshToken; |
| 69 |
} |
| 70 |
|
| 71 |
try{ |
| 72 |
|
| 73 |
$params = [ |
| 74 |
'form_params' => [ |
| 75 |
'version' => DEPICTER_VERSION, |
| 76 |
'version_initial' => \Depicter::options()->get('version_initial'), |
| 77 |
] |
| 78 |
]; |
| 79 |
|
| 80 |
$response = \Depicter::remote()->post( 'v2/token/refresh', $params ); |
| 81 |
|
| 82 |
$payload = JSON::decode( $response->getBody(), true ); |
| 83 |
|
| 84 |
if ( ! isset( $payload['errors'] ) && !empty( $payload['refreshToken'] ) ) { |
| 85 |
if ( ! empty( $payload['clientKey'] ) ) { |
| 86 |
\Depicter::options()->set( 'client_key', $payload['clientKey'] ); |
| 87 |
\Depicter::cache('base')->set( 'is_client_registered', true, DAY_IN_SECONDS ); |
| 88 |
} |
| 89 |
\Depicter::cache('base')->set( 'refresh_token', $payload['refreshToken'], DAY_IN_SECONDS ); |
| 90 |
return $payload['refreshToken']; |
| 91 |
} |
| 92 |
} catch ( GuzzleException|\Exception $e ) { |
| 93 |
\Depicter::options()->set('refresh_token_error_message', $e->getMessage() ); |
| 94 |
} |
| 95 |
|
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get access token |
| 101 |
* |
| 102 |
* @param bool $force_check |
| 103 |
* @return string|bool |
| 104 |
*/ |
| 105 |
public function getAccessToken( $force_check = false ) { |
| 106 |
|
| 107 |
$accessToken = \Depicter::cache('base')->get( 'access_token', '' ); |
| 108 |
if ( !empty( $accessToken ) && ! $force_check ) { |
| 109 |
return $accessToken; |
| 110 |
} |
| 111 |
|
| 112 |
try{ |
| 113 |
|
| 114 |
$response = \Depicter::remote()->post( 'v2/token/access', [ |
| 115 |
'form_params' => [ |
| 116 |
'refresh_token' => $this->getRefreshToken( $force_check ) |
| 117 |
] |
| 118 |
] ); |
| 119 |
|
| 120 |
$payload = JSON::decode( $response->getBody(), true ); |
| 121 |
|
| 122 |
if ( ! isset( $payload['errors'] ) && !empty( $payload['accessToken'] ) ) { |
| 123 |
\Depicter::cache('base')->set( 'access_token', $payload['accessToken'], DAY_IN_SECONDS ); |
| 124 |
return $payload['accessToken']; |
| 125 |
} |
| 126 |
} catch ( GuzzleException|\Exception $e ) { |
| 127 |
\Depicter::options()->set('access_token_error_message', $e->getMessage() ); |
| 128 |
} |
| 129 |
|
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get id token |
| 135 |
* |
| 136 |
* @return string|bool |
| 137 |
*/ |
| 138 |
public function getIdToken() { |
| 139 |
|
| 140 |
$idToken = \Depicter::cache('base')->get( 'id_token', '' ); |
| 141 |
if ( !empty( $idToken ) ) { |
| 142 |
return $idToken; |
| 143 |
} |
| 144 |
|
| 145 |
try{ |
| 146 |
|
| 147 |
$response = \Depicter::remote()->post( 'v2/token/id' ); |
| 148 |
|
| 149 |
$payload = JSON::decode( $response->getBody(), true ); |
| 150 |
|
| 151 |
if ( ! isset( $payload['errors'] ) && !empty( $payload['idToken'] ) ) { |
| 152 |
\Depicter::cache('base')->set( 'id_token', $payload['idToken'], MONTH_IN_SECONDS ); |
| 153 |
return $payload['idToken']; |
| 154 |
} |
| 155 |
} catch ( GuzzleException|\Exception $e ) { |
| 156 |
\Depicter::options()->set('id_token_error_message', $e->getMessage() ); |
| 157 |
} |
| 158 |
|
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Send user feedback |
| 164 |
* |
| 165 |
* @param array $bodyParams |
| 166 |
* |
| 167 |
* @return bool |
| 168 |
* @throws GuzzleException |
| 169 |
*/ |
| 170 |
public function reportIssue( $bodyParams = [] ) { |
| 171 |
$response = \Depicter::remote()->post( 'v1/report/issue', [ |
| 172 |
'form_params' => $bodyParams |
| 173 |
]); |
| 174 |
return $response->getStatusCode() == 200; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Send user error reports |
| 179 |
* |
| 180 |
* @param array $bodyParams |
| 181 |
* |
| 182 |
* @return bool |
| 183 |
* @throws GuzzleException |
| 184 |
*/ |
| 185 |
public function reportError( $bodyParams = [] ) { |
| 186 |
$response = \Depicter::remote()->post( 'v1/report/error', [ |
| 187 |
'form_params' => $bodyParams |
| 188 |
]); |
| 189 |
return $response->getStatusCode() == 200; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* send subscriber |
| 194 |
* @param array $bodyParams |
| 195 |
* |
| 196 |
* @return bool |
| 197 |
* @throws GuzzleException |
| 198 |
*/ |
| 199 |
public function subscribe( $bodyParams = [] ) { |
| 200 |
$response = \Depicter::remote()->post( 'v1/subscriber/store', [ |
| 201 |
'form_params' => $bodyParams |
| 202 |
]); |
| 203 |
return $response->getStatusCode() == 200; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/** |
| 208 |
* Validate user activation status |
| 209 |
*/ |
| 210 |
public function validateActivation() { |
| 211 |
try { |
| 212 |
$response = \Depicter::remote()->post( 'v1/client/validate/activation' ); |
| 213 |
$info = JSON::decode( $response->getBody(), true); |
| 214 |
|
| 215 |
|
| 216 |
if( is_null( $info['success'] ) ){ |
| 217 |
\Depicter::options()->set('activation_error_message', '' ); |
| 218 |
|
| 219 |
} elseif ( ! empty( $info['data'] ) ) { |
| 220 |
\Depicter::options()->set('subscription_status', $info['data']['status'] ); |
| 221 |
|
| 222 |
if ( !empty( $info['data']['expires_at'] ) ) { |
| 223 |
\Depicter::options()->set('subscription_expires_at', $info['data']['expires_at'] ); |
| 224 |
} |
| 225 |
|
| 226 |
if ( !empty( $info['data']['subscription_id'] ) ) { |
| 227 |
\Depicter::options()->set('subscription_id', $info['data']['subscription_id'] ); |
| 228 |
} |
| 229 |
|
| 230 |
if ( !empty( $info['data']['manual_renew'] ) ) { |
| 231 |
\Depicter::options()->set('manual_renew', $info['data']['manual_renew'] ); |
| 232 |
} |
| 233 |
|
| 234 |
if ( empty( \Depicter::options()->get('initial_date', '') ) && !empty( $info['data']['created_at'] ) ) { |
| 235 |
\Depicter::options()->set('initial_date', $info['data']['created_at'] ); |
| 236 |
} |
| 237 |
|
| 238 |
\Depicter::options()->set('user_tier', $info['data']['user_tier'] ?? '' ); |
| 239 |
\Depicter::options()->set('activation_error_message', '' ); |
| 240 |
|
| 241 |
if ( $info['data']['status'] == 'active' ) { |
| 242 |
return true; |
| 243 |
} |
| 244 |
} elseif( $info['success'] == 1 ){ |
| 245 |
return true; |
| 246 |
} |
| 247 |
|
| 248 |
if( ! empty( $info['log'] ) ) { |
| 249 |
\Depicter::options()->set('activation_log_message', $info['log'] ); |
| 250 |
} |
| 251 |
|
| 252 |
} catch ( GuzzleException $exception ) { |
| 253 |
\Depicter::options()->set('activation_error_message' , $exception->getMessage() ); |
| 254 |
\Depicter::options()->set('connection_error_message' , $exception->getMessage() ); |
| 255 |
} |
| 256 |
|
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* check if user is new or not |
| 262 |
* @return bool |
| 263 |
*/ |
| 264 |
public function isNewClient() { |
| 265 |
$initialDate = \Depicter::options()->get('initial_date', ''); |
| 266 |
if( empty( $initialDate ) || ( time() - strtotime( $initialDate ) <= 2 * DAY_IN_SECONDS ) ){ |
| 267 |
return empty( \Depicter::documentRepository()->all( [ 'id' ] ) ); |
| 268 |
} |
| 269 |
|
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|