Skip to content

Commit 7d7941d

Browse files
authored
Merge pull request #15 from dpdconnect/1.1.7
1.1.7
2 parents 991526a + 2fe1673 commit 7d7941d

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

src/Common/AuthenticatedHttpClient.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ public function sendRequest($httpMethod, $resourceName, array $query = [], array
5252
$this->authentication->getPassword()
5353
);
5454

55-
$this->authentication
56-
->setJwtToken($tokens['token']);
57-
if (is_callable($this->authentication->tokenUpdateCallback)) {
58-
call_user_func($this->authentication->tokenUpdateCallback, $this->authentication->getJwtToken());
55+
if (isset($tokens['token'])) {
56+
$this->authentication
57+
->setJwtToken($tokens['token']);
58+
if (is_callable($this->authentication->tokenUpdateCallback)) {
59+
call_user_func($this->authentication->tokenUpdateCallback, $this->authentication->getJwtToken());
60+
}
5961
}
6062
}
6163

@@ -85,7 +87,7 @@ public function sendRequest($httpMethod, $resourceName, array $query = [], array
8587
);
8688
$response = $this->basicHttpClient->sendRequest($httpMethod, $resourceName, $query, $headers, $body);
8789
} catch (AuthenticateException $exception) {
88-
throw $exception;
90+
$response = [];
8991
}
9092

9193
}

src/Resources/Authentication.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,19 @@ protected function authenticate($requestBody)
5454
$headers = [
5555
'Content-Type' => 'application/json',
5656
];
57+
try {
58+
$response = $this->httpClient->sendRequest(
59+
HttpClient::REQUEST_POST,
60+
self::RESOURCE_URI_AUTH,
61+
false,
62+
$headers,
63+
json_encode($requestBody)
64+
);
65+
return $this->processRequest($response);
66+
}catch (\Exception $exception){
67+
return [];
68+
}
5769

58-
$response = $this->httpClient->sendRequest(
59-
HttpClient::REQUEST_POST,
60-
self::RESOURCE_URI_AUTH,
61-
false,
62-
$headers,
63-
json_encode($requestBody)
64-
);
65-
66-
return $this->processRequest($response);
6770
}
6871

6972
/**

src/Resources/Product.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public function getList($query = [])
4040
$this->storeCachedList($products, $query);
4141
return $products;
4242
} catch (DpdException $e) {
43-
throw $e;
43+
$result = $this->getCachedList($query,31556926); //a year
44+
if ($result) {
45+
return $result;
46+
}
47+
return [];
4448
}
4549
}
4650

@@ -49,11 +53,11 @@ public function getList($query = [])
4953
*
5054
* @return false|mixed
5155
*/
52-
private function getCachedList($query)
56+
private function getCachedList($query, $maxAge = 3600)
5357
{
54-
$filename = sys_get_temp_dir() . '/dpd/' . sha1('dpd-products' . date('YmdH') . serialize($query));
58+
$filename = sys_get_temp_dir() . '/dpd/dpd-products' ;
5559

56-
if (!file_exists($filename) || filesize($filename) == 0) {
60+
if (!file_exists($filename) || filesize($filename) == 0 || filemtime($filename) < time()-$maxAge) {
5761
return false;
5862
}
5963

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

73-
$filename = sys_get_temp_dir() .'/dpd/' . sha1('dpd-products' . date('YmdH') . serialize($query));
77+
$filename = sys_get_temp_dir() .'/dpd/dpd-products';
7478
file_put_contents($filename, serialize($products));
7579
}
7680
}

src/Resources/Token.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private function isTokenValid($token)
104104
*/
105105
private function getCachedPublicJWTToken($username)
106106
{
107-
$filename = sys_get_temp_dir() . '/dpd/' . sha1('dpd-products' . date('YmdH') . serialize($username));
107+
$filename = sys_get_temp_dir() . '/dpd/' . sha1('dpd-token' . date('YmdH') . serialize($username));
108108

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

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

0 commit comments

Comments
 (0)