Skip to content

Commit 097119b

Browse files
committed
ACP2E-3930: [QUANS] - Does Magento_Fedex core module check for a valid-active token before sending a request to get a new one?
1 parent c037435 commit 097119b

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

app/code/Magento/Fedex/Model/Carrier.php

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -968,26 +968,16 @@ protected function _getAccessToken(): string|null
968968
* @param string|null $apiKey
969969
* @param string|null $secretKey
970970
* @return string|null
971-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
972971
*/
973972
private function retrieveAccessToken(?string $apiKey, ?string $secretKey): string|null
974973
{
975-
if (!$apiKey || !$secretKey) {
976-
$this->_debug(__('Authentication keys are missing.'));
974+
if (!$this->areAuthKeysValid($apiKey, $secretKey)) {
977975
return null;
978976
}
979977
$cacheKey = 'fedex_access_token_' . $apiKey . $secretKey;
980978
$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;
991981
}
992982
$requestArray = [
993983
'grant_type' => self::AUTHENTICATION_GRANT_TYPE,
@@ -1015,6 +1005,44 @@ private function retrieveAccessToken(?string $apiKey, ?string $secretKey): strin
10151005
return $accessToken;
10161006
}
10171007

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+
10181046
/**
10191047
* Get Access Token for Tracking Rest API
10201048
*

app/code/Magento/Fedex/Test/Unit/Model/CarrierTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use PHPUnit\Framework\TestCase;
5050
use Psr\Log\LoggerInterface;
5151
use Magento\Catalog\Api\Data\ProductInterface;
52+
use Magento\Framework\App\CacheInterface;
5253

5354
/**
5455
* CarrierTest contains units test for Fedex carrier methods
@@ -132,6 +133,11 @@ class CarrierTest extends TestCase
132133
*/
133134
private DecoderInterface $decoderInterface;
134135

136+
/**
137+
* @var CacheInterface|MockObject
138+
*/
139+
private $cacheMock;
140+
135141
/**
136142
* @return void
137143
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -206,6 +212,9 @@ protected function setUp(): void
206212
->onlyMethods(['decode'])
207213
->getMock();
208214

215+
$this->cacheMock = $this->getMockBuilder(CacheInterface::class)
216+
->onlyMethods(['load', 'save'])
217+
->getMockForAbstractClass();
209218
$this->carrier = $this->getMockBuilder(Carrier::class)
210219
->addMethods(['rateRequest'])
211220
->setConstructorArgs(
@@ -229,6 +238,7 @@ protected function setUp(): void
229238
'productCollectionFactory' => $this->collectionFactory,
230239
'curlFactory' => $this->curlFactory,
231240
'decoderInterface' => $this->decoderInterface,
241+
'cache' => $this->cacheMock,
232242
'data' => [],
233243
'serializer' => $this->serializer,
234244
]

0 commit comments

Comments
 (0)