@@ -15,61 +15,65 @@ class OAuthHttpClient
1515
1616 private static $ clientSecret ;
1717
18- private static function getCredentialsInfo () {
18+ private static function getCredentialsInfo ()
19+ {
1920 return collect (json_decode (
2021 file_get_contents (self ::$ path )
2122 ));
2223 }
2324
24- private static function requestToken () {
25+ private static function requestToken ()
26+ {
2527 $ response = Http::acceptJson ()
2628 ->post (self ::$ host . '/oauth/token ' , [
2729 'grant_type ' => 'client_credentials ' ,
2830 'client_id ' => self ::$ clientId ,
2931 'client_secret ' => self ::$ clientSecret ,
3032 ]);
3133
32- if ($ response ->failed ()){
34+ if ($ response ->failed ()) {
3335 abort (403 );
3436 }
3537
3638 Storage::put ('oauth-credentials.json.key ' , $ response ->body ());
3739 }
3840
39- private static function validateToken () {
40- // check if exist file with token
41- if (! file_exists (self ::$ path )){
41+ private static function validateToken ()
42+ {
43+ if (! file_exists (self ::$ path )) {
4244 self ::requestToken ();
4345 }
4446
4547 $ data = self ::getCredentialsInfo ();
4648
4749 $ expiredDate = now ()->parse (date ("Y-m-d H:i:s " , $ data ['expires_in ' ]));
4850
49- if (now ()->lt ($ expiredDate )){
51+ if (now ()->lt ($ expiredDate )) {
5052 self ::requestToken ();
5153 }
5254 }
5355
54- private static function getAuthorizationToken () {
56+ private static function getAuthorizationToken ()
57+ {
5558 self ::validateToken ();
5659
5760 return self ::getCredentialsInfo ()['access_token ' ];
5861 }
5962
60- private static function getOauthAuthorization (){
63+ private static function getOauthAuthorization ()
64+ {
6165 return [
6266 'Authorization ' => 'Bearer ' . self ::getAuthorizationToken ()
6367 ];
6468 }
6569
66- public static function oauthRequest () {
70+ public static function oauthRequest ()
71+ {
6772 self ::$ path = storage_path ('app/oauth-credentials.json.key ' );
6873 self ::$ host = config ('oauth-client.host ' );
6974 self ::$ clientId = config ('oauth-client.client_id ' );
7075 self ::$ clientSecret = config ('oauth-client.client_secret ' );
7176
72- info (self ::$ path );
7377 return Http::withHeaders (
7478 self ::getOauthAuthorization ()
7579 );
0 commit comments