@@ -968,26 +968,16 @@ protected function _getAccessToken(): string|null
968
968
* @param string|null $apiKey
969
969
* @param string|null $secretKey
970
970
* @return string|null
971
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
972
971
*/
973
972
private function retrieveAccessToken (?string $ apiKey , ?string $ secretKey ): string |null
974
973
{
975
- if (!$ apiKey || !$ secretKey ) {
976
- $ this ->_debug (__ ('Authentication keys are missing. ' ));
974
+ if (!$ this ->areAuthKeysValid ($ apiKey , $ secretKey )) {
977
975
return null ;
978
976
}
979
977
$ cacheKey = 'fedex_access_token_ ' . $ apiKey . $ secretKey ;
980
978
$ cacheType = 'fedex_api ' ;
981
- $ cachedData = $ this ->cache ->load ($ cacheKey );
982
- if ($ cachedData ) {
983
- $ cachedData = json_decode ($ cachedData , true );
984
- $ currentTime = time ();
985
- if (isset ($ cachedData ['access_token ' ]) &&
986
- isset ($ cachedData ['expires_at ' ])
987
- && $ currentTime < $ cachedData ['expires_at ' ]
988
- ) {
989
- return $ cachedData ['access_token ' ];
990
- }
979
+ if ($ cachedToken = $ this ->getCachedAccessToken ($ cacheKey )) {
980
+ return $ cachedToken ;
991
981
}
992
982
$ requestArray = [
993
983
'grant_type ' => self ::AUTHENTICATION_GRANT_TYPE ,
@@ -1015,6 +1005,44 @@ private function retrieveAccessToken(?string $apiKey, ?string $secretKey): strin
1015
1005
return $ accessToken ;
1016
1006
}
1017
1007
1008
+ /**
1009
+ * Validate apiKey and secretKey
1010
+ *
1011
+ * @param string|null $apiKey
1012
+ * @param string|null $secretKey
1013
+ * @return bool
1014
+ */
1015
+ private function areAuthKeysValid (?string $ apiKey , ?string $ secretKey ): bool
1016
+ {
1017
+ if (!$ apiKey || !$ secretKey ) {
1018
+ $ this ->_debug (__ ('Authentication keys are missing. ' ));
1019
+ return false ;
1020
+ }
1021
+ return true ;
1022
+ }
1023
+
1024
+ /**
1025
+ * Retrieve access token from cache
1026
+ *
1027
+ * @param string $cacheKey
1028
+ * @return string|null
1029
+ */
1030
+ private function getCachedAccessToken (string $ cacheKey ): ?string
1031
+ {
1032
+ $ cachedData = $ this ->cache ->load ($ cacheKey );
1033
+ if (!$ cachedData ) {
1034
+ return null ;
1035
+ }
1036
+
1037
+ $ cachedData = json_decode ($ cachedData , true );
1038
+ $ currentTime = time ();
1039
+ if (isset ($ cachedData ['access_token ' ], $ cachedData ['expires_at ' ]) && $ currentTime < $ cachedData ['expires_at ' ]) {
1040
+ return $ cachedData ['access_token ' ];
1041
+ }
1042
+
1043
+ return null ;
1044
+ }
1045
+
1018
1046
/**
1019
1047
* Get Access Token for Tracking Rest API
1020
1048
*
0 commit comments