1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Api; |
4
|
|
|
|
5
|
|
|
use DominionEnterprises\Util; |
6
|
|
|
use DominionEnterprises\Util\Arrays; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class to store API results |
10
|
|
|
*/ |
11
|
|
|
final class MongoCache implements Cache |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Mongo collection for storing cache |
15
|
|
|
* |
16
|
|
|
* @var \MongoDB\Collection |
17
|
|
|
*/ |
18
|
|
|
private $collection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Construct a new instance of MongoCache |
22
|
|
|
* |
23
|
|
|
* @param string $url mongo url |
24
|
|
|
* @param string $db name of mongo database |
25
|
|
|
* @param string $collection name of mongo collection |
26
|
|
|
*/ |
27
|
|
|
public function __construct($url, $db, $collection) |
28
|
|
|
{ |
29
|
|
|
Util::ensure( |
30
|
|
|
true, |
31
|
|
|
class_exists('\MongoDB\Client'), |
32
|
|
|
'\RuntimeException', |
33
|
|
|
['mongo extension is required for ' . __CLASS__] |
34
|
|
|
); |
35
|
|
|
Util::throwIfNotType(['string' => [$url, $db, $collection]], true); |
36
|
|
|
$mongo = new \MongoDB\Client( |
37
|
|
|
$url, |
38
|
|
|
[], |
39
|
|
|
['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] |
40
|
|
|
); |
41
|
|
|
$this->collection = $mongo->selectCollection($db, $collection); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @see Cache::set() |
46
|
|
|
*/ |
47
|
|
|
public function set(Request $request, Response $response, $expires = null) |
48
|
|
|
{ |
49
|
|
|
Util::throwIfNotType(['int' => [$expires]], false, true); |
50
|
|
|
|
51
|
|
View Code Duplication |
if ($expires === null) { |
|
|
|
|
52
|
|
|
$expiresHeader = null; |
53
|
|
|
if (!Arrays::tryGet($response->getResponseHeaders(), 'Expires', $expiresHeader)) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$expires = Util::ensureNot( |
58
|
|
|
false, |
59
|
|
|
strtotime($expiresHeader[0]), |
60
|
|
|
"Unable to parse Expires value of '{$expiresHeader[0]}'" |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$id = self::getUniqueId($request); |
65
|
|
|
$cache = [ |
66
|
|
|
'_id' => $id, |
67
|
|
|
'httpCode' => $response->getHttpCode(), |
68
|
|
|
'body' => $response->getResponse(), |
69
|
|
|
'headers' => $response->getResponseHeaders(), |
70
|
|
|
'expires' => new \MongoDB\BSON\UTCDateTime(floor($expires * 1000)), |
71
|
|
|
]; |
72
|
|
|
$this->collection->replaceOne(['_id' => $id], $cache, ['upsert' => true]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @see Cache::get() |
77
|
|
|
*/ |
78
|
|
|
public function get(Request $request) |
79
|
|
|
{ |
80
|
|
|
$cache = $this->collection->findOne(['_id' => self::getUniqueId($request)]); |
81
|
|
|
if ($cache === null) { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return new Response($cache['httpCode'], $cache['headers'], $cache['body']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Ensures proper indexes are created on the mongo cache collection |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function ensureIndexes() |
94
|
|
|
{ |
95
|
|
|
$this->collection->createIndex(['expires' => 1], ['expireAfterSeconds' => 0, 'background' => true]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Helper method to get a unique id of an API request. |
100
|
|
|
* |
101
|
|
|
* This generator does not use the request headers so there is a chance for conflicts |
102
|
|
|
* |
103
|
|
|
* @param Request $request The request from which to generate an unique identifier |
104
|
|
|
* |
105
|
|
|
* @return string the unique identifier |
106
|
|
|
*/ |
107
|
|
|
private static function getUniqueId(Request $request) |
108
|
|
|
{ |
109
|
|
|
return $request->getUrl() . '|' . $request->getBody(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.