Skip to content

Commit

Permalink
Merge pull request #15 from dpdconnect/1.1.7
Browse files Browse the repository at this point in the history
1.1.7
  • Loading branch information
dpdplugin authored Mar 7, 2022
2 parents 991526a + 2fe1673 commit 7d7941d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
12 changes: 7 additions & 5 deletions src/Common/AuthenticatedHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ public function sendRequest($httpMethod, $resourceName, array $query = [], array
$this->authentication->getPassword()
);

$this->authentication
->setJwtToken($tokens['token']);
if (is_callable($this->authentication->tokenUpdateCallback)) {
call_user_func($this->authentication->tokenUpdateCallback, $this->authentication->getJwtToken());
if (isset($tokens['token'])) {
$this->authentication
->setJwtToken($tokens['token']);
if (is_callable($this->authentication->tokenUpdateCallback)) {
call_user_func($this->authentication->tokenUpdateCallback, $this->authentication->getJwtToken());
}
}
}

Expand Down Expand Up @@ -85,7 +87,7 @@ public function sendRequest($httpMethod, $resourceName, array $query = [], array
);
$response = $this->basicHttpClient->sendRequest($httpMethod, $resourceName, $query, $headers, $body);
} catch (AuthenticateException $exception) {
throw $exception;
$response = [];
}

}
Expand Down
21 changes: 12 additions & 9 deletions src/Resources/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,19 @@ protected function authenticate($requestBody)
$headers = [
'Content-Type' => 'application/json',
];
try {
$response = $this->httpClient->sendRequest(
HttpClient::REQUEST_POST,
self::RESOURCE_URI_AUTH,
false,
$headers,
json_encode($requestBody)
);
return $this->processRequest($response);
}catch (\Exception $exception){
return [];
}

$response = $this->httpClient->sendRequest(
HttpClient::REQUEST_POST,
self::RESOURCE_URI_AUTH,
false,
$headers,
json_encode($requestBody)
);

return $this->processRequest($response);
}

/**
Expand Down
14 changes: 9 additions & 5 deletions src/Resources/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public function getList($query = [])
$this->storeCachedList($products, $query);
return $products;
} catch (DpdException $e) {
throw $e;
$result = $this->getCachedList($query,31556926); //a year
if ($result) {
return $result;
}
return [];
}
}

Expand All @@ -49,11 +53,11 @@ public function getList($query = [])
*
* @return false|mixed
*/
private function getCachedList($query)
private function getCachedList($query, $maxAge = 3600)
{
$filename = sys_get_temp_dir() . '/dpd/' . sha1('dpd-products' . date('YmdH') . serialize($query));
$filename = sys_get_temp_dir() . '/dpd/dpd-products' ;

if (!file_exists($filename) || filesize($filename) == 0) {
if (!file_exists($filename) || filesize($filename) == 0 || filemtime($filename) < time()-$maxAge) {
return false;
}

Expand All @@ -70,7 +74,7 @@ private function storeCachedList($products, $query)
mkdir(sys_get_temp_dir() . '/dpd/');
}

$filename = sys_get_temp_dir() .'/dpd/' . sha1('dpd-products' . date('YmdH') . serialize($query));
$filename = sys_get_temp_dir() .'/dpd/dpd-products';
file_put_contents($filename, serialize($products));
}
}
4 changes: 2 additions & 2 deletions src/Resources/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function isTokenValid($token)
*/
private function getCachedPublicJWTToken($username)
{
$filename = sys_get_temp_dir() . '/dpd/' . sha1('dpd-products' . date('YmdH') . serialize($username));
$filename = sys_get_temp_dir() . '/dpd/' . sha1('dpd-token' . date('YmdH') . serialize($username));

if (!file_exists($filename) || filesize($filename) == 0) {
return false;
Expand All @@ -123,7 +123,7 @@ private function storeCachedPublicJWTToken($token, $username)
mkdir(sys_get_temp_dir() . '/dpd/');
}

$filename = sys_get_temp_dir() .'/dpd/' . sha1('dpd-products' . date('YmdH') . serialize($username));
$filename = sys_get_temp_dir() .'/dpd/' . sha1('dpd-token' . date('YmdH') . serialize($username));
file_put_contents($filename, serialize($token));
}
}

0 comments on commit 7d7941d

Please sign in to comment.