Skip to content

Commit 8c8b56d

Browse files
authored
Merge pull request #17 from dpdconnect/1.1.9
1.1.9
2 parents 6b7ef03 + 350d026 commit 8c8b56d

File tree

12 files changed

+167
-146
lines changed

12 files changed

+167
-146
lines changed

src/CacheWrapper.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace DpdConnect\Sdk;
4+
5+
use DpdConnect\Sdk\Resources\CacheInterface;
6+
7+
class CacheWrapper
8+
{
9+
/** @var CacheInterface|null */
10+
public $cache;
11+
12+
/**
13+
* @param CacheInterface|null $cache
14+
*/
15+
public function __construct($cache = null)
16+
{
17+
$this->cache = $cache;
18+
}
19+
20+
/**
21+
* @param $key
22+
* @param false $previous
23+
* @param string $prefix
24+
* @return false|mixed
25+
*/
26+
public function getCachedList($key, $previous = false, $prefix = 'dpd')
27+
{
28+
// This is an hour
29+
$maxAge = 3600;
30+
31+
if($previous) {
32+
// This is a year;
33+
$maxAge = 31556926;
34+
}
35+
36+
// Check if implementation supports own caching
37+
if($this->cache instanceof CacheInterface) {
38+
return $this->cache->getCache($prefix .sha1(serialize($key)) . ($previous ? '_prev': ''));
39+
}
40+
41+
// Fallback to SDK caching
42+
$filename = sys_get_temp_dir().'/dpd/'.sha1($prefix . date('Ymd').serialize($key));
43+
44+
if (!file_exists($filename) || filesize($filename) == 0 || filemtime($filename) < time()-$maxAge) {
45+
return false;
46+
}
47+
48+
return unserialize(file_get_contents($filename));
49+
}
50+
51+
/**
52+
* @param $data
53+
* @param $key
54+
* @param string $prefix
55+
*/
56+
public function storeCachedList($data, $key, $prefix = 'dpd')
57+
{
58+
// This is an hour
59+
$hour = 3600;
60+
// This is a year ;)
61+
$year = 31556926;
62+
63+
// Check if implementation supports own caching
64+
if($this->cache instanceof CacheInterface) {
65+
$this->cache->setCache($prefix. sha1(serialize($key)), $data, $hour);
66+
$this->cache->setCache($prefix .sha1(serialize($key)). '_prev', $data, $year);
67+
68+
return;
69+
}
70+
71+
// Fallback to SDK caching
72+
if (!file_exists(sys_get_temp_dir().'/dpd/')) {
73+
mkdir(sys_get_temp_dir().'/dpd/');
74+
}
75+
76+
$filename = sys_get_temp_dir().'/dpd/'.sha1($prefix.date('Ymd').serialize($key));
77+
file_put_contents($filename, serialize($data));
78+
}
79+
}

src/Client.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use DpdConnect\Sdk\Common\Authentication;
66
use DpdConnect\Sdk\Common\HttpClient;
77
use DpdConnect\Sdk\Common\ResourceClient;
8+
use DpdConnect\Sdk\Resources\CacheableInterface;
9+
use DpdConnect\Sdk\Resources\CacheInterface;
810
use DpdConnect\Sdk\Resources\Country;
911
use DpdConnect\Sdk\Resources\Job;
1012
use DpdConnect\Sdk\Resources\Parcel;
@@ -168,4 +170,16 @@ public function getToken()
168170
{
169171
return $this->token;
170172
}
173+
174+
/**
175+
* @param CacheInterface|null $cache
176+
*/
177+
public function setCacheCallable($cache)
178+
{
179+
foreach ($this as $prop) {
180+
if($prop instanceof CacheableInterface) {
181+
$prop->setCacheWrapper(new CacheWrapper($cache));
182+
}
183+
}
184+
}
171185
}

src/Resources/BaseResource.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DpdConnect\Sdk\Resources;
44

5+
use DpdConnect\Sdk\CacheWrapper;
56
use DpdConnect\Sdk\Common\ResourceClient;
67
use DpdConnect\Sdk\Common\ResponseError;
78
use DpdConnect\Sdk\Exceptions;
@@ -13,7 +14,7 @@
1314
* @SuppressWarnings(PHPMD.NumberOfChildren)
1415
* @SuppressWarnings(PHPMD.TooManyFields)
1516
*/
16-
class BaseResource
17+
class BaseResource implements CacheableInterface
1718
{
1819
/**
1920
* @var ResourceClient
@@ -26,9 +27,16 @@ class BaseResource
2627
protected $object;
2728

2829
/**
29-
* @param ResourceClient $resourceClient
30+
* @var CacheWrapper
3031
*/
31-
public function __construct($resourceClient)
32+
protected $cacheWrapper = null;
33+
34+
/**
35+
* @param $resourceClient
36+
*/
37+
public function __construct(
38+
$resourceClient
39+
)
3240
{
3341
$this->resourceClient = $resourceClient;
3442
}
@@ -73,4 +81,12 @@ public function processRequest($body)
7381
$ResponseError = new ResponseError($body);
7482
throw new RequestException($ResponseError->getErrorString());
7583
}
84+
85+
/**
86+
* @param CacheWrapper|null $cacheWrapper
87+
*/
88+
public function setCacheWrapper($cacheWrapper)
89+
{
90+
$this->cacheWrapper = $cacheWrapper;
91+
}
7692
}

src/Resources/CacheInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace DpdConnect\Sdk\Resources;
4+
5+
interface CacheInterface
6+
{
7+
/**
8+
* @param string $key
9+
* @param $data
10+
* @param int $expire
11+
* @return mixed
12+
*/
13+
public function setCache($key, $data, $expire);
14+
15+
/**
16+
* @param string $key
17+
* @return mixed
18+
*/
19+
public function getCache($key);
20+
}

src/Resources/CacheableInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace DpdConnect\Sdk\Resources;
4+
5+
use DpdConnect\Sdk\CacheWrapper;
6+
7+
interface CacheableInterface
8+
{
9+
/**
10+
* @param CacheWrapper $cacheWrapper
11+
*/
12+
public function setCacheWrapper($cacheWrapper);
13+
}

src/Resources/Country.php

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,23 @@
99
*/
1010
class Country extends BaseResource
1111
{
12-
/**
13-
* @param ResourceClient $resourceClient
14-
*/
15-
public function __construct(
16-
ResourceClient $resourceClient
17-
) {
18-
parent::__construct($resourceClient);
19-
}
20-
2112
/**
2213
* @param array $query
2314
*
2415
* @return array
2516
*/
2617
public function getList($query = [])
2718
{
28-
$result = $this->getCachedList($query);
19+
$result = $this->cacheWrapper->getCachedList($query);
2920

3021
if ($result) {
3122
return $result;
3223
}
3324

3425
$this->resourceClient->setResourceName('api/connect/v1/countries');
3526
$countries = $this->resourceClient->getResources($query);
36-
$this->storeCachedList($countries, $query);
27+
$this->cacheWrapper->storeCachedList($countries, $query);
3728

3829
return $countries;
3930
}
40-
41-
/**
42-
* @param $query
43-
*
44-
* @return false|mixed
45-
*/
46-
private function getCachedList($query)
47-
{
48-
$filename = sys_get_temp_dir().'/dpd/'.sha1('dpd'.date('Ymd').serialize($query));
49-
50-
if (!file_exists($filename) || filesize($filename) == 0) {
51-
return false;
52-
}
53-
54-
return unserialize(file_get_contents($filename));
55-
}
56-
57-
/**
58-
* @param $countries
59-
* @param $query
60-
*/
61-
private function storeCachedList($countries, $query)
62-
{
63-
if (!file_exists(sys_get_temp_dir().'/dpd/')) {
64-
mkdir(sys_get_temp_dir().'/dpd/');
65-
}
66-
67-
$filename = sys_get_temp_dir().'/dpd/'.sha1('dpd'.date('Ymd').serialize($query));
68-
file_put_contents($filename, serialize($countries));
69-
}
7031
}

src/Resources/Job.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010
*/
1111
class Job extends BaseResource
1212
{
13-
/**
14-
* @param ResourceClient $resourceClient
15-
*/
16-
public function __construct($resourceClient)
17-
{
18-
parent::__construct($resourceClient);
19-
}
20-
2113
/**
2214
* @param $id
2315
*

src/Resources/Parcel.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111
*/
1212
class Parcel extends BaseResource
1313
{
14-
/**
15-
* @param ResourceClient $resourceClient
16-
*/
17-
public function __construct($resourceClient)
18-
{
19-
parent::__construct($resourceClient);
20-
}
21-
2214
/**
2315
* @param array $query
2416
*

src/Resources/Parcelshop.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
*/
1010
class Parcelshop extends BaseResource
1111
{
12-
/**
13-
* @param ResourceClient $resourceClient
14-
*/
15-
public function __construct($resourceClient)
16-
{
17-
parent::__construct($resourceClient);
18-
}
19-
2012
/**
2113
* @param $id
2214
*

src/Resources/Product.php

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,68 +13,34 @@
1313
*/
1414
class Product extends BaseResource
1515
{
16-
/**
17-
* @param ResourceClient $resourceClient
18-
*/
19-
public function __construct(
20-
ResourceClient $resourceClient
21-
) {
22-
parent::__construct($resourceClient);
23-
}
24-
2516
/**
2617
* @param array $query
2718
* @return array
2819
* @throws DpdException
2920
*/
3021
public function getList($query = [])
3122
{
32-
$result = $this->getCachedList($query);
23+
$result = $this->cacheWrapper->getCachedList($query);
3324
if ($result) {
3425
return $result;
3526
}
3627

3728
$this->resourceClient->setResourceName('api/connect/v1/available-products');
3829
try {
3930
$products = $this->resourceClient->getResources($query);
40-
$this->storeCachedList($products, $query);
31+
32+
$this->cacheWrapper->storeCachedList($products, $query);
4133
return $products;
4234
} catch (DpdException $e) {
43-
$result = $this->getCachedList($query,31556926); //a year
35+
$result = $this->cacheWrapper->getCachedList($query, true); //a year
36+
37+
// So we had a failure save cache now for an hour
38+
$this->cacheWrapper->storeCachedList($result, $query);
39+
4440
if ($result) {
4541
return $result;
4642
}
4743
return [];
4844
}
4945
}
50-
51-
/**
52-
* @param $query
53-
*
54-
* @return false|mixed
55-
*/
56-
private function getCachedList($query, $maxAge = 3600)
57-
{
58-
$filename = sys_get_temp_dir() . '/dpd/dpd-products' ;
59-
60-
if (!file_exists($filename) || filesize($filename) == 0 || filemtime($filename) < time()-$maxAge) {
61-
return false;
62-
}
63-
64-
return unserialize(file_get_contents($filename));
65-
}
66-
67-
/**
68-
* @param $products
69-
* @param $query
70-
*/
71-
private function storeCachedList($products, $query)
72-
{
73-
if (!file_exists(sys_get_temp_dir() . '/dpd/')) {
74-
mkdir(sys_get_temp_dir() . '/dpd/');
75-
}
76-
77-
$filename = sys_get_temp_dir() .'/dpd/dpd-products';
78-
file_put_contents($filename, serialize($products));
79-
}
8046
}

0 commit comments

Comments
 (0)