-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkPeer.php
262 lines (232 loc) · 6.53 KB
/
LinkPeer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
require_once(dirname(__FILE__) . '/Link.php');
/**
* LinkPeer - interacts with the persistence layer to offer common
* methods such as retrieval and storing
*
* @author Paul Matthews <pmatthews@ibuildings.com>
*/
class LinkPeer
{
/**
* The collection to be used within the database
*/
const COLLECTION = 'link';
/**
* The id filed in the dataset
*/
const ID_FIELD = 'url';
/**
* The id filed for the database
*/
const DB_ID_FIELD = '_id';
/**
* db the database to interact with
* MongoDb in this example
*
* @var MongoDb
* @access private
*/
private $db;
/**
* collection a handle to the the current database collection
*
* @var MongoCollection
* @access private
*/
private $collection;
/**
* __construct requires an instance of the database to interact with
*
* @param MongoDb $db
* @access public
* @return void
*/
public function __construct(MongoDb $db)
{
$this->db = $db;
}
/**
* insert a link or update it
*
* @param Link $link to be inserted into the database
* @access public
* @return boolean true if the insertion succeeded
*/
public function insert(Link $link)
{
return $this->update($link);
}
/**
* update a link or create it
*
* @param Link $link to be inserted into the database
* @access public
* @return boolean true if the insertion succeeded
*/
public function update(Link $link)
{
$data = $this->translateDataset($link->toArray(), true);
if ($data && $this->getCollection()->save($data)) {
return true;
}
return false;
}
/**
* delete a link
*
* @param Link $link the link to be removed
* @access public
* @return boolean true if the delete succeeded
*/
public function delete(Link $link)
{
$link = $this->translateDataset($link->toArray(), true);
$linkKey = $this->getIdFromDataset($link);
$key = array(
'_id' => $linkKey,
);
if ($linkKey) {
$options = array(
'fsync' => true, // Forces the update to be synced to disk
);
try {
if ($this->getCollection()->remove($key, $options)) {
return true;
}
} catch (MongoException $e) {
// @TODO log the error
}
}
return false;
}
/**
* fetchAll returns all the links in the current collection
*
* @access public
* @return Array<Link> the array of links
*/
public function fetchAll()
{
$results = array();
// perform a find with a blank query
foreach ($this->getCollection()->find() as $result) {
// Create a Link out of each of the results
$results[] = $this->factory($result);
}
return $results;
}
/**
* fetchByUrl retrieves a link object by url
*
* @param string $url the url to fetch Links by
* @access public
* @return Link the corresponding Link object or null if none is found
*/
public function fetchByUrl($url)
{
$query = array('url' => $url);
$mongoQuery = $this->translateDataset($query);
$response = $this->getCollection()->findOne(
$mongoQuery
);
// if the database returns a result return the link
if ($response) {
return $this->factory($response);
}
// Otherwise return null
return null;
}
/**
* fetchByTag retreves links by tag
*
* @param string $tag the tag to fetch Links by
* @access public
* @return Array<Link> the array of links
*/
public function fetchByTag($tag)
{
$results = array();
$collection = $this->getCollection();
foreach ($collection->find(array('tags' => $tag)) as $result) {
$results[] = $this->factory($result);
}
return $results;
}
/**
* Get the value from the dataset
*
* @param array $data
* @access public
* @return mixed the id string if its set or false if it isn't
*/
public function getIdFromDataset(array $data)
{
if (!isset($data[self::DB_ID_FIELD])) {
return false;
}
return $data[self::DB_ID_FIELD];
}
/**
* Prepare the dataset for use with the database or the application.
*
* @param array $data the data for the non peer object
* @param mixed $toDb true if to database, false to application
* @access public
* @return mixed the dataset or false if the key couldn't be found
*/
public function translateDataset(array $data, $toDb = true)
{
$keyFrom = self::ID_FIELD;
$keyTo = self::DB_ID_FIELD;
if (!$toDb) {
$keyFrom = self::DB_ID_FIELD;
$keyTo = self::ID_FIELD;
}
if (!isset($data[$keyFrom])) {
return false;
}
$data[$keyTo] = $data[$keyFrom];
unset($data[$keyFrom]);
return $data;
}
/**
* getCollection get a handle to the collection object
*
* @access private
* @return MongoCollection the mongo collection class reffering to the
* appropriate dataset
*/
private function getCollection()
{
// if the collection is not already cached
if (empty($this->collection)) {
// selects the collection if it exists
$this->collection = $this->db->selectCollection(self::COLLECTION);
// or creates a new one if it doesn't
if (empty($this->collection)) {
$this->collection = $this->db->createCollection(
self::COLLECTION
);
}
}
return $this->collection;
}
/**
* factory get a new instance of a Link from an array defining
* it's properties in the Link->toArray() format
*
* @param array $linkArray the array defining the Link object
* @access private
* @return Link the link object as populated by the linkArray
*/
private function factory($linkArray)
{
if (!isset($linkArray['_id'])) {
throw new Exception('Missing data');
}
$linkArray = $this->translateDataset($linkArray, false);
$tmp = new Link();
return $tmp->fromArray($linkArray);
}
}