1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Geotools library. |
5
|
|
|
* |
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace League\Geotools\Cache; |
13
|
|
|
|
14
|
|
|
use League\Geotools\Batch\BatchGeocoded; |
15
|
|
|
use League\Geotools\Exception\InvalidArgumentException; |
16
|
|
|
use League\Geotools\Exception\RuntimeException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* MongoDB cache class. |
20
|
|
|
* |
21
|
|
|
* @author Antoine Corcy <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class MongoDB extends AbstractCache implements CacheInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The database name. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
const DATABASE = 'geotools'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The collection name. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
const COLLECTION = 'geotools_cache'; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The collection to work with. |
42
|
|
|
* |
43
|
|
|
* @var MongoCollection |
44
|
|
|
*/ |
45
|
|
|
protected $collection; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor. |
50
|
|
|
* |
51
|
|
|
* @param string $server The server information (optional). |
52
|
|
|
* @param string $database The database name (optional). |
53
|
|
|
* @param string $collection The collection name (optional). |
54
|
|
|
* |
55
|
|
|
* @throws InvalidArgumentException |
56
|
|
|
*/ |
57
|
|
|
public function __construct($server = null, $database = self::DATABASE, $collection = self::COLLECTION) |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
$mongoDB = new \MongoClient($server); |
61
|
|
|
$this->collection = $mongoDB->$database->$collection; |
62
|
|
|
} catch (\Exception $e) { |
63
|
|
|
throw new InvalidArgumentException($e->getMessage()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
|
|
*/ |
70
|
|
|
public function getKey($providerName, $query) |
71
|
|
|
{ |
72
|
|
|
return md5($providerName . $query); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritDoc} |
77
|
|
|
*/ |
78
|
|
|
public function cache(BatchGeocoded $geocoded) |
79
|
|
|
{ |
80
|
|
|
try { |
81
|
|
|
$this->collection->insert( |
82
|
|
|
array_merge( |
83
|
|
|
array('id' => $this->getKey($geocoded->getProviderName(), $geocoded->getQuery())), |
84
|
|
|
$this->normalize($geocoded) |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
} catch (\Exception $e) { |
88
|
|
|
throw new RuntimeException($e->getMessage()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function isCached($providerName, $query) |
96
|
|
|
{ |
97
|
|
|
$result = $this->collection->findOne( |
98
|
|
|
array('id' => $this->getKey($providerName, $query)) |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
if (null === $result) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$cached = new BatchGeocoded; |
106
|
|
|
$cached->fromArray($result); |
107
|
|
|
|
108
|
|
|
return $cached; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritDoc} |
113
|
|
|
*/ |
114
|
|
|
public function flush() |
115
|
|
|
{ |
116
|
|
|
$this->collection->drop(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|